我尝试了什么:
listview_selector_focussed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#008000"
android:endColor="#00FF00"
android:angle="90" />
</shape>
listview_selector_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#800000"
android:endColor="#FF0000"
android:angle="90" />
</shape>
listview_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="@drawable/listview_selector_focused" />
<item
android:state_pressed="true"
android:drawable="@drawable/listview_selector_pressed" />
</selector>
listview布局:
<ListView
android:listSelector="@color/listview_selector"
/>
我已经尝试过这么多..但不幸的是,这件事情无法发挥作用
我想在点击时更改列表视图行颜色,当我点击另一行时,必须取消选择前一行并恢复原始状态
适配器类:
public class ListViewAdapter extends BaseAdapter {
Context ctx;
ArrayList<HashMap<String, String>> arraylist;
LayoutInflater inflater;
TextView tvA, tvB;
String a, b;
String out;
ListViewAdapter(Context ctx, ArrayList<HashMap<String, String>> arraylist) {
this.ctx = ctx;
this.arraylist = arraylist;
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public Object getItem(int position) {
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listitem, parent, false);
tvA = (TextView) itemView.findViewById(R.id.tvA);
tvB = (TextView) itemView.findViewById(R.id.tvB);
tvA.setText(arraylist.get(position).get("a"));
a = arraylist.get(position).get("a");
b = arraylist.get(position).get("b");
return itemView;
}
listview适配器布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal">
<TextView
android:padding="5dp"
android:id="@+id/tvA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minLines="2"
android:gravity="center_vertical"
android:text="Large Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0052a5"
android:textSize="@dimen/font_large" />
<TextView
android:padding="5dp"
android:id="@+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#0052a5" />
</LinearLayout>
答案 0 :(得分:0)
尝试类似我的代码
public class ReportActivity extends ActionBarActivity {
ListView listview;
Context mContext;
DatabaseHandler dbHandler;
ArrayList<ReportModel> mlist;
ReportAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
mContext=this;
listview=(ListView) findViewById(R.id.listView);
dbHandler=new DatabaseHandler(mContext);
mlist=dbHandler.getAllContacts();
Collections.reverse(mlist);
adapter = new ReportAdapter(mContext, R.layout.adapter_layout,
mlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
// Capture ListView item click
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// Capture total checked items
final int checkedCount = listview.getCheckedItemCount();
// Set the CAB title according to total checked items
mode.setTitle(checkedCount + " Selected");
// Calls toggleSelection method from ListViewAdapter Class
adapter.toggleSelection(position);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = adapter
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ReportModel selecteditem = adapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
adapter.remove(selecteditem);
dbHandler.deleteContact(selecteditem.getId());
}
}
// 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
adapter.removeSelection();
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
});
}
}
}
在菜单文件夹中创建xml文件activity_main.xml ........
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/delete"
android:title="delete"/>
</menu>
答案 1 :(得分:0)