出于某种原因,我不能以编程方式在列表视图上设置选中的行。它在setOnItemLongClickListener中正常工作。
屏幕旋转后,我试图在listview上调用setItemChecked()。根据日志检查正确的行但我没有看到更改。
如果我加载/刷新数据,我调用下面的setupList()方法。 如果purchaseOrderArrayList不为null,则在onCreate方法中也会调用此方法。
private void setupList() {
mPurchaseOrderListView.setAdapter(new PurchaseOrderListAdapter(getActivity(), purchaseOrdersArrayList));
if (mSelectedPurchaseOrderIndex != -1) {
//this is where it does not work :(
mPurchaseOrderListView.setItemChecked(mSelectedPurchaseOrderIndex, true);
Log.v(Constants.LOG, "number of selections = " + mPurchaseOrderListView.getCheckedItemCount());
Log.v(Constants.LOG, "id of selections = " + mPurchaseOrderListView.getCheckedItemPosition());
Log.v(Constants.LOG, "choice mode = " + mPurchaseOrderListView.getChoiceMode());
}
mPurchaseOrderListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
// if actionMode is null "not started"
if (mActionMode != null)
return false;
// Start the CAB
mActionMode = getActivity().startActionMode(mActionModeCallback);
mActionMode.setTag(purchaseOrdersArrayList.get(position));
mSelectedPurchaseOrderIndex = position;
view.setActivated(true);
return true;
}
});
}
这是我的listview(fragment_purchase_orders.xml)
的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/centered_background"
tools:context="com.posmanagement.mamobile.PurchaseOrderFragment">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/purchaseOrderListView"
android:choiceMode="singleChoice"
/>
这是行的布局(simple_row_layout.xml)
<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/simple_list_background"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Item Description"
android:id="@+id/label"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#191919"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="012548"
android:id="@+id/subLabel"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#646565"/>
这是行背景的布局(simple_list_background.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@color/yellow_color" android:state_selected="true"/>
<item android:drawable="@color/green_color" android:state_activated="true"/>
<item android:drawable="@color/white_color"/>
答案 0 :(得分:0)
而不是使用
view.setActivated(true)
我用过
mPurchaseOrderList.setItemChecked(position, true)
一切正常,我不再需要存储位置。因此我删除了mSelectedPurchaseOrderIndex
变量。 (注意我相信这是因为列表视图包含在一个片段中,如果列表视图在一个活动中我需要保存所选位置)