android使用onClickListeners进行多个视图

时间:2013-11-17 10:25:58

标签: android android-actionbar navigation-drawer

我正在创建一个主页,主页片段中有一个操作栏和抽屉布局我有一个网格视图,其内容与抽屉布局相同,所以我想重新使用抽屉布局中的onClickListener网格视图。但是目前因为我的网格视图位于片段中,所以无法访问on click侦听器。当我使我的监听器静态时,我无法编辑ActionBar的功能,即:标题等等。所以我的问题是如何从内部片段重用onClick监听器?

到目前为止我的代码(这适用于抽屉布局,但片段内部的网格视图不适用)

/* 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;
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使用setter将DrawerItemClickListener注入Fragment。然后GridView和ListView将使用相同的侦听器。

在片段中为听众设置字段和设置器:

private DrawerItemClickListener listener;

public void setClickListener(DrawerItemClickListener listener){
    this.listener = listener;
}  

创建片段时设置:

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);

现在在onCreateView()中将侦听器设置为GridView:

...
menuGrid.setOnItemClickListener(listener);
...

GridView和ListView现在将使用相同的侦听器。小心点击逻辑。因为位置必须相同才能获得正确的碎片。