我正在我的应用程序中实现视图寻呼机,它可以工作,但只显示一个元素。
如果我想显示所有卡片并且如果多余的卡片转到其他页面怎么办?
这是我的代码
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_margin="@dimen/fab_margin"
android:foregroundGravity="center"
android:overScrollMode="never"
android:clipToPadding="true" />
onCreate
models=new ArrayList<>();
models.add(new ViewPagerModel(R.drawable.icon,""+getString(R.string.app_profile)));
models.add(new ViewPagerModel(R.drawable.ic_school,""+R.string.mainurl));
models.add(new ViewPagerModel(R.drawable.ic_class,""+R.string.eForm));
models.add(new ViewPagerModel(R.drawable.ic_info,""+R.string.about));
models.add(new ViewPagerModel(R.drawable.noaman,""+R.string.about_the_programmer));
adapter=new ViewPagerAdapter(models,getContext());
viewPager=view.findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.setPadding(8,0,8,0);
查看寻呼机适配器
public class ViewPagerAdapter extends PagerAdapter {
private List<ViewPagerModel> models;
private LayoutInflater layoutInflater;
private Context context;
public ViewPagerAdapter(List<ViewPagerModel> models, Context context) {
this.models = models;
this.context = context;
}
@Override
public int getCount() {
return models.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.item,container,false);
ImageView imageView;
TextView title;
imageView=view.findViewById(R.id.pagerImage);
title=view.findViewById(R.id.pagerTitle);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
container.addView(view,0);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
}
答案 0 :(得分:0)
传送时间您正在将视图添加到0位
替换container.addView(view,0);
与container.addView(view);