我有一个包含列表的Android活动。可以单击每个项目,当发生这种情况时,会在文本下方显示一个新布局,并带有一些按钮。
我想在点击时将所选项目设置为中心,因为当单击最后一项时,菜单会丢失,您需要滚动,这非常令人困惑。
我的代码会保存最后一个选定项目的位置,因此当点击另一个项目时,它可以隐藏该项目的编辑菜单。
当我使用setSelection时,这种行为会中断,看起来listView中的位置会发生变化,因为该逻辑不再起作用。所有选项仍然可见,不同的行正在响应而不是单击的一行,如果我单击所选的一行下方的行,则会抛出NullPointerException。
我试图以多种不同的方式做到这一点,存储视图而不是位置,使用scrollTo而不是setSelection等......但它似乎总是打破。这是有问题的代码,你们有什么想法吗?非常感谢。
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RelativeLayout itemView = (RelativeLayout) mListView.getChildAt(position);
LinearLayout showItemOptionsView = (LinearLayout) itemView.findViewById(R.id.itemOptionsView);
showItemOptionsView.setVisibility(View.VISIBLE);
// Sets previous to GONE
if (mPreviouslySelectedItemPosition != -1) {
itemView = (RelativeLayout) mListView.getChildAt(mPreviouslySelectedItemPosition);
showItemOptionsView = (LinearLayout) itemView.findViewById(R.id.itemOptionsView);
showItemOptionsView.setVisibility(View.GONE);
}
mListView.setSelection(position);
mPreviouslySelectedItemPosition = position;
}
这是布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/groceryListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp" />
<LinearLayout
android:id="@+id/addItemView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/groceryListName"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/itemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textAutoComplete" />
<Button
android:id="@+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:onClick="addItem"
android:text="@string/addItem" />
</LinearLayout>
<TextView
android:id="@+id/clickOnItemToEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/addItemView"
android:layout_marginBottom="5dp"
android:text="@string/clickOnItemToEdit"
android:textSize="10dp" />
<Button
android:id="@+id/shop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:onClick="startShoppingActivity"
android:text="@string/startShopping" />
<ListView
android:id="@+id/items"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/shop"
android:layout_below="@id/clickOnItemToEdit" />
</RelativeLayout>
这是行的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/showItemView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/itemId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TableRow
android:id="@+id/itemTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:stretchColumns="3" >
<Button
android:id="@+id/itemQuantityLess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="lessQuantityHandler"
android:text="@string/itemQuantityLess" />
<TextView
android:id="@+id/itemQuantity"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textSize="20sp" />
<Button
android:id="@+id/itemQuantityMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="moreQuantityHandler"
android:text="@string/itemQuantityMore" />
<TextView
android:id="@+id/itemProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:textSize="20sp" />
</TableRow>
<LinearLayout
android:id="@+id/itemOptionsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="@id/itemTable"
android:gravity="center|right"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="@+id/editItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:onClick="editItem"
android:text="@string/editItem" />
<Button
android:id="@+id/removeItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:onClick="removeItem"
android:text="@string/removeItem" />
</LinearLayout>
</RelativeLayout>
这是适配器的代码
@Override
public void bindView(View view, Context context, Cursor cursor) {
int idIndex = cursor.getColumnIndexOrThrow(ItemAndroidDAO._ID);
String id = cursor.getString(idIndex);
TextView idView = (TextView) view.findViewById(R.id.itemId);
idView.setText(id);
int productIdIndex = cursor.getColumnIndexOrThrow(ItemAndroidDAO.PRODUCT_ID_COLUMN);
String productId = cursor.getString(productIdIndex);
Product product = productFacade.findById(Long.parseLong(productId));
// The product must exist, otherwise it fails.
AppException.assertNotNull(product, "The related product cannot be null.");
TextView productIdView = (TextView) view.findViewById(R.id.itemProductName);
productIdView.setText(product.getName());
int quantityViewIndex = cursor.getColumnIndex(ItemAndroidDAO.QUANTITY_COLUMN);
int quantity = cursor.getInt(quantityViewIndex);
TextView quantityView = (TextView) view.findViewById(R.id.itemQuantity);
quantityView.setText(Integer.toString(quantity));
}
and the newView() (it's in a superclass)
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listView = (View) layoutInflater.inflate(getLayoutResource(), null);
bindView(listView, context, cursor);
return listView;
}
API级别为7(2.1)。
答案 0 :(得分:0)
我不知道我是否了解您的代码发生了什么(或者您正在尝试做什么),但您可以尝试其他方法。而不是尝试使用onItemClick()
方法中的选定行使您的适配器具有字段:
long selectedPosition = -1; // -1 no visible LinearLayout
然后在onItemClick()
中只会:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// setter method that will update the adapter's selectedPosition to hold the id of the row clicked
adapter.setSelectedPosition(id);
// notify the adapter that it need to update the data
// I don't know exactly what adapter you implement, the notifiyDataSetChanged() might not work(you'll have to make another cursor).
adapter.notifyDataSetChanged();
}
然后在你的适配器的bindView()
方法中,除了你做的之外,看看selectedPosition
是否等于游标行ID。如果是这种情况,请搜索LinearLayout
并将其显示为:
//...
long theId = cursor.getLong(cursor.getColumnIndex("_id"));
if (theId == selectedPosition) {
//this is the row that the user clicked, so show the extra stuff
LinearLayout ll = (LinearLayout) view.findViewById(R.id.itemOptionsView);
ll.setVisibility(View.VISIBLE);
} else {
// implement this so you don't end up with a recycled view.
LinearLayout ll = (LinearLayout) view.findViewById(R.id.itemOptionsView);
ll.setVisibility(View.GONE);
}
//...