0
我正在用光標適配器填充我的列表視圖,並且我想允許用戶長時間按任何項目並使用操作模式執行可用操作。我正在使用支持庫作爲我的最小sdk版本= 10hightlight多個選定項目
問題:當我長時間點擊該項目時,會顯示動作模式,但該項目未被選中爲高亮顯示。
這裏是我的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"
,它是在繪製文件夾定義爲:
<?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;
}
};
}
在此先感謝