gpt4 book ai didi

android - Viewpager 中的多个 Recyclerview 问题

转载 作者:行者123 更新时间:2023-11-29 23:23:16 26 4
gpt4 key购买 nike

我有一个 Viewpager,里面有两个标签。选项卡是卡片和收藏夹卡片。我在两个选项卡中使用相同的 fragment 。该 fragment 包含一个我动态更新的 RecyclerView。我将元素一一添加到两个选项卡中。我首先将元素完全添加到第一个选项卡(卡片选项卡),完成后我将元素添加到第二个选项卡(收藏夹选项卡)。我的问题是当我将元素添加到第二个选项卡时,第一个选项卡内的 RecyclerView 也会更新。现在第一个选项卡 RecyclerView 中的元素是现有元素加上我在第二个选项卡中添加的元素的总和。我在 ViewPager 选项卡中设置了两个不同的 ArrayList。但是当 getItemCount() 在第一个选项卡 RecyclerView 中调用时,ArrayList 具有我在第二个选项卡中添加的元素。

我在两个选项卡中使用带有 RecyclerView 的相同 fragment 。当我想更新单个选项卡时,我在 ViewPager 上调用 notifyDatasetChanged()。所以每次 fragment 中的RecyclerView都会被重新初始化。

这是我的 Viewpager 适配器

public class ViewPagerWebCardsAdapter extends FragmentStatePagerAdapter {

private ArrayList<WebCardBaseResponseModel> webCardDetailList;
private ArrayList<WebCardBaseResponseModel> followingWebCardList;
private Context context;
private boolean isEmptyFavoriteWebcardList;
private boolean isEmptyWebcardsList;

public ViewPagerWebCardsAdapter(FragmentManager fm, ArrayList<WebCardBaseResponseModel> webCardDetailList,
ArrayList<WebCardBaseResponseModel> followingWebCardList,
Context context) {
super(fm);
this.webCardDetailList = webCardDetailList;
this.followingWebCardList = followingWebCardList;
this.context = context;
}

public void updateWebCardLists( ArrayList<WebCardBaseResponseModel> webCardDetailList,
ArrayList<WebCardBaseResponseModel> followingWebCardList) {
this.webCardDetailList = webCardDetailList;
this.followingWebCardList = followingWebCardList;
notifyDataSetChanged();
}

public void setEmptyFollowingCardsFragment() {
isEmptyFavoriteWebcardList = true;
notifyDataSetChanged();
}

public void setEmptyWebCardsFragment() {
isEmptyWebcardsList = true;
notifyDataSetChanged();
}

@Override
public Fragment getItem(int position) {
if(position == 0) {
WebCardListFragment webCardListFragment = new WebCardListFragment();
webCardListFragment.setWebCardDetailList(webCardDetailList);
webCardListFragment.setEmptyListFragment(isEmptyWebcardsList);
return webCardListFragment;
} else {
WebCardListFragment webCardListFragment = new WebCardListFragment();
webCardListFragment.setWebCardDetailList(followingWebCardList);
webCardListFragment.setEmptyListFragment(isEmptyFavoriteWebcardList);
webCardListFragment.setAsFavoritesList();
return webCardListFragment;
}
}

@Override
public int getCount() {
return Constants.NUMBER_TWO;
}

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

@Override
public CharSequence getPageTitle(int position) {
if(position == 0) {
return context.getString(R.string.webcards);
} else {
return context.getString(R.string.favorite);
}
}

这是我的卡片 fragment

public class WebCardListFragment extends Fragment {

@BindView(R.id.web_card_recycler_view)
RecyclerView mWebCardDetailRecyclerView;

@BindView(R.id.tv_description)
TextView tvDescription;

@BindView(R.id.progress_bar)
public ProgressBar progressBar;

private WebCardListAdapter mWebCardListAdapter;
ArrayList<WebCardBaseResponseModel> mWebCardDetailList;
WebCardActivity activity;
private boolean isFavorites;
private boolean isEmptyListFragment;

@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = (WebCardActivity) context;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public void setWebCardDetailList(ArrayList<WebCardBaseResponseModel> mWebCardDetailList) {
this.mWebCardDetailList = mWebCardDetailList;
}

public void setAsFavoritesList() {
this.isFavorites = true;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webcard_list, container, false);
ButterKnife.bind(this, view);
initView();

return view;
}

private void initView() {
if(isFavorites) {
tvDescription.setText(getString(R.string.select_webcard_to_share));
}

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity);
mWebCardDetailRecyclerView.setLayoutManager(mLayoutManager);
mWebCardDetailRecyclerView.setHasFixedSize(true);
mWebCardDetailRecyclerView.setItemAnimator(new DefaultItemAnimator());
if(mWebCardDetailList != null && mWebCardDetailList.size() > 0) {
progressBar.setVisibility(View.GONE);
tvDescription.setVisibility(View.VISIBLE);
mWebCardListAdapter = new WebCardListAdapter(activity, mWebCardDetailList, isFavorites);
}
if(isEmptyListFragment) {
progressBar.setVisibility(View.GONE);
tvDescription.setVisibility(View.VISIBLE);
tvDescription.setText(getString(R.string.no_webcards_to_list));
}
mWebCardDetailRecyclerView.setAdapter(mWebCardListAdapter);
}

public void setEmptyListFragment(boolean isEmptyList) {
isEmptyListFragment = isEmptyList;
}

最后,这是我在 fragment 中使用的RecyclerView适配器

public class WebCardListAdapter extends RecyclerView.Adapter<WebCardListAdapter.ViewHolder> {
private Context mContext;
private ArrayList<WebCardBaseResponseModel> mWebCardDetailList;
private boolean isFavorites;

public WebCardListAdapter(Context context, ArrayList<WebCardBaseResponseModel> webCardDetailList, boolean isFavorites) {
mContext = context;
mWebCardDetailList = webCardDetailList;
this.isFavorites = isFavorites;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_webcard_detail_layout, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
setListItemView(holder, position);
setListItemClickListener(holder);
}

@Override
public int getItemCount() {
return mWebCardDetailList.size();
}

@Override
public long getItemId(int position) {
return position;
}

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

class ViewHolder extends RecyclerView.ViewHolder {
private TextView mCardNameTextView;
private LinearLayout mWeCardLinearLayout;
@BindView(R.id.tv_phone)
TextView phone;

ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mCardNameTextView = (TextView) itemView.findViewById(R.id.webcard_name_textview);
mWeCardLinearLayout = (LinearLayout) itemView.findViewById(R.id.wed_card_title_linear_layout);
}
}

我希望两个选项卡中的 ListView 是独立的。当我将元素添加到第二个选项卡 RecyclerView 时,这不应反射(reflect)在第一个选项卡 RecyclerView 中。

最佳答案

基本区别

FragmentPagerAdapter

适用于有限(固定)数量的项目( fragment )。为什么?因为它永远不会在 FragmentManager 创建后从 FragmentManager 中删除它(除非该 Activity 已完成)。它只会从当前不可见的 fragment 中分离 View 。 onDestroyView() 将在您的 Fragment 无法访问时调用,稍后 onCreateView() 将在您返回此 Fragment 时调用。

FragmentStatePagerAdapter

FragmentStatePagerAdapter 更精通内存。一旦无法访问,它就会从 FragmentManager 中完全删除 Fragment 实例。已移除 fragment 的状态存储在 FragmentStatePagerAdapter 中。一旦您返回到现有项目并恢复状态,就会重新创建 Fragment 实例。此适配器适用于计数未知的列表或项目变化很大的列表。

所以FragmentPagerAdapter会解决这个问题

我在 View 寻呼机上调用了 notifyDatasetChanged()。因此,每次都会重新初始化 fragment 内的回收器 View 。

关于android - Viewpager 中的多个 Recyclerview 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53884978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com