Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在下次选择之前设置ListView选择颜色?_Android_Listview_Select_Background_Background Color - Fatal编程技术网

Android 如何在下次选择之前设置ListView选择颜色?

Android 如何在下次选择之前设置ListView选择颜色?,android,listview,select,background,background-color,Android,Listview,Select,Background,Background Color,我将此选择器作为列表视图的列表选择器。此外,在ListView中,我设置了 android:choiceMode="singleChoice" android:listSelector="@drawable/list_selector" android:background="@android:color/transparent" android:cacheColorHint="@android:color/transparent" 当用户触摸ListView的任何项目时,它会呈现适当的蓝色

我将此
选择器
作为
列表视图的
列表选择器
。此外,在ListView中,我设置了

android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent" 
当用户触摸
ListView
的任何项目时,它会呈现适当的蓝色,但一旦用户移开手指,它就会变回黑色。我希望所选项目保留蓝色,直到和除非用户触摸任何其他项目

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
     android:state_selected="false"
        android:state_pressed="false" 
        android:drawable="@color/grey" />
    <item android:state_pressed="true" 
        android:drawable="@color/blue" />
    <item android:state_selected="true"
     android:state_pressed="false" 
        android:drawable="@color/blue" />
    </selector>


我需要做什么更改?

onListItem中单击
,强制查看状态:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        v.setSelected(true);
}

您必须将
ListView
android:choiceMode
设置为
singleChoice
。此外,您应该将
android:listSelector
设置为您已经实现的选择器,并从
ListView
的项目中删除该选择器(或任何其他
android:background
drawable),使其背景透明

除此之外,您还应扩展选择器,以响应
android:state_checked
,并注册
OnItemClickListener
,如下所示:

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        listView.setItemChecked(position, true);
    }

});
listView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
setItemChecked(位置,true);
}
});
用于列表项的顶级视图组件需要实现界面
Checkable
。请参阅,以获取已可即时签出的类的列表。如果您只想显示文本
CheckedTextView
,则应为您执行此操作。否则,如果使用自定义视图,则必须自己实现此接口。

在代码中尝试此方法

StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {android.R.attr.state_pressed},
        getResources().getDrawable(R.drawable.pressed));
    states.addState(new int[] {android.R.attr.state_focused},
        getResources().getDrawable(R.drawable.focused));
    states.addState(new int[] { },
        getResources().getDrawable(R.drawable.normal));
    imageView.setImageDrawable(states);

它可能会帮助您……

在这种情况下,我认为您需要从
ArrayAdapter
类中管理它。 在这里,您必须记住上次单击的项目视图和位置

基本思想是
onListItemClick
将单击的位置和项目视图传递给适配器 我们将用您喜欢的颜色设置它的背景,并将最后选择的颜色更改为查看 背景色设置为默认颜色

 public class SampleAdapter extends ArrayAdapter<Object> {

    private int mSelection = 0;

    public SampleAdapter(Context context, int resource, int textViewResourceId,
            List<Object> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public void setSelection(int mSelection, View selectedItemView) {
        this.mSelection = mSelection;

        if (selectedItemView!= null && lastSelectedRow != null
                && selectedItemView!= lastSelectedRow) {
            lastSelectedRow
                    .setBackgroundResource(R.drawable.bg_normal);
            selectedItemView
                    .setBackgroundResource(R.drawable.bg_selected);
        }

        this.lastSelectedRow = selectedItemView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //Usual code logic here....

        if (mSelection == position) {
            mViewHolder.mRootView
                    .setBackgroundResource(R.drawable.bg_selected);
            lastSelectedRow = mViewHolder.mRootView; 
        } else {
            mViewHolder.mRootView
                    .setBackgroundResource(R.drawable.bg_normal);
        }


        return view;
    }

    private static class ViewHolder {
        TextView name;
        View mRootView;
    }
}

我为ListView的项目设置了无背景。在ListView中,我设置了android:choiceMode=“singleChoice”、android:listSelector=“@drawable/list_selector”、android:background=“@android:color/transparent”和android:cacheColorHint=“@android:color/transparent”。但它仍显示出同样的效果。这里我想提到的是,我在LinearLayout中给出了一个backgound图像,它保存了Listview。问题是什么?我扩展了我的解释。看一下第二部分。我不想显示任何复选框,只需突出显示listview项。不可能吗?是的,我知道了。这也是我所需要的,让它工作的唯一方法是实现一个
可检查的
组件,因为Android的默认小部件有复选框或类似的东西。在线性布局的情况下,有一个很好的例子,可以尝试添加一个只有android的项目:state_selected=“true”
public void onItemClick(AdapterView<?> arg0, View listItemView,
        int position, long id) {

    if(myAdapter != null )
    {
        myAdapter.setSelection(position,listItemView);
    }
}
myAdapter.setSelection(position,mListViwe.getChildAt(pos));
mListViwe.setSelectionFromTop(position, 0);