所以基本上我在ViewPager
和TabLayout
中有两个标签,以便在标签视图中存储两个片段。
问题是,我在recycler view
中使用名片视图,该名片视图就像一个“赞”按钮。基本上,当用户单击该按钮时,它应该出现在下一个选项卡上-但不是。只有当用户走出整个页面并返回到该页面并选择该选项卡时,才会显示所选的名片视图。
我检查了数据库,它正在执行应有的操作,但是recycler view
不会更新。
这是我喜欢在卡片视图中显示内容的代码
public class HomeFragment extends Fragment {
private static final String TAG = "MSG: ";
private Toolbar toolbar;
public TabLayout tabLayout;
private ViewPager viewPager;
private Context context;
public View root;
public int[] tabsUnselected = {
R.drawable.unselected_notification,
R.drawable.unselected_chat,
R.drawable.unselected_issue,
R.drawable.unselected_verified
};
public int[] tabSelected = {
R.drawable.selected_notification,
R.drawable.selected_chat,
R.drawable.selected_issue,
R.drawable.selected_verified
};
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
context = container.getContext();
setupTabs(root);
tabLayout.getTabAt(0).setIcon(tabSelected[0]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position) {
case 0:
tabLayout.getTabAt(0).setIcon(tabSelected[0]);
break;
case 1:
tabLayout.getTabAt(1).setIcon(tabSelected[1]);
break;
case 2:
tabLayout.getTabAt(2).setIcon(tabSelected[2]);
break;
case 3:
tabLayout.getTabAt(3).setIcon(tabSelected[3]);
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position) {
case 0:
tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);
break;
case 1:
tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);
break;
case 2:
tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);
break;
case 3:
tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);
break;
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return root;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);
tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);
tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);
tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);
}
public void setupTabs(View view) {
toolbar = view.findViewById(R.id.toolbar);
viewPager = view.findViewById(R.id.viewpager);
tabLayout = view.findViewById(R.id.tabs);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tabLayout.setupWithViewPager(viewPager);
setupViewPager(viewPager);
setupTabIcons();
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new Notification(), "Notification");
adapter.addFragment(new Conversation(), "Conversation");
adapter.addFragment(new Issues(), "Issues");
adapter.addFragment(new Pinned(), "Pinned");
viewPager.setAdapter(adapter);
}
private void experiment() {
}
- 因此,当第一次打开该标签时,效果很好,并且填充了回收站视图,但是当单击某个视图时,我想转到另一个标签, 像下一个标签一样。
我的寻呼机适配器:
public class NotificationFeedAdapter extends RecyclerView.Adapter<NotificationFeedAdapter.ViewHolder> {
public List<NotificationFeed> notificationFeedList;
public Context context;
public NotificationFeedAdapter(List<NotificationFeed> notificationFeedList) {
this.notificationFeedList = notificationFeedList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_single_item, parent, false);
context = parent.getContext();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final String list = notificationFeedList.get(position).NotificationFeedID;
String body = notificationFeedList.get(position).getBody();
String userHandle = notificationFeedList.get(position).getUserHandle();
holder.descView.setText(body);
holder.userName.setText(userHandle);
holder.id = list;
}
@Override
public int getItemCount() {
return notificationFeedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View mView;
private TextView descView;
private TextView userName;
private String id;
private String TAG = "MSG: ";
private TextView mMore;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
itemView.setOnClickListener(this);
descView = mView.findViewById(R.id.body);
userName = mView.findViewById(R.id.user);
mMore = mView.findViewById(R.id.more);
}
@Override
public void onClick(View v) {
// Navigate to tab
}
}
}
我将在onclick
事件中添加什么,以使其进入下一个标签。
答案 0 :(得分:0)
使用所需的选项卡创建包含参数的listener
进行导航,将其传递到适配器,从Adapter
传递到ViewHolder
,并在HomeFragment
中听{{ 1}},然后使用
ViewPager