2011-08-07 71 views
1

我想替換ListItemonItemClick的視圖。然而,我所遇到的問題,重繪我這樣做的時候:在ListView中選擇更改ListItem視圖

public void onItemClick (AdapterView<?> parent, View view, int position, long id) { 
    hlistAdapter.selected = position; 
} 

然後在適配器:

public View getView (int position, View convertView, ViewGroup parent) { 
    View retval; 
    if (position == selected) 
     retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_list_item_selected, null); 
    else 
     retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_list_item, null); 

    TextView title = (TextView) retval.findViewById (R.id.location); 
    title.setText (dataObjects[position]); 

    return retval; 
} 

我應該怎麼辦呢?

回答

2

我確定有多種方法可以做到這一點。我用一個充當邊框的框架環繞適配器行佈局,然後在getView(....)中的框架上更改背景形狀(如果選中該項目)。

如果附加onClickListener,則傳入的View是Frame類。然後,您可以使用v.findViewById(...)在適配器行佈局 中查找任何其他視圖並對其進行修改。如果我不更改適配器數據,我只是使用v.invalidate()來重繪該特定適配器行。根據定義,如果你得到一個onClickListener命中,那個特定的適配器視圖 是可見和充滿的,所以你可以獨立於適配器操作View ......只要你在UI線程上。

ListView只是膨脹可見或即將變爲可見的適配器行,因此您必須在操作之前確保Frame視圖可見。爲此,您使用ListView.getFirstVisiblePosition()和ListView.getLastVisiblePosition(),它會告訴您哪些適配器行當前正在顯示。

下面是在左側和文字兩行圖標右側的圖標適配器行佈局的例子:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/AdapterRowAccounts_Border" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="3dip" 
    android:background="@drawable/shape_adapterrowborder"  
> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/AdapterRowAccounts_Content" 
    android:paddingTop="1dip" 
    android:background="@drawable/shape_listviewbackground" 
    > 
    <ImageView 
     android:id="@+id/AdapterRowAccounts_Icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:tag="ic_subdirfolder" 
     android:src="@drawable/ic_accountedit" 
     android:layout_marginRight="3dip" 
     android:scaleType="centerInside" 
    /> 
    <TextView 
     android:id="@+id/AdapterRowAccounts_Text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/AdapterRowAccounts_Icon" 
     android:textColor="#000" 
     android:text="test1" 
     android:textColorHighlight="#FFF" 
     android:singleLine="true" 
     android:ellipsize="middle" 
    /> 
    <TextView 
     android:id="@+id/AdapterRowAccounts_Text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/AdapterRowAccounts_Text1" 
     android:layout_alignLeft="@id/AdapterRowAccounts_Text1" 
     android:text="test2" 
     android:textColor="#000" 
     android:textColorHighlight="#FFF" 
     android:singleLine="true" 
     android:ellipsize="middle" 
     android:textSize="10dip" 
    /> 
</RelativeLayout> 
</FrameLayout> 

這是代碼的從getView一針(INT位置......)改變邊界。由於Frame是佈局中最外層的元素,因此getView()傳入的「v」屬於Frame類。

if (mPosition >= 0 && mPosition < this.getCount()) { 
     mBundle = this.getItem(mPosition); 
     // If it is the currently selected row, change the background 
     ImageView mIcon = (ImageView) v.findViewById(R.id.AdapterRowAccounts_Icon); 
     if (mPosition == mSelectedPosition) { 
      v.setBackgroundResource(R.drawable.shape_adapterrowborder); 
      mIcon.setImageResource(R.drawable.ic_accountedit); 
     } else { 
      v.setBackgroundResource(R.drawable.shape_adapterrow); 
      mIcon.setImageResource(R.drawable.ic_account); 
     } 

}