如何从viewpager和pageradapter中删除项目

时间:2014-09-28 01:39:29

标签: android android-viewpager android-pageradapter

我正在使用pageradapter和viewpager来显示imageview数组,textview数组。但是如何在按钮点击时删除分页器。下面代码中的代码,但我在xml onclick中添加了按钮应该删除页面。

http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/

 package com.androidbegin.viewpagertutorial;

    import android.content.Context;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

    public class ViewPagerAdapter extends PagerAdapter {
        // Declare Variables
        Context context;
        String[] rank;
        String[] country;
        String[] population;
        int[] flag;
        LayoutInflater inflater;

        public ViewPagerAdapter(Context context, String[] rank, String[] country,
                String[] population, int[] flag) {
            this.context = context;
            this.rank = rank;
            this.country = country;
            this.population = population;
            this.flag = flag;
        }

        @Override
        public int getCount() {
            return rank.length;
        }

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

        @Override
        public Object instantiateItem(final ViewGroup container, final int position) {

            // Declare Variables
            TextView txtrank;
            TextView txtcountry;
            TextView txtpopulation;
            ImageView imgflag;

            Button b;

            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);
            b=(Button)itemView.findViewById(R.id.button1);


            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {



                }
            });

            // Capture position and set to the TextViews
            txtrank.setText(rank[position]);
            txtcountry.setText(country[position]);
            txtpopulation.setText(population[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[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);

        }
    }

////////////////////

// Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);
        // Pass results to ViewPagerAdapter Class
        adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag);
        // Binds the Adapter to the ViewPager
        viewPager.setAdapter(adapter);

2 个答案:

答案 0 :(得分:12)

首先,您需要使用List,因为它会动态删除对象。 第二,你需要

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

强制适配器每次都创建新页面,而不是在内存页面中使用。

 public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    List<String> rank;
    List<String> country;
    List<String> population;
    List<Integer> flag = new ArrayList<Integer>();
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, String[] rank, String[] country,
            String[] population, int[] flag) {
        this.context = context;
        this.rank = new ArrayList<String>(Arrays.asList(rank));
        this.country = new ArrayList<String>(Arrays.asList(country));
        this.population = new ArrayList<String>(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;
    }
}

答案 1 :(得分:0)

“选定的答案”将很快击中表现

您确实需要实现Adapter的std::max_align_t方法,但应按预期覆盖它。

getItemPosition()方法告诉ViewPager在调用getItemPosition()时是否删除viewItem,重绘或不重绘viewItem。

基本上,调用adapter.notifyDataSetChanged()时将调用您的getItemPosition()方法的实现来回答一个简单的问题。

问题是“对于我们拥有的每个viewItem,位置是否都已更改?”

此问题有两个答案

  1. adapter.notifyDataSetChanged()
  2. PagerAdapter.POSITION_UNCHANGED //nothing changed save resources use cached viewitem

此问题确定天气ViewPager重新创建一个新的viewItem或使用已缓存的viewItem。也是根据此答案,决定删除一个被视为已删除或删除的ViewItem。

如果每次调用PagerAdapter.POSITION_NONE //we don't have that view any more recreate it or delete it.时ViewPager必须为项目列表中的每个数据项目创建一个新的viewitem。想象一下,如果您有数据库中成千上万个数据的列表;性能会受到打击。正是从adapter.notifyDataSetChanged()返回PagerAdapter.POSITION_NONE;的过程。您正在绕过关键的性能机制。

作为开发人员,您有责任弄清楚如何回答该问题。但是请记住,回答问题的方式会影响应用程序的性能。

我如何实现

在您的 PagerAdapter 中,getItemPosition()。实例化后,在视图上设置标签。给它起一个唯一的名字。我使用在该视图中显示的图像的名称。

instantiateItem(@NonNull ViewGroup container, int position)

然后覆盖public MyAdaptorPagerAdaptor(Context context, ArrayList<MyMedia> myMedias) { this.context = context; this.myMedias = myMedias; this.authCode = authCode; } public Object instantiateItem(@NonNull ViewGroup container, int position) { mageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); mageView.setTag(myMedia.getName()); //the most important part //....... do your stuff return imageView; }

该消息中的

getItemPosition(@NonNull Object object)返回super.getItemPosition(object);上方的第1个答案,这是默认行为。

但是我们要遍历数据项,以查看从PagerAdapter.POSITION_UNCHANGED //nothing changed save resources use cached viewitem作为对象传递给我们的viewItem是否仍然存在于适配器项数组之外。

我们对不存在的内容感兴趣。那就是我们返回答案2的时候。 getItemPosition(@NonNull Object object)

作为对象传递给我们的viewItem恰好是我们在PagerAdapter.POSITION_NONE方法中返回的对象。就我而言,它是public Object instantiateItem(@NonNull ViewGroup container, int position)

ImageView

最后一部分。现在,从片段活动中,只需从Adapter数组或ArrayList中删除数据,这些数据将作为数据源传递给适配器。

@Override
public int getItemPosition(@NonNull Object object) {
    int res = super.getItemPosition(object);
    ImageView imageView = (ImageView) object;
    final String tag = (String) imageView.getTag();

    // look for an item with our tag from our items list.
    MyMedia myMedia = null;
    for (MyMedia item : myMedias) {
        if (item.getName().equals(tag)) {
            myMedia = item;
            break;
        }
    }

    //if this item is null, then it does not exist in my items list
    // so I tell View pager I don't have that item.
    if (myMedia == null) {
        res = PagerAdapter.POSITION_NONE;
    }
    return res;
}