Skip to content Skip to sidebar Skip to footer

Listview Adapter And Focus State

I have some ListView. This is code of item view:

Solution 1:

My idea of doing this is to create a custom array adapter with an integer variable representing the color of your textview elements. In the getView method of your custom adapter you will set the color of your textViews. Something like this:

publicclassCustomAdapterextendsBaseAdapter {

private String[] data; //Your dataprivate Context context;
    privateinttextViewColor= Color.BLACK; //The color of your textViews with a default valuepublicCustomAdapter(Context _context, String[] _data) {
    context = _context;
    data = _data;
}

    publicvoidsetColor(int _color) {
        textViewColor = color;
    }

    //Use a viewholder if you havent already for more efficient listView.staticclassViewHolder {
    protected TextView textView;
}

@Overridepublic View getView(int position, View inView, ViewGroup parent) {

    Viewv= inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflaterinflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = newViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

          viewHolder.textview.setTextColor(textViewColor); //Set the color of your textViews//Do anything else you are supposed to...
   }

  ....
}

Then set a OnFocusChangeListener on your listView. In there, set the new color of your textViews. Something like this:

listView.setOnFocusChangeListener(newOnFocusChangeListener() {

                @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
                    CustomAdapteradapter= (CustomAdapter) listView.getAdapter();
                    adapter.setColor(Color.White); //set the new color of your textViews components
                    adapter.notifyDataSetChanged(); //Call this to update the listview

                }
            });

If you want to change only the color of the textView of the selected item in the listView you need to add the onFocusChangedListener to the listViewItem on the getView() method in the adapter. Something like this:

@Overridepublic View getView(int position, View inView, ViewGroup parent) {

    Viewv= inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflaterinflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = newViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }


          //set the onFocusChangedListener in the View item of the listView
          v.setOnFocusChangeListener(newOnFocusChangeListener() {

            @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
                TextViewtextView= (TextView) v.findViewbyId(R.id.your_textViewId);
                if(hasFocus) {
                      textview.setTextColor(Color.BLUE);
                }
                else {
                      textview.setTextColor(Color.BLACK);
                }

            }
        });

          //Do anything else you are supposed to...
   }

Solution 2:

This is real simple with stateful colors:

Create "color list" file. For example res/color/font_color_selector.xml

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:color="@android:color/holo_red_light" /><itemandroid:state_selected="true"android:color="@android:color/holo_blue_light" /><itemandroid:color="@android:color/white" /></selector>

Then in your layout: <TextView android:textColor="@color/font_color_selector" ... />

Post a Comment for "Listview Adapter And Focus State"