gpt4 book ai didi

android - 如何在 ListView 中更改多个项目行删除中的背景颜色

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:35 24 4
gpt4 key购买 nike

我在 ListView 中选择多个项目进行删除。我可以删除多个项目。代码如下:

smsListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// Capture total checked items
final int checkedCount = smsListView.getCheckedItemCount();
// Set the CAB title according to total checked items
mode.setTitle(checkedCount + " Selected");
View v = smsListView.getChildAt(position
- smsListView.getFirstVisiblePosition());
// Calls toggleSelection method from ListViewAdapter Class
((SmsArrayAdapter) arrayAdapter).toggleSelection(position,v);

}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = ((SmsArrayAdapter) arrayAdapter)
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
// Remove selected items following the ids
arrayAdapter.remove(selecteditem);
}
}
// Close CAB
mode.finish();
return true;
default:
return false;
}
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
((SmsArrayAdapter) arrayAdapter).removeSelection();
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}


});

但是我想改变我选择的行的颜色。目前列表中的选择项没有颜色。

enter image description here

我试过以下方法。

第 1 步:将下行写入您的 ListView 项目布局

android:background="?android:attr/activatedBackgroundIndicator"

第 2 步:将下行写入 style.xml 文件

 <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="android:activatedBackgroundIndicator">@drawable/muliple_selected_item</item>
</style>

第 3 步:在 Drawable 文件夹中创建 muliple_selected_item.xml 并编写以下代码。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:state_checked="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:state_pressed="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:drawable="@android:color/transparent" />
</selector>

但是通过这段代码,当我选择 ListView 的任何项目时,列表的所有项目都被着色了。我只想将背景颜色更改为我选择的那些项目。我怎样才能做到这一点 ?

listview布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/smsItemContainerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|top"
android:text="SMS Inbox"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/unread_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-20dp"
android:background="@drawable/notification_bg"
android:gravity="center"
android:text="88"
android:textColor="@android:color/background_light"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:visibility="invisible" />



</RelativeLayout>

<ListView
android:id="@+id/SMSList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />


</LinearLayout>

行布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/muliple_selected_item" >>

<TextView
android:id="@+id/textView_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS From"
android:textAppearance="?android:attr/textAppearanceLarge" />



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="SMS : " />

<TextView
android:id="@+id/textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Time : " />

</LinearLayout>

</LinearLayout>

muliple_selected_item.xml 如下:.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:state_checked="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:state_pressed="true" android:drawable="@color/lvMultipleSelectionColor" />
<item android:drawable="@android:color/transparent" />
</selector>

最佳答案

您需要为您的 ListView 设置 Multiple Select Choice 模式

smsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

您需要为 ListView 的 行布局创建 Drawable 文件,并在该布局中使用 Drawable 作为背景。

这是可绘制文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/menuitem_press_background_color" android:state_activated="true" />
<item android:drawable="@color/white" />
</selector>

编辑:

删除 selectView() 中使用的 changeBackgroundColor() 并检查它是否有效

可以引用这个Selecting multiple items in ListView .可能对你有帮助

关于android - 如何在 ListView 中更改多个项目行删除中的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com