2017-10-05 166 views
1

我在我的應用程序中有兩個標籤,每個都有gridview控股20+ imageview s。我成功地實現了我想要的,但問題是,當我從第一個標籤頁滑動到第二個標籤頁時,轉換並不平滑。我爲gridview s使用相同的適配器。android |平滑缺少交換標籤gridview

下面是我的適配器實現

public class LogoImageAdapter extends BaseAdapter{ 
    private Context context; 
    private List<Integer> logoIds; 
    public LogoImageAdapter(Context context, List<Integer> logoIds) { 
     this.context = context; 
     this.logoIds = logoIds; 
    } 
    @Override 
    public int getCount() { 
     return logoIds.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return logoIds.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return logoIds.get(position); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View gridView; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      gridView = inflater.inflate(R.layout.grid_item, null); 
     } else { 
      gridView = convertView; 
     } 
     /*TextView title = (TextView) gridView.findViewById(R.id.siteNameTV); 
      title.setText("Title_" + position);*/ 
     ImageView logoImage = (ImageView) gridView.findViewById(R.id.logoImageView); 
     logoImage.setImageResource(logoIds.get(position)); 
     return gridView; 
    } 
} 

請幫我獲得平穩的過渡選項卡。

編輯:ViewPager實施

public class HomepageActivity extends AppCompatActivity { 
    private SectionsPagerAdapter mSectionsPagerAdapter; 
    private ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_homepage); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     if (getSupportActionBar()!=null) 
      getSupportActionBar().setDisplayShowTitleEnabled(false); 
     // Create the adapter that will return a fragment for each of the primary sections/tabs of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_items, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.mShare: 
       shareApplication(); 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 
     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        return new Homepage(); 
       case 1: 
        return new Classified(); 
       case 2: 
        return new Tourism(); 
       case 3: 
        return new Shopping(); 
       case 4: 
        return new Travel(); 
       default: 
        return null; 
      } 
     } 
     @Override 
     public int getCount() { 
      // number of tabs. 
      return Constants.TAB_NUMBER; 
     } 
     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return Constants.TAB_HOMEPAGE; 
       case 1: 
        return Constants.TAB_CLASSIFIED; 
       case 2: 
        return Constants.TAB_TOURISM; 
       case 3: 
        return Constants.TAB_SHOPPING; 
       case 4: 
        return Constants.TAB_TRAVEL; 
       default: 
        return null; 
      } 
     } 
    } 
} 
+0

你的viewpager代碼是什麼樣的? – Jon

+0

@ Jonathan727我用viewpager實現更新了我的問題。 – MrG

回答

0

它接縫你用同一個適配器來加載圖片的兩個標籤,因此可以有一些延遲。然後檢查您的圖像尺寸是否大或小圖像(高分辨率)。由於圖像網格視圖的大尺寸可能會導致加載適配器時出現相同的延遲。我之前也有過這方面的經驗,我減少了圖像的大小,減少了延遲。