简体   繁体   中英

Add Icon in Navigation Drawer Manually

im confuse to add icon beside title on navigation drawer, anyone know how to do that? im following this lik for create some nav drawer here ,

this my code

FragmentDrawer.java

public class FragmentDrawer extends Fragment {

    private static String TAG = FragmentDrawer.class.getSimpleName();

    private RecyclerView recyclerView;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private NavigationDrawerAdapter adapter;
    private View containerView;
    private static String[] titles = null;
    private FragmentDrawerListener drawerListener;

    public FragmentDrawer() {

    }

    public void setDrawerListener(FragmentDrawerListener listener) {
        this.drawerListener = listener;
    }

    public static List<NavDrawerItem> getData() {
        List<NavDrawerItem> data = new ArrayList<>();



        // preparing navigation drawer items
        for (int i = 0; i < titles.length; i++) {
            NavDrawerItem navItem = new NavDrawerItem();
            navItem.setTitle(titles[i]);
            data.add(navItem);
        }
        return data;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // drawer labels
        titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflating view layout
        View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
        recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);

        adapter = new NavigationDrawerAdapter(getActivity(), getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                drawerListener.onDrawerItemSelected(view, position);
                mDrawerLayout.closeDrawer(containerView);
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

        return layout;
    }


    public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
        containerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                toolbar.setAlpha(1 - slideOffset / 2);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

    }

    public static interface ClickListener {
        public void onClick(View view, int position);

        public void onLongClick(View view, int position);
    }

    static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }


    }

    public interface FragmentDrawerListener {
        public void onDrawerItemSelected(View view, int position);
    }
}

string.xml

<string name="nav_item_home">Home</string>
<string name="nav_item_friends">Friends</string>
<string name="nav_item_notifications">Messages</string>
<string name="nav_item_calculator">BMI Calculator</string>
<string name="nav_item_listview">Test Json</string>
<string name="nav_item_testabs">Test Tabs</string>


<!-- navigation drawer item labels  -->
<string-array name="nav_drawer_labels">
    <item>@string/nav_item_home</item>
    <item>@string/nav_item_friends</item>
    <item>@string/nav_item_notifications</item>
    <item>@string/nav_item_calculator</item>
    <item>@string/nav_item_listview</item>
    <item>@string/nav_item_testabs</item>
</string-array>

<array name="nav_drawer_icons">
    <item>@drawable/arm1</item>
    <item>@drawable/abs2</item>
    <item>@drawable/back</item>
    <item>@drawable/leg</item>
    <item>@drawable/shoulder</item>
    <item>@drawable/achest</item>
</array>

MainActivity.java

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.nav_item_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.nav_item_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.nav_item_notifications);
            break;
        case 3:
            fragment = new CalculatorFragment();
            title = getString(R.string.nav_item_calculator);
            break;
        case 4:
            fragment = new MovieFragment();
            title = getString(R.string.nav_item_listview);
            break;
        case 5:
            fragment = new TabFragment();
            title = getString(R.string.nav_item_testabs);
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

NavDrawwerItem.java

public class NavDrawerItem {
private boolean showNotify;
private String title;
private int icon;

public NavDrawerItem() {

}

public NavDrawerItem(boolean showNotify, String title, int icon) {
    this.showNotify = showNotify;
    this.title = title;
    this.icon = icon;
}

public boolean isShowNotify() {
    return showNotify;
}

public void setShowNotify(boolean showNotify) {
    this.showNotify = showNotify;
}

public String getTitle() {
    return title;
}

public int getIcon(){
    return this.icon;
}

public void setTitle(String title) {
    this.title = title;
}

public void setIcon(int icon){
    this.icon = icon;
}

You need to add Imageview in your drawerlist xml,and make your adapter custom and set your icons,

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

  <ImageView
    android:id="@+id/drawer_item_icon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="10sp"
    android:src="@drawable/ic_launcher" />

  <TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:textSize="25sp"
    android:paddingTop="8sp"
    android:paddingBottom="8sp"
    android:paddingLeft="15sp" />

</LinearLayout>

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