2012-03-24 87 views
0

我有一個列表的Android活動。每個項目都可以點擊,當發生這種情況時,新的佈局顯示在文本下方,並帶有一些按鈕。setSelection引起奇怪的行爲ListView

我想單擊時將所選項目設置到中心,因爲單擊最後一個時,菜單會丟失,您需要滾動,這非常容易混淆。

我的代碼保存了最後一個選定項目的位置,所以當另一個項目被點擊時,它可以隱藏該項目的編輯菜單。

當我使用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

可能想發表您的適配器 – dldnh 2012-03-24 16:43:19

+0

很好的建議,我已經編輯與適配器代碼的帖子。謝謝。 – 2012-03-24 18:17:08

+0

我懷疑你的行佈局(ListView行中的ListView)?!)。 – Luksprog 2012-03-24 18:40:53

回答

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); 
}  
//... 
+0

非常感謝,做了這個訣竅。 – 2012-04-10 03:59:34