Note that there are some explanatory texts on larger screens.

plurals
  1. POSlidingMenu highlight selected item
    primarykey
    data
    text
    <p>I'm using <a href="https://github.com/jfeinstein10/SlidingMenu" rel="nofollow">this</a> library <code>Sliding Menu</code> but <code>I'm not using ActionBarSherlock</code>.</p> <p>So my <strong>SlidingMenu</strong> is a <strong>ListFragment</strong> populated with an <code>ArrayAdapter</code>, if I click on an item of my menu ListView a new Activity is started.</p> <p>My question is <strong>how can I highlight my current selected item in the ListView ?</strong></p> <pre><code>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&lt;MenuItem&gt; { 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; } } </code></pre> <p>Thanks</p> <p>EDIT 1 : </p> <p>Here is my menu_list.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@color/red" android:state_pressed="true"/&gt; &lt;item android:drawable="@color/red" android:state_selected="true"/&gt; &lt;item android:drawable="@color/red" android:state_activated="true"/&gt; &lt;/selector&gt; </code></pre> <p>And my slidingmenu_selector.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@color/red" android:state_pressed="true"/&gt; &lt;item android:drawable="@color/red" android:state_selected="true"/&gt; &lt;item android:drawable="@color/red" android:state_activated="true"/&gt; &lt;/selector&gt; </code></pre> <p>The row of my listview is highlighted but only when I press, when I release the background color reset.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload