setItemChecked() 视图无法更改
setItemChecked() view failing to change
出于某种原因,我无法以编程方式在列表视图上设置选中的行。它在 setOnItemLongClickListener 中正常工作。
屏幕旋转后,我试图在我的列表视图上调用 setItemChecked()。根据日志,检查了正确的行,但我没有看到更改。
如果我 load/refresh 数据,我调用下面的 setupList() 方法。
如果 purchaseOrderArrayList 不为空,也会在 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;
}
});
}
这是我的列表视图的布局 (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"/>
而不是使用
view.setActivated(true)
我用过
mPurchaseOrderList.setItemChecked(position, true)
一切正常,我不再需要存储位置。因此我删除了 mSelectedPurchaseOrderIndex
变量。 (注意我相信这是因为列表视图包含在片段中,如果列表视图在 activity 我确实需要保存所选位置)
出于某种原因,我无法以编程方式在列表视图上设置选中的行。它在 setOnItemLongClickListener 中正常工作。
屏幕旋转后,我试图在我的列表视图上调用 setItemChecked()。根据日志,检查了正确的行,但我没有看到更改。
如果我 load/refresh 数据,我调用下面的 setupList() 方法。 如果 purchaseOrderArrayList 不为空,也会在 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;
}
});
}
这是我的列表视图的布局 (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"/>
而不是使用
view.setActivated(true)
我用过
mPurchaseOrderList.setItemChecked(position, true)
一切正常,我不再需要存储位置。因此我删除了 mSelectedPurchaseOrderIndex
变量。 (注意我相信这是因为列表视图包含在片段中,如果列表视图在 activity 我确实需要保存所选位置)