Reputation: 2402
I'm creating a homepage which has an action bar and Drawer Layout within the homepage fragment i have a grid view whose content is the same as the drawer layout so i'm wanting to reuse the onClickListener from the drawer layout for my GridView. However currently because my Grid view is in a fragment it cannot access the on click listener. when i make my listener static i can't then edit features of the ActionBar i.e.:title etc. So my question is how do i reuse onClick listeners from inside fragments?
heres my code so far(this works perfect for the drawer Layout but the Grid View inside the fragment it doesn't)
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
public void Logout(){
Global.Logout(this);
}
private void selectItem(int position) {
// update the main content by replacing fragments
if(position == 0){
Fragment fragment = new homePageFragment();
Bundle args = new Bundle();
args.putInt(homePageFragment.ARG_MENU_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}else if(position == 9){
Logout();
Intent Logout = new Intent(getApplicationContext(), Login.class);
startActivity(Logout);
}else{
Fragment fragment = new listViewFragment();
Bundle args = new Bundle();
args.putInt(listViewFragment.ARG_MENU_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
getActionBar().setTitle(menuTitles[position]);
}
// update selected item and title, then close the drawer
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(menuBgColours[position])));
menuList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(menuList);
}
public static class homePageFragment extends Fragment {
public static final String ARG_MENU_NUMBER = "menu_number";
public homePageFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lo_homepage_frame, container, false);
menuGrid = (GridView) rootView.findViewById(R.id.gridView1);
menuGrid.setAdapter(menuGridAdapter);
userImage = (ImageView) rootView.findViewById(R.id.userPhoto);
userImage.setImageBitmap(userPhoto);
TextView userInfoText = (TextView) rootView.findViewById(R.id.userInfo);
userInfoText.setText(userInfo);
userInfoText.setTypeface(gilSans);
menuGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
}
});
return rootView;
}
}
Upvotes: 0
Views: 208
Reputation: 12943
You could inject the DrawerItemClickListener
to the Fragment with a setter. Then the GridView and the ListView will use the same listener.
Have a field and setter in your Fragment for the listener:
private DrawerItemClickListener listener;
public void setClickListener(DrawerItemClickListener listener){
this.listener = listener;
}
Set this when you create the Fragment:
Fragment fragment = new homePageFragment();
fragment.setClickListener(this); // attach it like this
Bundle args = new Bundle();
args.putInt(homePageFragment.ARG_MENU_NUMBER, position);
fragment.setArguments(args);
Now set the listener to your GridView in your onCreateView():
...
menuGrid.setOnItemClickListener(listener);
...
The GridView and the ListView will now use the same listener. Be careful with your on click logic. Because the positions have to be the same to get the right Fragment.
Upvotes: 1