简体   繁体   中英

How open fragment using navigation drawer onClickListener()

In my navigation drawer i use this kind of method to open an activity: edited

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    Fragment fragment = null;
                    @SuppressLint("ResourceAsColor") @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
Fragment fragment = null;
            FragmentManager fragmentManager = getFragmentManager();
            Bundle bundle = new Bundle();
                        switch (position) {

                        case 0:     

                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));  

                            break;

                        case 1:     

                            fragment = new TrimFragment();
                         fragment.setArguments(bundle);
                         fragmentManager.beginTransaction()
                         .replace(R.id.content_frame, fragment).commit();

                            break;

                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strg)));  

                            break;

                        case 3:     

                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strtwit)));  

                            break;
                        }

                    }
                });
            }

But what i want is open a fragment right now. Is it possible from this structure do it? Thanks

在适当情况下添加/替换要显示在容器中的片段

In the switch case, use this code. This is how if you want to replace an existing fragment with the one you click on the Navigation Drawer List.

// update the main content by replacing fragments

Fragment fragment = null;
            FragmentManager fragmentManager = getFragmentManager();
            Bundle bundle = new Bundle();
            switch (position) {
            case 0:

                fragment = new Dashboard_Fragment();
                break;
            case 1:

                fragment = new SearchCustomer_Fragment();
                //Request Origin
                bundle.putString("origin", "searchCustomer");
                fragment.setArguments(bundle);
fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

                break;
            case 2:

                fragment = new STB_Check_Fragment();
fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();
                break;


    }

You can show the fragment instead of activity:

See this snippet.

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());  //onclick listener

/* 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) {
        setCharacter(position);
    }
}

//here a new fragment is created and replaces the old one.
public void setCharacter(int position){
    Fragment fragment = new CharacterFragment();
    Bundle args = new Bundle();
    args.putInt(CharacterFragment.ARG_CHAR_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mCharacterTitles[position]);
    mDrawerLayout.closeDrawer(mLinearLayout);
}

@Override
public void setTitle(CharSequence title) {
    // TODO Auto-generated method stub
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * Fragment that appears in the "content_frame", shows a planet
 */
public static class CharacterFragment extends Fragment {
    public static final String ARG_CHAR_NUMBER = "character_number";

    public CharacterFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_character, container, false);
        int i = getArguments().getInt(ARG_CHAR_NUMBER);
        String charName = getResources().getStringArray(R.array.characters_array)[i];

        TextView charTextView = (TextView) rootView.findViewById(R.id.text);
        charTextView.setText(charName);
        getActivity().setTitle(charName);
        return rootView;
    }
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM