ViewPager不会使用更新的内容更新片段

时间:2016-10-08 07:24:17

标签: android android-fragments android-viewpager

我痛苦了3天。我使用兼容性库中的View Pager。我已经成功地显示了几个视图。我的问题是,当我交换视图时,不要在其回收站视图中按片段更新数据。但是当我关闭我的应用程序然后打开片段显示更新内容由recycler view.i使用所有方法,如setnotifydatachanged()和更多,但viewpager中的Fragment不更新content.Thankx任何帮助     这是我的主要活动

 public class MainActivity extends AppCompatActivity {
 private ViewPager viewPager;
 private TabLayout tabLayout;
 @Override

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(new              MainAdapter(getSupportFragmentManager(),MainActivity.this));
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    addIconsToTab();

}
       private void addIconsToTab() {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
        int drwableId = -1;
        switch (i) {
            case 0:
                drwableId = R.drawable.home;
                break;
            case 1:
                drwableId = R.drawable.favourite;
                break;
            case 2:
                drwableId = R.drawable.watchlater;
                break;


        }
        tabLayout.getTabAt(i).setIcon(drwableId);
       }
    }
    }
   this is my main adapter 
   public class MainAdapter extends FragmentPagerAdapter {

  Context mContext;
   public MainAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext=context;
  }

  @Override
   public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new AllVideos();
        case 1:
            return new Favourite();
        case 2:
            return new WatchLater();

        default:
            return null;
     }
   }

@Override
 public int getCount() {
    return 3;
  }

  @Override
  public CharSequence getPageTitle(int position) {

    switch (position) {
        case 0:
            return mContext.getString(R.string.All_Videos);
        case 1:
            return mContext.getString(R.string.Favourite);
        case 2:
            return mContext.getString(R.string.Wtach_Later);
        default:
            return null;
    }
   }



   @Override
    public int getItemPosition(Object object) {

    return POSITION_NONE;
  }
 } 
 And this is my Fragment
 public class Favourite extends Fragment {



  private RecyclerView mRecyclerView;
  private FavouriteAdapter favouriteAdapter;



 RecyclerView.LayoutManager layoutManager;
 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup        container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_recyclerview_carpaccio,     container, false);
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState)      {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(getActivity());
    DatabaseHandler db = new DatabaseHandler(getActivity());
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);
    List<VideoInformationDataModel> videoinfo = db.getAllFavourite();
    favouriteAdapter = new FavouriteAdapter(getActivity(),videoinfo);
    mRecyclerView.setAdapter(favouriteAdapter);
    favouriteAdapter.notifyDataSetChanged();
              Toast.makeText(getActivity(),videoinfo.size()+"",Toast.LENGTH_SHORT).show();


   }

  }

 And this is fragmentAdapter
  public class FavouriteAdapter extends        RecyclerView.Adapter<FavouriteAdapter.MyViewHolder>
{
     private int[] maid = {R.id.deleteoption};
    Context context;
    private List<VideoInformationDataModel> videoInformationDataModels;
   String videoid1;
    private FavouriteAdapter favouriteAdapter;
   public class MyViewHolder extends RecyclerView.ViewHolder implements           View.OnClickListener {
    public TextView title;
    View root;
    protected ImageLoader img_uni_imageLoader;
    DatabaseHandler db = new DatabaseHandler(context);
    ImageView networkImage,options;
    TextView Duration,Videoname;
    public MyViewHolder(View view) {
        super(view);
        root = itemView.findViewById(R.id.videoImage);
        Duration= (TextView) itemView.findViewById(R.id.duration);
        Videoname= (TextView) itemView.findViewById(R.id.VideoName);
        networkImage= (ImageView) itemView. findViewById(R.id.imgNetwork);
        options= (ImageView) itemView.findViewById(R.id.deleteoption);
        options.setOnClickListener(this);

      } 


    @Override
    public void onClick(View view) {
        if (maid[0] == view.getId()) {

            PopupMenu popup = new PopupMenu(context, options);
            popup.getMenuInflater().inflate(R.menu.deleteoption,           popup.getMenu());
            popup.setOnMenuItemClickListener(new          PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    switch (menuItem.getItemId())
                    {
                        case R.id.delete:

                            int pos = getAdapterPosition () ;

                            videoid1=            videoInformationDataModels.get(pos).getVideoid();

                            db.deleteFavourite(videoid1);

                            videoInformationDataModels.remove(pos);
                            notifyItemRemoved(pos);

                            notifyItemRangeChanged(pos, getItemCount());


                            return true;


                    }
                    return false;
                }
            });
            popup.show();
        }


       }
    }


  public FavouriteAdapter(Context context, List<VideoInformationDataModel>       videoinformation) {

      this.videoInformationDataModels = videoinformation;
      this.context=context;


  }

  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.favouritesingleitem, parent, false);

    return new MyViewHolder(itemView);
   }

  @Override
  public void onBindViewHolder(MyViewHolder holder, final int position) {

         holder.Duration.setText(videoInformationDataModels.get(position).getVideoduratio   n());
         holder.Videoname.setText(videoInformationDataModels.get(position).getVideoinfo()    );
    holder.img_uni_imageLoader = ImageLoader.getInstance();
             .img_uni_imageLoader.init(ImageLoaderConfiguration.createDefault(context));
         holder.img_uni_imageLoader.displayImage(videoInformationDataModels.get(position)   .getVideoimg(), holder.networkImage);
    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, YouTube.class);
            Bundle extras = new Bundle();

            String Videtittle =     videoInformationDataModels.get(position).getVideoinfo();
            String videodes =      videoInformationDataModels.get(position).getFld_description();

            String duration =   videoInformationDataModels.get(position).getVideoduration();
            String videoid =    videoInformationDataModels.get(position).getVideoid();
              extras.putString("Videtittle", Videtittle);
            extras.putString("videodes", videodes);
            extras.putString("duration", duration);
            extras.putString("videoid", videoid);
            intent.putExtras(extras);
            context.startActivity(intent);

             }
          });

        }





  @Override
 public int getItemViewType(int position)
 {
    return position;
 }




 public int getItemCount() {
    return videoInformationDataModels.size();
 }
 }

2 个答案:

答案 0 :(得分:0)

我不太确定您面临的问题,但是您可以尝试通过FragmentStatePagerAdapter替换FragmentPagerAdapter(由MainAdapter扩展)。

答案 1 :(得分:0)

我没有清楚地理解你的问题是什么,这可能是错误实施的结果。但是,如果您尝试过favouriteAdapter.notifyDataSetChanged();,但仍然无法刷新。

您也可以移除视图,刷新视图并添加视图,看看是否有效。

           ViewGroup parent = (ViewGroup) viewPager.getParent(); // get the view parent
            if (parent != null) {
                parent.removeView(viewPager); // remove the view
                viewPager.getAdapter().notifyDataSetChanged(); // notify that the data has changed
                parent.addView(viewPager); // then, re-add the view
            }