我正在使用游标适配器填充列表视图,并且我希望允许用户长按任何项目并使用操作模式执行可用操作。我使用支持库作为最小sdk版本= 10
问题:当我长时间点击项目时,会显示操作模式但该项目未突出显示为已选中。
这是我的activity_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</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="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:background="@drawable/activated_background"
>
<TextView
android:id="@+id/item_id"
android:layout_width="40dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
<TextView
android:id="@+id/item_amount"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
/>
</LinearLayout>
我已将android:background="@drawable/activated_background"
用于行布局,并在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="@drawable/list_activated_holo" />
<item android:drawable="@android:color/transparent" />
</selector>
当我长按项目时,它不会突出显示。
这是我的活动类:
public class MainActivity extends ActionBarActivity {
ActionMode _actionMode =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.listView1);
SQLiteDatabase db = new ItemDbContract(getBaseContext()).getReadableDatabase();
String selection[] = {ProductTable._ID,ProductTable.TITLE,ProductTable.Price};
Cursor cursor = db.query(ProductTable.TABLE_NAME, selection, null, null, null, null, null);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ItemsListAdapter(getBaseContext(), cursor));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(_actionMode!=null)
{
listView.setItemChecked(arg2, true);
System.out.println("item position checked="+arg2);
}
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("long clicked on "+arg2);
listView.setItemChecked(arg2, true);
((ActionBarActivity)MainActivity.this).startSupportActionMode(actionModeCallback);
return true;
}
});
}
ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
// TODO Auto-generated method stub
_actionMode = arg0;
_actionMode.getMenuInflater().inflate(R.menu.item_journal_context_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
};
}
提前致谢
答案 0 :(得分:0)
最后通过维护自定义适配器中所选项目的列表并为所选项目设置不同的背景来解决我的问题。