我有一个private _album: {src: string, caption: string, thumb: string}[] = // ..rest of the code
,里面有两个标签。选项卡是“卡”和“收藏夹”卡。我在两个标签中都使用了相同的片段。该片段包含一个Viewpager
,我会对其动态更新。我将元素逐个添加到两个选项卡。首先,我将元素完全添加到第二个选项卡(“最爱卡片”选项卡)中。我的问题是,当我将元素添加到第二个选项卡时,我在第一个选项卡中的RecyclerView
也得到了更新。现在,第一个选项卡RecyclerView
中的元素是现有元素加上我在第二个选项卡中添加的元素的总和。我在两个RecyclerView
标签中都设置了两个不同的ArrayList。但是当ViewPager
在第一个选项卡getItemCount()
中调用时,ArrayList具有我在第二个选项卡中添加的元素。
我在两个选项卡中都使用带有RecyclerView
的相同片段。当我想更新单个标签时,我在RecyclerView
上调用notifyDatasetChanged()
。因此,每次ViewPager
都在片段中重新初始化。
这是我的Viewpager适配器
RecyclerView
}
这是我的卡片片段
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);
}
}
}
最后,这是我在片段内部使用的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;
}
适配器
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
中。
答案 0 :(得分:1)
基本区别
FragmentPagerAdapter
适用于有限(固定)数量的项目(片段)。为什么?因为它一旦创建就永远不会从FragmentManager中删除片段实例(除非Activity完成)。它仅将视图与当前不可见的片段分离。一旦无法访问片段,onDestroyView()就会在您的片段上被调用,一旦您回到该片段,onCreateView()将被调用。
FragmentStatePagerAdapter
FragmentStatePagerAdapter对内存的了解更多。一旦无法到达Fragment实例,它将完全从FragmentManager中删除它们。删除的Fragments的状态存储在FragmentStatePagerAdapter内部。返回到现有项目后,将重新创建Fragment实例,并恢复状态。该适配器适用于计数未知的列表或项目变化很大的列表。
因此FragmentPagerAdapter将解决此问题
我在视图寻呼机上调用notifyDatasetChanged()。因此,每次碎片中的回收站视图都会重新初始化。
答案 1 :(得分:0)
从vew寻呼机适配器类中删除替代函数“ getItemPosition”。希望对您有所帮助。
答案 2 :(得分:0)
感谢您的回答,无论如何,此问题与Recyclerview
或Viewpager
无关,这是由于我对变量引用的误解而发生的。我在活动中的其他位置更改了ArrayList
。这会影响recyclerview
中的列表。