1

我想實現一個ViewPager動態視圖,即當用戶向左/向右輕拂/拖動視圖時,視圖會自動更新。使用預存儲中的下一行更新視圖數據庫。我可以如何實現它?
我想這樣做的原因是:我有大約3000個分貝條目,我想一次顯示一個。我無法一次創建這麼多的視圖。Viewpager具有大量的視圖或動態視圖

回答

1

如波紋管,希望能幫到你。

public class BottomAdatper extends PagerAdapter { 
@Override 
public int getCount() { 
    return 10000; 
} 

@Override 
public boolean isViewFromObject(View view, Object o) { 
    return view == o; 
} 

ArrayList<TextView> caches = new ArrayList<>(); 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    caches.add((TextView) object); 
    container.removeView((View) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    TextView tv = null; 
    if (caches.size() == 0) { 
     tv = new TextView(container.getContext()); 
     tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     tv.setGravity(Gravity.CENTER); 
     tv.setTextSize(60); 
     tv.setBackgroundColor(Color.YELLOW); 
    } else { 
     tv = caches.remove(0); 
    } 
    tv.setText("" + position); 
    container.addView(tv); 
    return tv; 
} 
}