我有List
的Users
和2 TabItem
。第一个TabItem
为MyUsers
,第二个为Favorites
。我使用foreach
循环对Users
进行排序。现在,我想要的是将Users
List
显示为MyUsers
,然后在循环后将排序后的Users
显示为Favorites
。
UserFragment.java
public class UserFragment extends Fragment implements MyUserRecyclerViewAdapter.OnItemListener {
@BindView(R.id.tabs)
TabLayout mTabLayout;
@BindView(R.id.tabItem)
TabItem mTabItemMyUsers;
@BindView(R.id.tabItem2)
TabItem mTabItemFavorites;
private UserApiService mApiService;
private List<User> mUsers;
private List<User> mFavorites;
private RecyclerView mRecyclerView;
private int mPosition;
public static UserFragment newInstance(int position) {
UserFragment userFragment = new UserFragment();
Bundle args = new Bundle();
args.putInt("position", position);
userFragment.setArguments(args);
return userFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApiService = DI.getUserApiService();
mPosition = getArguments().getInt("position", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_list, container, false);
Context context = view.getContext();
mRecyclerView = (RecyclerView) view;
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
initList();
isUserFavorite();
return view;
}
public void isUserFavorite() {
mFavorites = new ArrayList<>();
for (User user : mUsers) {
if (user.isFavorite()) {
mFavorites.add(user);
}
}
mRecyclerView.setAdapter(new MyUserRecyclerViewAdapter(mFavorites, this));
}
private void initList() {
mUser = mApiService.getUsers();
mRecyclerView.setAdapter(new MyUserRecyclerViewAdapter(mUsers, this));
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
如果我理解你的问题是对的。您将创建两个列表,一个是收到的无序用户列表,第二个是被标记为收藏夹的用户列表。然后,您想通过单击同一布局中RecyclerView
中两个选项卡之一来切换要显示在片段布局中单个TabLayout
中的列表。
您应该先填充列表,而不是立即将它们都设置到RecyclerView中,然后使用mTabLayout
在addOnTabSelectedListener
上管理选项卡选择,在其中可以选择要设置到recyclerview中的列表为您的列表定义了LayoutManager
,接下来最后使用适配器上的notifyDataSetChanged()
更新视图。
因此,保持大多数逻辑不变,您的代码将需要重构为类似以下内容:
...
@Override
public View onCreateView(La...) {
...
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
...
initList();
isUserFavorite();
initSelection();
return view;
}
public void isUserFavorite() {
mFavorites = new ArrayList<>();
for (User user : mUsers) {
if (user.isFavorite()) {
mFavorites.add(user);
}
}
}
private void initList() {
mUser = mApiService.getUsers();
}
public void initSelection() {
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// assuming users is at tab index 0 and favorites is at tab Index 1
if(tab.getPosition() == 1) {
selectFavorites();
} else {
selectUsers();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// if you wish to select the users tab at 0 by default
Objects.requireNonNull(mTabLayout.getTabAt(0)).select();
}
private void selectFavorites() {
// whichever adapter you were using to show favorites
MyUserRecyclerViewAdapter mFavAdapter = new MyUserRecyclerViewAdapter(mFavorites, context);
mRecyclerView.setAdapter(mFavAdapter);
mFavAdapter.notifyDataSetChanged();
}
private void selectUsers() {
// whichever adapter you were using to show all users
MyNeighbourRecyclerViewAdapter mUsersAdapter = new MyNeighbourRecyclerViewAdapter(mUsers, context);
mRecyclerView.setAdapter(mUsersAdapter);
mFavAdapter.notifyDataSetChanged();
}
希望这就是您想要的。