这应该是一个简单的问题。
我试图创建一个" if语句"或"切换声明"基于可绘制文件夹中的浮动操作按钮图像资源。
我在ActivityMain文件和activity_main.xml文件中编写了一个浮动操作按钮。
在ActivityMain中我只在tab.getPosition()== 2时显示我的fab。所以它只显示在5个片段中的1个中。在这1个片段中,我有两个标签。
下面是片段代码和我的具体问题。谢谢你的帮助。非常感谢。
public class PageMessageFragment extends Fragment {
private RecyclerView recyclerView;
private View view;
private TabLayout chat_tabs;
private SearchView search;
public FloatingActionButton fab;
// Random Model
private List<Friend> items_friend = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.page_fragment_message, container, false);
// activate fragment menu
setHasOptionsMenu(true);
// Random Model
List<Friend> items = Constant.getFriendsData(getActivity());
items_friend = items.subList(0, 4); // Actual friends list
fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); // Gets fab ID from activity_main.xml
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
chat_tabs = (TabLayout) view.findViewById(R.id.chat_tabs);
chat_tabs.addTab(chat_tabs.newTab().setText("MESSAGES"), true);
chat_tabs.addTab(chat_tabs.newTab().setText("GROUP"));
chat_tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition()==0){
showListMessages(); // Actual Friends List
fab.setImageResource(R.drawable.ic_create);
}else if(tab.getPosition()==1){
showListGroups();
fab.setImageResource(R.drawable.ic_add_group);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
showListMessages();
//fab.show();
return view;
}
/* This is one thing that I tried, but cant seem to get it to work correctly. The if statement doesnt work
* and always displays the snackbar. What Im trying to do is execute ActivitySelectFriend based on the
* fab.setImageResource file. Which changes depending on the tab Im in. So if Im in "Tab Messages" I want
* display the Activity. If Im on "Tab Group" I want to just display a snackbar for now. */
@Override
public void onViewCreated (final View view, Bundle savedInstanceState) {
final FloatingActionButton fab1 = (FloatingActionButton) getActivity().findViewById(R.id.fab); // Gets fab ID from activity_main.xml
/*
fab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fab1.getDrawable() == R.drawable.ic_create) {
Intent i = new Intent(v.getContext(), ActivitySelectFriend.class);
startActivity(i);
} else {
Snackbar.make(view, "Select Group", Snackbar.LENGTH_SHORT).show();
}
}
}); */
}