Android ListView行突出显示

时间:2012-12-12 15:32:20

标签: android android-listview listviewitem android-custom-view android-selector

我在网上搜索了这个问题,而没有找到解决问题的方法。我知道在其他几篇文章中对此进行了讨论,但没有一篇能帮我解决问题。

我已经实现了一个ListView,其中突出显示了所选项目。一切都运行得很好,当我选择一行它突出显示并在使用myListView.setSelection(索引)时高亮,但我有一个问题,我真的需要解决:当我点击布局中的任何其他视图(按钮,复选框,radiobutton ecc)ListView丢失选择。

这是列表视图的实现方式:

我已经扩展了标准ListView

public SelectableListView(Context context) {
        super(context);
        initStyle(null);
    }

    public SelectableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initStyle(attrs);
    }

    public SelectableListView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        initStyle(attrs);

    }

    private void initStyle(AttributeSet attrs) {

        this.setBackgroundResource(R.drawable.red_border_fat);
        this.setPadding(3, 3, 3, 3);

        this.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //without the following 2 lines the selection won't work 
                requestFocusFromTouch();//
                arg1.setSelected(true);
            }
        });

    }

    @Override
    public void setSelection(int position) {
            //without the following line the selection won't work 
        requestFocusFromTouch();
        super.setSelection(position);
    }

listview以非常简单的方式添加到布局中

<my.name.space.SelectableListView
        android:layout_width="wrap_content"
        android:layout_height="100dp" >
    </my.name.space.SelectableListView>

这是适配器

public static SimpleAdapter GetClassAdapter(Context context,ArrayList<SeatClass> items){
        try {
            final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
            String[] fields = new String[] { "className" };
            int[] views = new int[] { R.skin_class.lblClassDescription};
            SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.skin_class_list, fields, views);
            for (SeatClass sc : items) {
                HashMap<String, String> temp = new HashMap<String, String>();

                temp.put("className", Typologic.getClasse(sc.Id).Description);

                list.add(temp);

            }
            return adapter;
        } catch (Exception e) {
            LogHelper.WriteLogError("error in GetClassAdapter", e);
            return null;
        }

    }

这是每行的布局(R.layout.skin_class_list)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+skin_class/mainview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/selectable_listview_background_selector"
    android:orientation="horizontal">

    <TextView
        android:id="@+skin_class/lblClassDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DESCRIPTION"
        android:textColor="@drawable/selectable_listview_textcolor_selector" />

</LinearLayout>

selectable_listview_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_selected="true" android:state_focused="true" android:drawable="@color/black" />
   <item android:state_selected="true" android:state_focused="false" android:drawable="@color/black" />
    <item android:state_pressed="true" android:state_focused="true" android:drawable="@color/black" />
    <item android:state_pressed="true" android:state_focused="false" android:drawable="@color/black" />

    <item android:drawable="@color/White" />

</selector>

selectable_listview_textcolor_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_selected="true" android:color="@color/White" />:
    <item android:state_pressed="true" android:color="@color/White" />:
    <item android:color="@color/black" />  

</selector>

正如我所说,这完美无缺,但是当我点击任何其他视图时,我无法保持亮点。我搜索了几天寻找解决方案,但似乎没有任何效果。

编辑: 我找到了这个解决方案,我不再使用选择器了。这里的MyLabel是一个自定义TextView,其中有一些 - 在这种情况下无关 - 功能

public abstract class SelectableListView<T> extends ListView {

    protected ArrayList<T> _data;
    protected int _selectedIndex = 0;

    public SelectableListView(Context context) {
        super(context);
        initStyle(null);
    }

    public SelectableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initStyle(attrs);
    }

    public SelectableListView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        initStyle(attrs);

    }

    public abstract void loadData(ArrayList<T> data);

    public T getSelectedItem() {
        if (_selectedIndex < 0)
            return null;
        else
            return _data.get(_selectedIndex);
    }

    public T getItemAt(int position) {
        if (position < 0 || position > _data.size())
            return null;
        else
            return _data.get(position);
    }

    @Override
    public void setSelection(int position) {

        colorizeSelection(position);
        super.setSelection(position);
        _selectedIndex = position;
    }


    private void colorizeSelection(int index){
        try {
            for (int i = 0; i < this.getCount(); i++) {
                if(i==index)
                    this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Red));
                else
                    this.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.White));
                ViewGroup root=(ViewGroup) this.getChildAt(i);
                colorizeSelection(root,i==index);
            }
        } catch (Exception e) {
            LogHelper.WriteLogError("error in colorizeSelection function", e);
        }
    }

    //this function is called recursively to scan all childs of a given view
    private void colorizeSelection(ViewGroup v,boolean selected){
        for (int j = 0; j < v.getChildCount(); j++) {
            if(v.getChildAt(j) instanceof ViewGroup)
                colorizeSelection((ViewGroup)v.getChildAt(j),selected);
            else if (v.getChildAt(j) instanceof MyLabel) {
                if(selected)
                    ((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.White));
                else
                    ((MyLabel)v.getChildAt(j)).setTextColor(getResources().getColor(R.color.black));
                ((NtvLabel)v.getChildAt(j)).invalidate();
            }

        }
    }

    private void initStyle(AttributeSet attrs) {
        _selectedIndex = -1;
        this.setBackgroundResource(R.drawable.red_border_fat);
        this.setPadding(3, 3, 3, 3);

        this.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                colorizeSelection(arg2);
                _selectedIndex = arg2;

            }
        });

    }



}

我还在等待一个更好的方法的建议......无论如何这现在有效:)

1 个答案:

答案 0 :(得分:0)

当您点击其他视图时,如果取消选择该视图,请考虑编写代码以在onLostFocus中为ListView

选择所有视图