Highlighting Selected item in menu-drawer/sliding menu

梦想的初衷 提交于 2019-12-03 03:41:32
faysal

You can give it a try:

public View getView(int position, View convertView, ViewGroup parent) {
    view.setSelectionHandlerColorResource(Set your color here);
}

This was my solution to highlight the current menu item:

1) Expose the last select drawerPosition of the NavDrawer activity :

public class NavDrawerBaseActivity extends Activity {

    public static int LAST_DISPLAY_POSITION = 0;

    private void displayView(int position) {
        LAST_DISPLAY_POSITION = position;

        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new ProgressPageFragment();
                break;
            // ..

            default:
                break;
        }

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

        // .. update selected item and title, then close the drawer
    }

And then Step 2, in the adapter class, reference that value:

public class NavDrawerListAdapter extends BaseAdapter {
   // ..

    @Override
    public View getView(int position, View convertView, ViewGroup parent {

        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        if (position == NavDrawerActivity.LAST_DISPLAY_POSITION) {
            txtTitle.setTextColor(Color.WHITE);
        }
        else
           txtTitle.setTextColor(Color.BLACK);

        // .. etc ..

There would be several ways to tackle this. My favorite approach would be to put your ListView item TextView inside a FrameLayout. This FrameLayout can then have a foreground drawable housing your indicator. You can show/hide this indicator either programmatically or using a selector.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!