How to delete item from viewpager and pageradapter

前端 未结 2 994
鱼传尺愫
鱼传尺愫 2021-01-05 08:56

I am using pageradapter and viewpager to display imageview array,textview array .But how do I delete pager on button click .Entire code in the code below ,but I have added b

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 09:52

    first you need to use List because of dynamically deleting objects. second you need

        @Override
        public int getItemPosition(Object object){
            return PagerAdapter.POSITION_NONE;
        }
    

    to force the adapter to create new page each time and not using in memory pages.

     public class ViewPagerAdapter extends PagerAdapter {
        // Declare Variables
        Context context;
        List rank;
        List country;
        List population;
        List flag = new ArrayList();
        LayoutInflater inflater;
    
        public ViewPagerAdapter(Context context, String[] rank, String[] country,
                String[] population, int[] flag) {
            this.context = context;
            this.rank = new ArrayList(Arrays.asList(rank));
            this.country = new ArrayList(Arrays.asList(country));
            this.population = new ArrayList(Arrays.asList(population));
            for (int i : flag){ 
                this.flag.add(i);
            }
    
        }
    
        @Override
        public int getCount() {
            return rank.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((RelativeLayout) object);
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
    
            // Declare Variables
            TextView txtrank;
            TextView txtcountry;
            TextView txtpopulation;
            ImageView imgflag;
    
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View itemView = inflater.inflate(R.layout.viewpager_item, container,
                    false);
    
            // Locate the TextViews in viewpager_item.xml
            txtrank = (TextView) itemView.findViewById(R.id.rank);
            txtcountry = (TextView) itemView.findViewById(R.id.country);
            txtpopulation = (TextView) itemView.findViewById(R.id.population);
             Button b=(Button)itemView.findViewById(R.id.button1);
    
             final int delPosition = position;
    
             b.setOnClickListener(new OnClickListener() {
    
                 @Override
                 public void onClick(View v) {
                     rank.remove(delPosition);
                     country.remove(delPosition);
                     population.remove(delPosition);
                     flag.remove(delPosition);
                     notifyDataSetChanged();
                 }
             });
            // Capture position and set to the TextViews
            txtrank.setText(rank.get(position));
            txtcountry.setText(country.get(position));
            txtpopulation.setText(population.get(position));
    
            // Locate the ImageView in viewpager_item.xml
            imgflag = (ImageView) itemView.findViewById(R.id.flag);
            // Capture position and set to the ImageView
            imgflag.setImageResource(flag.get(position));
    
            // Add viewpager_item.xml to ViewPager
            ((ViewPager) container).addView(itemView);
    
            return itemView;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // Remove viewpager_item.xml from ViewPager
            ((ViewPager) container).removeView((RelativeLayout) object);
    
        }
        @Override
        public int getItemPosition(Object object){
            return PagerAdapter.POSITION_NONE;
        }
    }
    

提交回复
热议问题