nitin tyagi
nitin tyagi

Reputation: 1186

ListView Highlight row item programmatically

I have listview for showing a navigation drawer and I have set a selector xml in listselector property of ListView. Now when I select the listview item then list item highlights. But I want to highlight an item programmatically. Code which I tried:

ListRow:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/navigation_list_selector">

                 >
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/drawer_section_height"
        android:gravity="center"
        android:layout_centerInParent="true"
          android:text="boy"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/navigation_text_color"
        android:textSize="@dimen/menu_text_size"/>
</RelativeLayout>

Selector XML:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/nav_list_selectorshape1"/>
        <item android:state_focused="true" android:drawable="@drawable/nav_list_selectorshape1" /> <!-- focused -->
         <item android:state_pressed="true" android:drawable="@drawable/nav_list_selectorshape1" /> <!-- pressed -->

        <item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> <!-- default -->
</selector>

ListFragment:

public class NavigationMenuFragment extends ListFragment  {


    private static final String TAG = NavigationMenuFragment.class.getName();
    public NavigationMenuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_nvigation_menu, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<String> listItems = new ArrayList<String>(Arrays.asList("SIGN OUT", "HOME", "MY DATA", "QW", "HISTORY", "SETTINGS"));
        NavigationListAdapter navigationListAdapter = new NavigationListAdapter(getActivity(),listItems);
        setListAdapter(navigationListAdapter);
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        android.support.v4.app.Fragment newContent = null;
        switch (position) {
            case 0:
                Log.d(TAG,"Click on Fragment = "+position);
              //  newContent = new ColorFragment(android.R.color.holo_red_dark);
                break;
            case 1:
                Log.d(TAG,"Click on Fragment = "+position);
             //   newContent = new ColorFragment(android.R.color.holo_green_dark);
                break;
            case 2:
                Log.d(TAG,"Click on Fragment = "+position);
              //  newContent = new ColorFragment(android.R.color.holo_blue_bright);
                break;
            case 3:
                Log.d(TAG,"Click on Fragment = "+position);
              //  newContent = new ColorFragment(android.R.color.white);
                break;
            case 4:
                Log.d(TAG,"Click on Fragment = "+position);
              //  newContent = new ColorFragment(android.R.color.black);
                break;
            case 5:
                Log.d(TAG,"Click on Fragment = "+position);
                //  newContent = new ColorFragment(android.R.color.white);
                break;
            case 6:
                Log.d(TAG,"Click on Fragment = "+position);
                //  newContent = new ColorFragment(android.R.color.black);
                break;
        }
      //  if (newContent != null) {
            if (getActivity() instanceof HomeActivity) {
                HomeActivity fca = (HomeActivity) getActivity();
                fca.switchContent(null);
            }
        // }
    }

    public void setHighlightedItem(int index){
                   ((NavigationListAdapter) getListAdapter()).setmSelectedItemIndex(index);
    }
}

Adapter:

public class NavigationListAdapter extends BaseAdapter {

    private List<String> mNavList;
    private LayoutInflater mlayoutInflater;
    private Context mContext;
    private int mSelectedItemIndex = 0;

    public NavigationListAdapter(Context context , List<String> navList){
        mNavList = navList;
        mContext = context;
        mlayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public int getCount() {
        return mNavList.size();
    }

    @Override
    public String getItem(int position) {
        return mNavList.get(position);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View view =convertView;
        ViewHolder viewholder;
        if(view == null){
           view =  mlayoutInflater.inflate(R.layout.navigation_list_row,null);
            viewholder = new ViewHolder();
            viewholder.label = (TextView)view.findViewById(R.id.label);
            view.setTag(viewholder);
        }else{
            viewholder = (ViewHolder)view.getTag();
        }
        viewholder.label.setText(getItem(i));

        if(mSelectedItemIndex != -1 && mSelectedItemIndex == position){
            view.setActivated(true);

         }
        return view;
    }

    class ViewHolder{
        TextView label;
    }

    public void setmSelectedItemIndex(int selectedItemIndex){
        mSelectedItemIndex = selectedItemIndex;
    }
}

TRY 1: setHighLight() method of ListFragment.

TRY :2 :

code inside getView of Adapter class:

/* if(mSelectedItemIndex != -1 && mSelectedItemIndex <= getCount()){
            view.setSelected(true);
            view.setFocusable(true);

         }*/

nothing works in my case.

Upvotes: 1

Views: 762

Answers (1)

Thomas R.
Thomas R.

Reputation: 8073

In your state drawable you need to use state_activated and on your view call the method setActivated(true) instead of view.setSelected(true).

Example for your state drawable:

    <item
      android:drawable="@color/color1" android:state_activated="true" android:state_focused="false" android:state_pressed="false"/>

    <item
      android:drawable="@color/color2" android:state_activated="true" android:state_focused="false" android:state_pressed="true"/>

Upvotes: 2

Related Questions