0

我正在使用这个Sliding Menu ,但是I'm not using ActionBarSherlock.

所以我的SlidingMenu是一个ListFragment填充的ArrayAdapter,如果我点击我的菜单 ListView 的一个项目,就会启动一个新的 Activity。

我的问题是如何在 ListView 中突出显示我当前选定的项目?

public class MenuListFragment extends ListFragment {

    public MenuListFragment(){
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.menu_list, null);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        SampleAdapter adapter = new SampleAdapter(getActivity());

        adapter.add(new MenuItem(0, "Tout voir", R.drawable.ic_launcher));
        adapter.add(new MenuItem(1, "A", R.drawable.ic_a));
        adapter.add(new MenuItem(2, "B", R.drawable.ic_b));
        adapter.add(new MenuItem(3, "C", R.drawable.ic_c));
        adapter.add(new MenuItem(4, "D", R.drawable.ic_d));
        adapter.add(new MenuItem(5, "E", R.drawable.ic_e));
        adapter.add(new MenuItem(6, "F", R.drawable.ic_f));
        adapter.add(new MenuItem(7, "G", R.drawable.ic_g));
        adapter.add(new MenuItem(8, "H", R.drawable.ic_h));
        adapter.add(new MenuItem(9, "I", R.drawable.ic_i));
        adapter.add(new MenuItem(10, "J", R.drawable.ic_j));
        adapter.add(new MenuItem(11, "K", R.drawable.ic_k));
        adapter.add(new MenuItem(12, "L", R.drawable.ic_l));

        setListAdapter(adapter);
    }

    private class MenuItem {
        public int id;
        public String tag;
        public int iconRes;
        //public boolean disableIcon = false;

        public MenuItem(int id, String tag, int iconRes) {
            this.id = id;
            this.tag = tag; 
            this.iconRes = iconRes;
        }

        /*public MenuItem(int id, String tag) {
            this.id = id;
            this.tag = tag; 
            this.disableIcon = true;
        }*/
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        int idItem = ((MenuItem) l.getItemAtPosition(position)).id;


        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.putExtra("id_signe", idItem);
        startActivity(intent);
    }   

    public class SampleAdapter extends ArrayAdapter<MenuItem> {

        public SampleAdapter(Context context) {
            super(context, 0);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            MenuItem mi = getItem(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
            }

            ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
            icon.setImageResource(mi.iconRes);

            TextView title = (TextView) convertView.findViewById(R.id.row_title);
            title.setText(mi.tag);

            return convertView;
        }

    }
}


public class BaseActivity extends SlidingFragmentActivity {
    private int mTitleRes;
    protected ListFragment mFrag;

    public BaseActivity(int titleRes) {
        mTitleRes = titleRes;
    }

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

        setTitle(mTitleRes);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        if (savedInstanceState == null) {
            FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
            mFrag = new MenuListFragment();
            t.replace(R.id.menu_frame, mFrag);
            t.commit();
        } else {
            mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
        }

        // customize the SlidingMenu
        SlidingMenu slidingMenu = getSlidingMenu();
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    toggle();
                    return true;
            }
            return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

谢谢

编辑 1:

这是我的 menu_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@color/red" android:state_pressed="true"/>
   <item android:drawable="@color/red" android:state_selected="true"/>
   <item android:drawable="@color/red" android:state_activated="true"/>
</selector>

还有我的slidingmenu_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@color/red" android:state_pressed="true"/>
   <item android:drawable="@color/red" android:state_selected="true"/>
   <item android:drawable="@color/red" android:state_activated="true"/>
</selector>

我的列表视图的行被突出显示,但只有当我按下时,当我释放背景颜色重置时。

4

2 回答 2

0

检查选定的答案....

在您的列表的列表项布局定义中使用下面...该定义是静态的,因此在您实现“OnClick()”时不需要任何内容

android:background="?android:attr/activatedBackgroundIndicator"
于 2013-11-11T15:26:55.027 回答
0
@Override
public void onListItemClick(ListView listview, View view, int position, long id) {

    for(int i=0;i<adapter.getCount();i++){
        LinearLayout slidingmenurow = (LinearLayout) listview.getChildAt(i).findViewById(R.id.slidingmenurow);
        slidingmenurow.setBackgroundResource(R.color.slingmenu_list_background);
    }
    View convertViewSelected = adapter.getView(position, view, listview);
    LinearLayout slidingmenurowSelected = (LinearLayout)convertViewSelected.findViewById(R.id.slidingmenurow);
    slidingmenurowSelected.setBackgroundResource(R.color.slingmenu_list_press);

    super.onListItemClick(listview, view, position, id);
}
于 2014-08-13T03:12:52.993 回答