2 个 TabItem 的一个用户列表

One List of Users for 2 TabItem

我有 ListUsers 和 2 个 TabItem。第一个TabItemMyUsers,第二个是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 中,然后使用 addOnTabSelectedListener 在您的 mTabLayout 上管理选项卡选择,您可以在其中选择要设置的列表你的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();
}

希望这就是您要找的。