Search code examples
androidandroid-listviewandroid-listfragment

Different background just for the selected list item


In my application, I want to have a different background to the item which is selected in the ListView. In fact I don't have a plain ListView, its ListFragment out of which I am getting my ListView through getListView() method. I applied the selector programmatically like:

ListView listview = getListView();
listview.setSelector(R.drawable.tablet_settings_list_selector);

And the list selector xml is like,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_focused="false"
    android:drawable="@drawable/tablet_menu_panel" />
<item android:state_pressed="true" android:state_focused="true"
    android:drawable="@drawable/tablet_menu_panel_ontap" />
<item android:state_activated="true"
    android:drawable="@drawable/tablet_selected_menu" />
</selector>

This is how my list should look like:

ListView with different background for selected item

As you can see, the selected list item has different background with an outward arrow to it. So how can I achieve this?


Solution

  • Finally I made a conclusion that you can not set an item selected for ever in a ListView or ListFragment. So for that, I wrote my custom BaseAdapter and solved my problem :) For people facing similar issue, here is my BaseAdapter, focus on the getView() method.

    public class ListFragmentListAdapter extends BaseAdapter {
    
    String[] items;
    Context _context = null;
    private TabletFragment _tabletFragmentChild;
    
    public ListFragmentListAdapter(Context context, TabletFragment tabletFragmentChild, String[] items) {
        this.items = items;
        this._context = context;
        this._tabletFragmentChild = tabletFragmentChild;
    }
    
        //---------------------MOST IMPORTATNT PART----------------------
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(_context).inflate(R.layout.tablet_listfragment_list_item, null);
        }
    
        TextView textview = (TextView) convertView.findViewById(R.id.tv_listfragment_list_item);
        textview.setText(items[position]);
    
        convertView.setBackgroundDrawable(null);
        textview.setTextColor(_context.getResources().getColor(R.color.white));
    
        if(position == _tabletFragmentChild.getSelectedItemIndex()){
            convertView.setBackgroundResource(R.drawable.tablet_selected1_menu);
            textview.setTextColor(Color.BLACK);
        }
        return convertView;
    }
        //------------------------END OF IMP PART0------------------------
    
    @Override
    public long getItemId(int position) {
    
        return position;
    }
    
    @Override
    public Object getItem(int position) {
        return items[position];
    }
    
    @Override
    public int getCount() {
    
        if (items == null)
            return 0;
    
        return items.length;
    }
    }