android - 从 ListView 中删除项目的 listSelector 更新
问题描述
我创建了一个显示在“singleChoice”ListView 中的列表,具有定义的 android:listSelector。只要列表未更改,选择就可以正常工作。然后我想从列表中删除选定的项目,并将选择切换到下一个项目,或者切换到最后一个项目,以防在我删除最后一个项目之前。以下代码按预期删除项目,但我不知道删除后如何将选择器切换到下一个项目。
这是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview_1"
android:layout_width="match_parent"
android:layout_height="175dp"
android:choiceMode="singleChoice"
android:listSelector="#ffcccccc"
android:paddingTop="10dp" />
<Button
android:id="@+id/button_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listview_1"
android:enabled="false"
android:onClick="onClick_delete"
android:text="Delete"
android:textAllCaps="false" />
</RelativeLayout>
这是代码:
public class MainActivity extends AppCompatActivity {
ListView listView1;
android.widget.ArrayAdapter<String> ArrayAdapter1;
int iRow;
Button buttonDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonDelete = (Button) findViewById(R.id.button_delete);
ArrayAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
ArrayAdapter1.add(new String("row 1"));
ArrayAdapter1.add(new String("row 2"));
ArrayAdapter1.add(new String("row 3"));
ArrayAdapter1.add(new String("row 4"));
listView1 = (ListView) findViewById(R.id.listview_1);
listView1.setAdapter(ArrayAdapter1);
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
iRow = position;
buttonDelete.setEnabled(true);
}
};
listView1.setOnItemClickListener(listener);
}
public void onClick_delete(View v) {
int count = ArrayAdapter1.getCount();
if (count - 1 < iRow)
iRow = count - 1;
String row = ArrayAdapter1.getItem(iRow);
Toast.makeText(getApplicationContext(),
"Deleting: " + iRow + " - \"" + row + "\"", Toast.LENGTH_SHORT)
.show();
ArrayAdapter1.remove(row);
ArrayAdapter1.notifyDataSetChanged();
if( 0 == ArrayAdapter1.getCount())
buttonDelete.setEnabled(false);
}
}
我使用 Nexus 4 API 23 模拟器在 Android Studio 3.6.1 上开发。
我尝试使用 listView1.performItemClick 或 listView1.setItemChecked 强制切换选择器,但没有成功。我很感激任何提示。
解决方案
您必须使用自定义视图和自定义数据listview
来实现这一点,而不是简单的数组字符串,这样您就可以在其中存储行的每个状态。
顺便说一句,当您调用ArrayAdapter1.notifyDataSetChanged();
整个列表时,可能会再次重新加载,但listView1.setItemChecked
我ArrayAdapter1.notifyDataSetChanged();
无法向您保证这将起作用。
推荐阅读
- vba - 对于 70,000 行,我的宏需要大量时间来根据各种条件给出输出注释
- angular - Angular - 嵌套数组和表单构建器映射
- authentication - Web 应用程序的 PIN 码身份验证
- maven - pom.xml 中的版本属性动态
- javascript - GLTF 索引计数与缓冲区大小错误相同
- angular - 使用 Angular 访问 Sharepoint API CORS
- javascript - 每当我转到使用特定组件的路线时,react-loadable 都会给出 404 错误
- java - 关闭正在处理 Stream 的 ExecutorService
- php - 保护站点免受服务器/cPanel 渗透/注入
- python - 从 np.load.txt 绘制每第 n 行