Highlighting Selected item in menu-drawer/sliding

2019-03-16 11:52发布

Library Used:

  1. https://github.com/SimonVT/android-menudrawer
  2. https://github.com/JakeWharton/ActionBarSherlock
  3. https://github.com/JakeWharton/Android-ViewPagerIndicator

Question:

I have been trying to implement highlighting on the menudrawer similar to YouTube/Beautiful Widget app, but i have no clue to how is should approach the problem.

below i am giving a sample to my aprproche which i am not sure, if its the correct way to implement something like this:

This is a Menu drawer Adaptervew.click lisner i created:

 private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

            mPreviousView=mActiveView;
            mActiveView=view;
            mActivePosition = position;
         //   mDrawer.setActiveView(view, position);
            TextView txtview=(TextView)mActiveView;
            txtview.setBackgroundResource( R.drawable.abs__cab_background_top_holo_dark);
           // mDrawer.closeMenu();
        }
    };

So basically what i am trying to do here is to use a .9 image on the current selected view!

What i want to know is is there a more systematic or better approach to do the same!

What i want to achive Screen Shots below::

enter image description here enter image description here

3条回答
成全新的幸福
2楼-- · 2019-03-16 12:04

You can give it a try:

public View getView(int position, View convertView, ViewGroup parent) {
    view.setSelectionHandlerColorResource(Set your color here);
}
查看更多
虎瘦雄心在
3楼-- · 2019-03-16 12:15

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-03-16 12:19

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 ..
查看更多
登录 后发表回答