목록이있는 Android 활동이 있습니다. 각 항목을 클릭 할 수 있으며,이 경우 새로운 레이아웃이 텍스트 아래에 일부 버튼과 함께 표시됩니다.ListView에서 setSelection이 이상한 동작을합니다.
마지막 항목을 클릭하면 메뉴가 손실되고 스크롤해야하므로 매우 혼란 스럽기 때문에 선택한 항목을 클릭하면 가운데로 설정하려고합니다.
내 코드는 마지막으로 선택한 항목의 위치를 저장하므로 다른 항목을 클릭하면 해당 항목의 편집 메뉴를 숨길 수 있습니다.
setSelection을 사용할 때이 동작이 중단됩니다. 논리가 더 이상 작동하지 않기 때문에 listView의 위치가 변경된 것처럼 보입니다. 모든 옵션이 계속 표시되고 다른 행이 클릭 한 행 대신에 반응하며 선택한 행 아래의 행을 클릭하면 NullPointerException이 발생합니다.
나는 여러 가지 방법으로 위치 대신보기를 저장하려고 시도했다. setSelection 대신 scrollTo를 사용했지만, 항상 깨지기 쉬운 것처럼 보인다. 이것은 문제의 코드입니다. 아이디어가 있습니까? 고마워. (
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>
이며, 이는이
@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 어댑터의 코드 행의 레이아웃
<?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>
입니다 2.1).
이 어댑터 – dldnh
좋은 제안을 게시 할 수 있습니다, 나는 어댑터 코드로 게시물을 편집했습니다. 감사. –
나는 그것이 당신의 행 레이아웃 ('ListView' 행에있는'ListView'?)인지 의심 스럽습니다. – Luksprog