Hello hoping someone can see what the problem is. Trying to grab data from firebase database and show it in a using FirebaseRecyclerAdapter but it only shows the data after I click on the searchview in the actionbar. Just can't figure out why.
public class CustomerFragment extends Fragment{
private FirebaseRecyclerAdapter<Customer,CustomerViewHolder> customerAdapter;
final String TAG = "CustomerFrag: ";
private EventBus eventBus = EventBus.getDefault();
private DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
private DatabaseReference customerRef = rootRef.child("customers");
private RecyclerView rvCustomer;
public CustomerFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
((AppStart)getActivity()).getSupportActionBar().setSubtitle("Customer List");
setHasOptionsMenu(true);
Log.d(TAG," onCreate");
}
// Inflate the menu; this adds items to the action bar if it is present.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
menu.clear();
inflater.inflate(R.menu.toolbar_customermenu, menu);
MenuItem searchItem = menu.findItem(R.id.customerSearch);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Customer");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
Log.d(TAG," onCreateOptionsMenu");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_customer, container, false);
rvCustomer = (RecyclerView)rootView.findViewById(R.id.rvCustomer);
rvCustomer.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
rvCustomer.setLayoutManager(manager);
Log.d(TAG," onCreateView");
customerAdapter = new FirebaseRecyclerAdapter<Customer,CustomerViewHolder>(Customer.class, R.layout.customer_cardview, CustomerViewHolder.class, customerRef) {
#Override
protected void populateViewHolder(CustomerViewHolder viewHolder, final Customer customer, final int position) {
Log.d(TAG," populateViewHolder");
viewHolder.tvCustomerName.setText(customer.getCustomerName());
viewHolder.tvCustomerAddress.setText(customer.getCustomerAddress());
viewHolder.tvCustomerTel.setText(customer.getCustomerTel());
}
};
rvCustomer.setAdapter(customerAdapter);
return rootView;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
AppCompatActivity activity = (AppCompatActivity) getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
Log.d(TAG," onOptionsItemsSelected");
switch (item.getItemId()) {
case R.id.customerSearch:
Log.d(TAG,"Search icon pressed");
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
static class CustomerViewHolder extends RecyclerView.ViewHolder{
private TextView tvCustomerName;
private TextView tvCustomerAddress;
private TextView tvCustomerTel;
private CardView cvCustomer;
public CustomerViewHolder(View itemView) {
super(itemView);
tvCustomerName = (TextView)itemView.findViewById(R.id.tvCustomerName);
tvCustomerAddress = (TextView)itemView.findViewById(R.id.tvCustomerAddress);
tvCustomerTel = (TextView)itemView.findViewById(R.id.tvCustomerTel);
cvCustomer = (CardView)itemView.findViewById(R.id.cvCustomer);
Log.d("CustFragment"," CustomerViewHolder");
}
}
}
after days of hitting my head I figured it was the recyclerView layout height was not set to match_parent both the height and the width have to be match_parent.
Hope this helps someone.
Related
I am stucking this problem. I would to click item menu action_delete to delete all movie in Movie List. But I can't do this, even Whne I click item action_delete it can't show Toask Nofitication
This is my code
public class FavoriteFragment extends Fragment {
private RecyclerView rcv_fav;
private AdapterFavorite adapterFavorite;
private MovieViewModel movieViewModel;
private List<Detail> listItem;
private Toolbar toolbar;
private Menu menu;
private MenuItem deleteItem;
private ConstraintLayout layout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favourite, container, false);
toolbar = view.findViewById(R.id.toolbar_fav);
rcv_fav = view.findViewById(R.id.rcv_favorite);
layout = view.findViewById(R.id.layout_main_activity);
movieViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MovieViewModel.class);
adapterFavorite = new AdapterFavorite(getContext());
movieViewModel.getAllMovies().observe(this, new Observer<List<Detail>>() {
#Override
public void onChanged(List<Detail> details) {
adapterFavorite.setFavMovie(details);
listItem = details;
rcv_fav.setAdapter(adapterFavorite);
adapterFavorite.notifyDataSetChanged();
}
});
initView();
swipe();
return view;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
private void initView() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
rcv_fav.setLayoutManager(linearLayoutManager);
rcv_fav.setHasFixedSize(true);
toolbar.setTitle("Favorite List");
toolbar.setTitleTextColor(getResources().getColor(R.color.white));
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_favorite, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.action_delete) {
movieViewModel.deleteAllMovies();
adapterFavorite.notifyDataSetChanged();
Toast.makeText(getContext(), "Delete All Movies", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
I was trying to figure out, but I can't
Please help me fix this issue, thanks a lot!
Inside initView() method add this line,
setSupportActionBar(toolbar);
I have to implement action mode in fragment, but its gets nullpointer exception in action mode. I have tried so much but not gets any results, Please help me I am new in android. I have also tried startActionMode but not impact. I have wasted my time & searched lot of code not found any solution. Please help me
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
private ActionModeCallback actionModeCallback;
private ActionMode actionMode;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
final Button btnLoad = root.findViewById(R.id.load);
btnLoad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
enableActionMode(1, true );
}
});
return root;
}
public void enableActionMode(int position, boolean bool) {
int count = 4;
if(actionMode == null) {
actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(actionModeCallback);
}
if (count == 0) {
actionMode.finish();
actionMode = null;
} else {
actionMode.setTitle(String.valueOf(count));
actionMode.invalidate();
}
}
private class ActionModeCallback implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// getSupportActionBar().hide();
menu.findItem(R.id.action_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.d("API123", ""+item.getItemId());
switch (item.getItemId()) {
case R.id.action_delete:
// delete all the selected rows
//mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
//clear selections
mode.finish();
actionMode = null;
}
}
}
This is mycode, null pointer in actionmode. please help me i wasting my time. searching lot of, no result found
I have an activity that has two fragments. The activity has a custom toolbar and the remaining space on the screen is used by the fragments (one at a time). In this toolbar there is a SearchView.OnQueryTextListener but this search should always be available in the fragment 1 and for the fragment 2 if should be available only if it meets a criteria. So what I need is a way to check, everytime I switch from fragments which one if occupying the screen at the moment and if it's the fragment one I set the searchView as available and for the fragment 2 the searchView will be available only if it meets the criteria.
This is my Activity:
public class ActivityTeams extends BaseActivity implements SearchView.OnQueryTextListener {
public ActivityTeams(){
super(R.layout.activity_teams);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
token = SharedPrefsHelper.getString(getApplicationContext(), Constants.TOKEN);
listview = (ListView) findViewById(R.id.listview);
listview.setVisibility(View.INVISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InternsFragment(), getResources().getString(R.string.team_internal));
adapter.addFragment(new ExternsFragment(), getResources().getString(R.string.team_external));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
#Override
protected void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null) {
if (user_type == 1) {
toolbar.setTitle(R.string.collaborators_title);
} else if (user_type == 2) {
toolbar.setTitle(R.string.admin_title);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_back_black);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search_team, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
ImageView searchSubmit = (ImageView) searchView.findViewById (android.support.v7.appcompat.R.id.search_go_btn);
searchSubmit.setImageResource(R.drawable.ic_dots_w);
searchSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear query
searchView.setQuery("", false);
//Collapse the action view
searchView.onActionViewCollapsed();
//Collapse the search widget
searchMenuItem.collapseActionView();
listview.setVisibility(View.GONE);
viewPager.bringToFront();
appBarLayout.setVisibility(View.VISIBLE);
}
});
for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
textView.setTextColor(Color.BLACK);
textView.setHintTextColor(Color.BLACK);
}
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return true;
}
This is fragment 1:
public class InternsFragment extends Fragment{
public InternsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_interns, container, false);
return rootView;
}
And this is fragment 2:
public class ExternsFragment extends Fragment {
public ExternsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_externs, container, false);
return rootView;
}
searchMenuItem in my activity is the thing I want to make change the visibility depending on the fragment that it's visible.
Use a ViewPager.OnPageChangeListener and use InvalidateOptionsMenu() inside that like so:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
currentFragment = position // use a field to store position
invalidateOptionsMenu();
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
});
Then depending on position, setup your menu in onPrepareOptionsMenu
I need Add SearchBar in ActionBar handle with more Fragments like whatsApp
when Search in Tab chats get data and when Search in Tab Status get another data
How do This?This is My Code there is 3 tabs i need every tab there is SearchView
This Activity which there is on Fragments
public class ActivityForIncludeFragments extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_include_fragments);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
CategoryAdapter adapter = new CategoryAdapter(this,getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
invalidateOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItemAddData = menu.findItem(R.id.add_data);
MenuItem menuItemActionSearch = menu.findItem(R.id.action_search);
menuItemAddData.setVisible(false);
menuItemActionSearch.setVisible(false);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.add_stocking_warehouse:
Intent intent = new Intent(ActivityForIncludeFragments.this,StockingWarehouse.class);
startActivity(intent);
break;
case R.id.add_reportes:
Intent intentReport = new Intent(ActivityForIncludeFragments.this,TableDaliyMovmentes.class);
startActivity(intentReport);
break;
}
return super.onOptionsItemSelected(item);
}
}
and This Fragment there is the data
public class AddPremissionFragment extends Fragment implements
LoaderManager.LoaderCallbacks<ArrayList<ItemsStore>>
,SearchView.OnQueryTextListener {
public AddPremissionFragment() {
// Required empty public constructor
}
public static final String ID_PERMISSION = "id";
public static final String NAME_PERMISION = "namePErmission";
public static final String NOTES_PERMISSION = "notes";
public static final String DIALOG_PERMISSION = "dialogPermission";
FloatingActionButton fab_add_permission;
ListView mListView;
public static AdapterAddPermission adapterAddPermission;
TaskDbHelper dbHelper;
ArrayList<ItemsStore> itemsPermissions = new ArrayList<ItemsStore>();
private ProgressBar progressBarPermission;
//Identifier for the category dataloader;
public static final int PERMISSION_LOADER = 2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_add_premission, container, false);
TextView namePremission = (TextView)view.findViewById(R.id.ETTypeStore);
dbHelper = new TaskDbHelper(getActivity());
mListView = (ListView) view.findViewById(R.id.listViewAddPermission);
progressBarPermission =(ProgressBar)view.findViewById(R.id.progressBarPermission);
adapterAddPermission = new AdapterAddPermission(getContext(), itemsPermissions);
View emptyView = view.findViewById(R.id.empty_view_permission);
mListView.setEmptyView(emptyView);
mListView.setAdapter(adapterAddPermission);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getContext(), "Click :"+ position, Toast.LENGTH_SHORT).show();
ItemsStore itemSPermision = itemsPermissions.get(position);
Bundle bundle = new Bundle();
bundle.putInt(ID_PERMISSION, itemSPermision.getId());
bundle.putString(NAME_PERMISION, itemSPermision.getNamePermission());
bundle.putString(NOTES_PERMISSION, itemSPermision.getNotes());
// startActivity(intent);
// long id = cursor.getLong(cursor.getColumnIndex(TaskContract.TaskEntry._ID));
EditPermissionFragment f = new EditPermissionFragment();
f.setArguments(bundle);
f.show(getFragmentManager(),DIALOG_PERMISSION);}});
// namePremission.setText("Name Permission");
fab_add_permission = (FloatingActionButton)view.findViewById(R.id.fab_add_permission);
fab_add_permission.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new EditPermissionFragment().show(getFragmentManager(),DIALOG_PERMISSION);
}
});
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Kick off the loader
getActivity().getSupportLoaderManager().initLoader(PERMISSION_LOADER, null, this);
}
#Override
public Loader<ArrayList<ItemsStore>> onCreateLoader(int id, Bundle args) {
return new LoaderPErmission(getContext().getApplicationContext(),itemsPermissions,dbHelper);
}
#Override
public void onLoadFinished(Loader<ArrayList<ItemsStore>> loader, ArrayList<ItemsStore> data) {
progressBarPermission.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
adapterAddPermission.swapData(data);
}
#Override
public void onLoaderReset(Loader<ArrayList<ItemsStore>> loader) {
adapterAddPermission.swapData(Collections.<ItemsStore>emptyList());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
itemsPermissions = dbHelper.getAllItemsPermissionBySearch(newText);
if (itemsPermissions !=null){
adapterAddPermission.setFilter(itemsPermissions);
// getSupportLoaderManager().restartLoader(Daily_LOADER,null,this);
}
return false;
}
}
Please check which fragment is visible using this
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment != null && fragment.isVisible()) {
if (fragment instanceof ChatFragment) {
....
}
else { }
}
I want to implement search filter in Tab view Fragment with data from sqlite database ,after more that three days of being though this and this without success finally i came across this which is seems to be clean idea since am kind new to android development.
i tried to implement it but does not seems to work it means search menu item not inflating also i get no result, i do not have idea where do i get t wrong and there is no error at all, any idea on what is wrong and how to make it work.
here is the Fragment code
public class TabFragment3 extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
ArrayList<HashMap<String, String>> arrayList;
DBController controller = new DBController(getContext());
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab_fragment_3, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.usersList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
controller = new DBController(getContext());
arrayList = controller.getAllUsers();
adapter = new UsersAdapter(arrayList,getContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
arrayList = controller.getAllUsers();
// Do something when collapsed
adapter.setFilter(arrayList);
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
#Override
public boolean onQueryTextChange(String query) {
return false;
}
#Override
public boolean onQueryTextSubmit(String newText) {
// Here is where we are going to implement the filter logic
newText = newText.toLowerCase();
ArrayList<HashMap<String, String>> userlist = controller.getAllUsers();
ArrayList<HashMap<String, String>> newList= new ArrayList<>();
for(HashMap<String, String> entry : userlist){
String name2= entry.get("number").toLowerCase();
if(name2.contains(newText)){
newList.add(entry);
}}
adapter.setFilter(newList);
return true;
}
}
/// adapter class
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
Context ctx;
public UsersAdapter(ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
this.mDataSet = mDataSet;
this.ctx=ctx;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v,mDataSet,ctx);
return userViewHolder;
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.name_entry.setText(mDataSet.get(position).get("userId"));
holder.email_entry.setText(mDataSet.get(position).get("userName"));
holder.icon_entry.setText(""+mDataSet.get(position).get("number"));
}
#Override
public int getItemCount() {
return mDataSet.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView name_entry, email_entry, icon_entry;
Context ctx;
ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
public UserViewHolder(View itemView, ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
super(itemView);
this.mDataSet=mDataSet;
this.ctx= ctx;
cardView = (CardView) itemView.findViewById(R.id.user_layout);
name_entry = (TextView) itemView.findViewById(R.id.name_entry);
email_entry = (TextView) itemView.findViewById(R.id.email_entry);
icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
}
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void setFilter(ArrayList<HashMap<String, String>> newList){
mDataSet= new ArrayList<HashMap<String, String>>();
mDataSet.addAll(newList);
notifyDataSetChanged();
}
}
/// main
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener,SearchView.OnQueryTextListener {
private ViewPager viewPager;
PagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
public boolean onQueryTextSubmit(String query)
{ return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
Add setHasOptionsMenu(true) in your TabFragment3's onCreateView() method. It tells the host activity that your fragment has menu options that it wants to add.
Also add super.onCreateOptionsMenu(menu,inflater)..
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Inside the MainActivity's onOptionsItemSelected(MenuItem item) return false, as the Menu item action would be hanlded in TabFragment3's onOptionsItemSelected(MenuItem item).