我的适配器
public class InitialViewPagerAdapter extends PagerAdapter {
private LayoutInflater mInflater;
private Context mContext;
private RelativeLayout mLayoutToInflate;
private ArrayList<InitialModel> mInitialModelList;
public InitialViewPagerAdapter(Context mContext, ArrayList<InitialModel> mInitialModelList) {
this.mContext = mContext;
this.mInitialModelList = mInitialModelList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((RelativeLayout) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mInflater.inflate(R.layout.item_initial_activity_viewpager, container, false);
mLayoutToInflate = (RelativeLayout) itemView.findViewById(R.id.layout_to_inflated_initial);
TextView title = (TextView) itemView.findViewById(R.id.txt_initial_title);
TextView descrition1 = (TextView) itemView.findViewById(R.id.txt_initial_above_image);
ImageView image = (ImageView) itemView.findViewById(R.id.img_initial);
ImageView imageButton = (ImageView) itemView.findViewById(R.id.img_initial_button);
ImageView imageTitle = (ImageView) itemView.findViewById(R.id.img_initial_title);
TextView textButton = (TextView) itemView.findViewById(R.id.txt_initial_button);
TextView description2 = (TextView) itemView.findViewById(R.id.txt_initial_below_image);
title.setText(mInitialModelList.get(position).getmTitle());
imageTitle.setBackgroundResource(mInitialModelList.get(position).getmImageTitle());
descrition1.setText(mInitialModelList.get(position).getmDescription1());
image.setBackgroundResource(mInitialModelList.get(position).getmImage());
if (mInitialModelList.get(position).getmTextButton() != null) {
textButton.setText(mInitialModelList.get(position).getmTextButton());
}
if (mInitialModelList.get(position).getmImageButton() != 0) {
imageButton.setBackgroundResource(mInitialModelList.get(position).getmImageButton());
}
description2.setText(mInitialModelList.get(position).getmDescription2());
((ViewPager) container).addView(itemView);
return itemView;
}
}
在我的MainActivity中,我设置了
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==0){
btnFunction.setVisibility(View.GONE);
}
if(position==2){
btnFunction = (RelativeLayout)findViewById(R.id.btn_initial);
btnFunction.setVisibility(View.VISIBLE);
}
if(position==3){
btnFunction = (RelativeLayout)findViewById(R.id.btn_initial);
btnFunction.setVisibility(View.GONE);
}
if(position==4){
btnFunction = (RelativeLayout)findViewById(R.id.btn_initial);
btnFunction.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
我想要的是在ViewPager的特定页面上设置按钮消失,但它会使按钮在所有页面上消失。请告诉我该怎么做!提前谢谢!
答案 0 :(得分:0)
您需要传递要删除按钮的位置列表,并在initializeView()
检查位置并执行您想要执行的操作。
请查看以下代码并发表评论:
public class InitialViewPagerAdapter extends PagerAdapter {
private LayoutInflater mInflater;
private Context mContext;
private RelativeLayout mLayoutToInflate;
private ArrayList<InitialModel> mInitialModelList;
private ArrayList<Integer> pageListToRemoveBtn;
public InitialViewPagerAdapter(Context mContext, ArrayList<InitialModel> mInitialModelList,ArrayList<String> list) {
this.mContext = mContext;
this.mInitialModelList = mInitialModelList;
this.pageListToRemoveBtn = list; // get page no where to remove button
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((RelativeLayout) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mInflater.inflate(R.layout.item_initial_activity_viewpager, container, false);
mLayoutToInflate = (RelativeLayout) itemView.findViewById(R.id.layout_to_inflated_initial);
TextView title = (TextView) itemView.findViewById(R.id.txt_initial_title);
TextView descrition1 = (TextView) itemView.findViewById(R.id.txt_initial_above_image);
ImageView image = (ImageView) itemView.findViewById(R.id.img_initial);
ImageView imageButton = (ImageView) itemView.findViewById(R.id.img_initial_button);
ImageView imageTitle = (ImageView) itemView.findViewById(R.id.img_initial_title);
TextView textButton = (TextView) itemView.findViewById(R.id.txt_initial_button);
TextView description2 = (TextView) itemView.findViewById(R.id.txt_initial_below_image);
title.setText(mInitialModelList.get(position).getmTitle());
imageTitle.setBackgroundResource(mInitialModelList.get(position).getmImageTitle());
descrition1.setText(mInitialModelList.get(position).getmDescription1());
image.setBackgroundResource(mInitialModelList.get(position).getmImage());
if (mInitialModelList.get(position).getmTextButton() != null) {
textButton.setText(mInitialModelList.get(position).getmTextButton());
}
if (mInitialModelList.get(position).getmImageButton() != 0) {
imageButton.setBackgroundResource(mInitialModelList.get(position).getmImageButton());
}
if(pageListToRemoveBtn.contains(positino))
{
// remove whatever view you want to remove.
}
description2.setText(mInitialModelList.get(position).getmDescription2());
((ViewPager) container).addView(itemView);
return itemView;
}
}