문제

홈페이지 조각 내에 작업 표시줄과 서랍 레이아웃이 있는 홈페이지를 만들고 있습니다. 내용이 서랍 레이아웃과 동일한 그리드 보기가 있으므로 내 GridView에 대한 서랍 레이아웃의 onClickListener를 재사용하고 싶습니다.그러나 현재 내 그리드 뷰는 조각에 있기 때문에 클릭 시 리스너에 액세스할 수 없습니다.리스너를 정적으로 만들면 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;
        }
    }
도움이 되었습니까?

해결책

당신은 주사 할 수 있습니다 DrawerItemClickListener setter를 사용하여 조각에 추가합니다.그런 다음 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는 동일한 리스너를 사용합니다.클릭 시 논리에 주의하세요.올바른 조각을 얻으려면 위치가 동일해야 하기 때문입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top