ViewPager 不显示自定义视图
ViewPager not showing custom views
正如标题所说,ViewPager 没有显示它应该显示的内容。我到处寻找解决方案,但其中 none 有效。也许你们可以帮助我。提前致谢!
我想向适配器动态添加自定义视图,因为视图与数据库中的数据异步。
自定义 PagerAdapter
public class SlidePagerAdapter extends PagerAdapter {
private Context mContext;
private List<CardView> mCards;
public SlidePagerAdapter(Context context) {
mContext = context;
mCards = new ArrayList<>();
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public int getItemPosition (Object object) {
int index = mCards.indexOf(object);
if (index == -1)
return POSITION_NONE;
else
return index;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mCards.get(position);
container.addView(v);
return container;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView(mCards.get(position));
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public void add(Card card) {
mCards.add(new CardView(mContext, card));
}
public void addAll(List<Card> cards) {
for (Card card : cards) {
add(card);
}
}
public void clear() {
mShopcards.clear();
}
}
自定义视图已成功创建并保存在适配器的列表中,但它们没有显示在 ViewPager 中...
检查这个:
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
不能比较 2.view 是 object 的子对象。
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mShopcards.get(position);
container.addView(v);
return container;
}
正如标题所说,ViewPager 没有显示它应该显示的内容。我到处寻找解决方案,但其中 none 有效。也许你们可以帮助我。提前致谢!
我想向适配器动态添加自定义视图,因为视图与数据库中的数据异步。
自定义 PagerAdapter
public class SlidePagerAdapter extends PagerAdapter {
private Context mContext;
private List<CardView> mCards;
public SlidePagerAdapter(Context context) {
mContext = context;
mCards = new ArrayList<>();
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public int getItemPosition (Object object) {
int index = mCards.indexOf(object);
if (index == -1)
return POSITION_NONE;
else
return index;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mCards.get(position);
container.addView(v);
return container;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView(mCards.get(position));
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public void add(Card card) {
mCards.add(new CardView(mContext, card));
}
public void addAll(List<Card> cards) {
for (Card card : cards) {
add(card);
}
}
public void clear() {
mShopcards.clear();
}
}
自定义视图已成功创建并保存在适配器的列表中,但它们没有显示在 ViewPager 中...
检查这个:
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
不能比较 2.view 是 object 的子对象。
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mShopcards.get(position);
container.addView(v);
return container;
}