Android-OnClickListener之后未调用notifyDataSetChanged

时间:2018-10-13 01:21:02

标签: android android-viewpager notifydatasetchanged

我需要刷新ArrayAdapter内的Viewpager。当用户单击一个按钮时,将更新传递给Viewpager的列表,并调用notifyDataSetChanged。

但是什么也没发生。请问如何制作?我已经尝试了2天了。请帮我。谢谢。 This image Shows a little more

public class MyAdapter extends extends ArrayAdapter<MyObject>{
   private ViewPager mViewPager;
   private ViewPagerAdapter mViewPagerAdapter;
//...

   public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

   final MyObject myObject =getItem(position);

//this part works
//viewpager loads the images
   mList.clear();
   mList.addAll(myObject.getListOfImgUri);
   mViewPagerAdapter = new 
   ViewPagerAdapter(mContext, mList); 
   mViewPager.setAdapter(mViewPagerAdapter);

   mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
   //this part never gets called
   mList.addAll(myObject.getSecondListOfImgUri());


        }
       });
 }

}

编辑:经过以下建议,这是获得的结果:

 mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

       //previously, this part was never  called

        mList.clear();
       mList.addAll(myObject.getSecondListOfImgUri());

       Log.d(tag, mList.get(0));//result: Logs new Image
       mViewPagerAdapter.notifyDataSetChanged()

            }
           });
     }

    }

而且,在ViewPager内部:

@NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {
            destroyItem(container, position,mViewGroup );
            mInflater = LayoutInflater.from(mContext);
            // mViewGroup = (ViewGroup) inflater.inflate(R.layout.view_item, container, false);

            mViewGroup = (ViewGroup) mInflater.inflate(R.layout.view_item,container, false);
            ImageView imageView = mViewGroup.findViewById(R.id.theImg);
            Glide.with(mContext).load(mListData.get(position)).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(imageView);
            container.addView(mViewGroup);
            Log.d(tag, "instantiateItem: " +mListData.get(0));//result: Logs new Image



            return mViewGroup;
        }

但是Viewpager内仅显示空白页面。

2 个答案:

答案 0 :(得分:0)

notifydatasetChanged()添加mViewPagerAdapter方法。当前MyAdapter正在使用notifydatasetchanged()方法。

答案 1 :(得分:0)

public class MyAdapter extends extends ArrayAdapter<MyObject>{
   private ViewPager mViewPager;
   private ViewPagerAdapter mViewPagerAdapter;
//...

   public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

   final MyObject myObject =getItem(position);

//this part works
//viewpager loads the images
   mList.clear();
   mList.addAll(myObject.getListOfImgUri);
   mViewPagerAdapter = new 
   ViewPagerAdapter(mContext, mList); 
   mViewPager.setAdapter(mViewPagerAdapter);

   mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
   //this part never gets called
   mList.addAll(myObjecy.getSecondListOfImgUri);

   **mViewPagerAdapter.notifyDataSetChanged();**

        }
       });
 }

}