[英]How to change background color in multiple items row delete in ListView
I am selecting multiple item in listview for delete . 我在listview中选择多个项目进行删除。 I can delete multiple item . 我可以删除多个项目。 The code is as follows : 代码如下:
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;
}
});
But I want to change color of the rows that I have selected . 但是我想更改所选行的颜色。 Currently there are no color on selection item in the list . 当前列表中的选择项上没有颜色。
I have tried the following . 我尝试了以下方法。
Step no 1: write below line to to your listview item layout 步骤1:将以下行写到您的listview项目布局
android:background="?android:attr/activatedBackgroundIndicator"
Step no 2: write below line to style.xml file 步骤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>
Step no 3: Create muliple_selected_item.xml into Drawable folder and write below code. 步骤3:将muliple_selected_item.xml创建到Drawable文件夹中,并编写以下代码。
<?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>
But by this code , all itemsof the list are colored when I select any item of the listview . 但是通过此代码,当我选择列表视图中的任何项目时,列表中的所有项目都是彩色的。 I want to change background color to those items only which I have selected . 我想将背景颜色更改为仅我选择的那些项目。 How can I do this ? 我怎样才能做到这一点 ?
The listview layoput is as follows : 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>
The rows layout is as follows : 行布局如下:
<?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>
The muliple_selected_item.xml is as follows : . 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>
You need to set Multiple Select Choice mode to your ListView
您需要将“多重选择选择”模式设置为ListView
smsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
You need to create Drawable file for your ListView's
row Layout and use Drawable
as background in that layout. 您需要为ListView's
行布局创建Drawable文件,并在该布局中使用Drawable
作为背景。
Here is Drawable file: 这是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>
EDIT: 编辑:
remove changeBackgroundColor()
used in selectView()
and check if it works 删除changeBackgroundColor()
使用的selectView()
并检查其是否有效
You can refer this Selecting multiple items in ListView . 您可以在ListView中引用此“ 选择多个项目” 。 it might help you 它可能会帮助你
Just put the line below in your single row xml file(the xml file in which you define the define the layout for single row of listview):- 只需将下面的行放在您的单行xml文件(您在其中定义定义listview的布局的xml文件)中:-
android:background="?android:attr/activatedBackgroundIndicator"
And no need to edit any more xml files. 无需编辑更多的xml文件。 For more details, please visit the link below :- 有关更多详细信息,请访问下面的链接:-
Click here to go to link ...! 单击此处转到链接...!
Hope this helps :) 希望这可以帮助 :)
You can use drawables for that like below, 您可以像以下那样使用可绘制对象,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true"
android:drawable="@android:color/darker_gray" />
</selector>
and set it as background of your row layout, 并将其设置为行布局的背景,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_background_activated" >
</RelativeLayout>
You can Achieve it in this way:- 您可以通过以下方式实现它:
- First add property(variable) isSelected to SMSItem class with default Value false. 首先将属性(变量)添加到SMSItem类,默认值为false。
- on onItemCheckedStateChanged method Execution set value for variable isSelected. ononItemCheckedStateChanged方法onSelected变量的执行设置值。
- on getView method of Adapter you have to chech value for isSelected variable. 在Adapter的getView方法上,必须检查isSelected变量的值。 then make a decison base on it, if false then set default background and if true the set another background to perticular row. 然后根据它做出决定,如果为false,则设置默认背景,如果为true,则将另一个背景设置为垂直行。
- call notifyDataSetChanged() method on Adepter from onItemCheckedStateChanged. 从onItemCheckedStateChanged调用Adepter上的notifyDataSetChanged()方法。
on Your Request, Im posting Code:- 应您的要求,我发布代码:-
add this to onItemCheckedStateChanged method 将此添加到onItemCheckedStateChanged方法
SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
selecteditem.setSelection(checked);
notifydatasetChanged();
and manage getView yourself 自己管理getView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.