Java 如何清除ListView选择?
TL;DR:您可以从(a)我的列表视图中选择一个选项。然后,你改变主意,在(b)我的编辑文本中键入一些内容。如何清除listview选择并仅显示edittext?(反之亦然) 我有一个带有选项列表视图和edittext的应用程序来创建自己的选项。我需要用户选择或创建一个选项,但不能同时选择两者。这是我的布局图: 每当用户从listview中选择一个选项时,我将其设置为绿色,将其设置为“selected”,如下所示:Java 如何清除ListView选择?,java,android,android-listview,android-xml,Java,Android,Android Listview,Android Xml,TL;DR:您可以从(a)我的列表视图中选择一个选项。然后,你改变主意,在(b)我的编辑文本中键入一些内容。如何清除listview选择并仅显示edittext?(反之亦然) 我有一个带有选项列表视图和edittext的应用程序来创建自己的选项。我需要用户选择或创建一个选项,但不能同时选择两者。这是我的布局图: 每当用户从listview中选择一个选项时,我将其设置为绿色,将其设置为“selected”,如下所示: <?xml version="1.0" encoding="utf-8"
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/colorPrimary"/>
<item
android:state_selected="false"
android:drawable="@color/windowBackground" />
</selector>
(这被设置为我的listview的背景)
问题:如果用户决定键入自己的选项,我想取消选择listview选项,因为他们只能有一个选项
e.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
for(int i=0; i<=5; i++){
listView.setItemChecked(i, false);
}
listView.clearChoices();
listView.requestLayout()
adapter.notifyDataSetChanged()
}
}
e.setOnEditorActionListener(新建TextView.OnEditorActionListener()
{
公共布尔onEditorAction(TextView v、int actionId、KeyEvent事件){
对于(int i=0;我的故事很短)
- ListView选择器(
android:listSelector
)旨在指示单击事件,但不是选定的项目
- 如果绘制了ListView选择器(在第一次单击之后),如果ListView中没有剧烈的更改,它将不会消失
- 因此,如果没有将状态应用于具有透明背景的可绘制文件作为列表视图选择器,则仅使用可绘制文件。不要对其使用纯彩色资源,不要混淆自己的意思
- 使用ListView选择模式(
android:choiceMode
)指示所选项目
- ListView通过在行的根视图上设置
android:state_activated
来告知选择了哪一行。为适配器提供相应的布局/视图,以正确表示所选项目
理论
好吧,ListView
中的内置选择乍一看是非常棘手的。但是有两个主要区别,你应该记住,以避免像这样混淆-列表视图选择器和选择模式
列表视图选择器
ListView选择器是一种可绘制资源,假定它表示单击列表项的事件。您可以通过XML属性或使用方法来指定它。我在文档中找不到它,但我的理解是,此资源不应该是纯彩色的,因为在绘制后,它不会消失,而不使用c视图中的更改(如设置适配器,这可能会导致出现一些小故障),因此只有在应用特定状态(例如)时,此类可绘制文件才可见。下面是一个可作为列表视图选择器使用的可绘制文件的简单示例
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@android:color/darker_gray" />
<item
android:drawable="@android:color/transparent" />
</selector>
您可以使用该方法以编程方式选择/取消选择行。如果希望ListView清除所有选定项,则可以使用该方法。您还可以使用以下方法系列检查选定项:,(对于单选模式),(对于多选模式)
结论
如果希望保持简单,请不要使用列表视图选择器指示所选项目
解决问题
选项1.脏修复-隐藏选择器
我们可以在需要时隐藏选择器drawable,并在稍后单击ListView项时显示它,而不是实际删除选择器、更改布局和实现健壮的方法:
public void hideListViewSelector() {
mListView.getSelector().setAlpha(0);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mListView.getSelector().getAlpha() == 0) {
mListView.getSelector().setAlpha(255);
}
}
使您的行反映激活状态
在您给适配器的注释中,如下所示:
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, text1, listOfThings);
此选项最接近我对“标准”适配器的理解
2.自定义适配器
您可以使用标准布局和大部分标准适配器,但获取项目视图的情况除外。只需引入一个匿名类并重写该方法,即可提供具有相应后台绘图的行视图:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="@android:color/holo_green_light" />
<item
android:drawable="@android:color/transparent" />
</selector>
final ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, listOfThings) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
if (convertView == null) {
view.setBackgroundResource(R.drawable.list_item_bg);
}
return view;
}
};
清除选择
之后,当单击行时,它们将反映所选状态,您可以通过调用clearChoices()
和result命令ListView重新绘制自身,轻松清除ListView的所选状态
这里有一个小小的注释,如果您希望在用户开始键入时取消选择项目,而不是在用户实际单击返回(完成)按钮时取消选择项目,则需要使用回调:
mEditText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mListView.getCheckedItemCount() > 0) {
mListView.clearChoices();
mListView.requestLayout();
}
}
@Override
public void afterTextChanged(Editable s) {}
});
希望它能有所帮助。我有一个很好的解决方案。将EditText添加到布局中,该布局在ListView中包含以下内容:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:choiceMode="singleChoice"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Comment"
android:layout_below="@id/list_view"
android:id="@+id/editText"
android:nextFocusUp="@id/editText"
android:nextFocusLeft="@id/editText"/>
</RelativeLayout>
希望它能与您一起工作:)在edittext侦听器中重新设置适配器对我来说很有用:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
listview.clearChoices();
listview.setAdapter(adapter);
Toast.makeText(getApplicationContext(),"Typing" + listview.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
我将选中的索引放在toast中,以检查该项是否被正确取消选中
希望这能奏效!!当您请求第二个数据集时,只需呼叫clear即可:
arrayAdapter!!.clear()
加载第一个数据集
用户选择一个元素,
此操作突出显示您的项目
无论出于何种原因,启动数据集的重新加载(因为edittext的值已更改),
此时,在适配器上调用clear()。
然后检索数据集,将其发送到arrayAdapter并
未选择任何人。
这是因为当您清除时,它也会清除选中的标志是e
aTextView
?您将如何键入它?不,e是edittext@Napster您可以显示您的edittext
布局吗?您是否调试以查看是否调用了此侦听器?@Napster侦听器肯定正在调用,我已将我的问题编辑为添加EditText
布局您使用的是自定义布局还是标准的ArrayAdapter
?非常感谢您的回答!这种方法很有意义,但是,我不知道如何将StateListDrawable
应用于我的适配器。我使用的是普通的ArrayAdapter
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/list_item_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
final ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.list_view_item, android.R.id.text1, listOfThings);
mEditText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mListView.getCheckedItemCount() > 0) {
mListView.clearChoices();
mListView.requestLayout();
}
}
@Override
public void afterTextChanged(Editable s) {}
});
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:choiceMode="singleChoice"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Comment"
android:layout_below="@id/list_view"
android:id="@+id/editText"
android:nextFocusUp="@id/editText"
android:nextFocusLeft="@id/editText"/>
</RelativeLayout>
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (canBeSelected) {
listView.setSelector(R.drawable.background);
listView.setSelected(true);
listView.setSelection(i);
} else {
if (!editText.isFocused()){
canBeSelected = true;
listView.setSelector(R.drawable.background);
listView.setSelected(true);
listView.setSelection(i);
}
}
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
canBeSelected = false;
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
listView.setSelector(transparentDrawable);
listView.clearChoices();
listView.setSelected(false);
return false;
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (editText.isFocused()){
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
listView.setSelector(transparentDrawable);
listView.clearChoices();
listView.setSelected(false);
canBeSelected = false;
}
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
listView.setSelector(transparentDrawable);
listView.clearChoices();
listView.setSelected(false);
canBeSelected = false;
}
@Override
public void afterTextChanged(Editable editable) {
if (editText.isFocused()) {
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
listView.setSelector(transparentDrawable);
listView.clearChoices();
listView.setSelected(false);
canBeSelected = false;
}
}
});
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
listview.clearChoices();
listview.setAdapter(adapter);
Toast.makeText(getApplicationContext(),"Typing" + listview.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
arrayAdapter!!.clear()