2011-08-07 13 views
1

ListItemonItemClickのビューを置き換えたいと思います。アダプタでListItemビューの変更ListViewで選択

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

これを行う方法は複数あります。私は境界線として機能するFrameを使ってアダプタの行レイアウトを囲み、アイテムが選択されている場合は、getView(....)内のFrameの背景形状を変更するだけです。

onClickListenerをアタッチすると、渡されたViewはFrameクラスになります。 v.findViewById(...)を使用して、アダプターの行レイアウト の他のビューを検索し、変更することができます。私はv.invalidate()を使用して、アダプタのデータを変更しなければその特定のアダプタ行を再描画させます。定義上、onClickListenerヒットを取得した場合、特定のアダプタビュー が表示され、膨らんでいるため、UIスレッド上にある限り、アダプタとは独立したビューを操作できます。

ListViewは可視またはほぼ可視のアダプタ行のみを膨張させるので、操作する前にフレームビューが表示されていることを確認する必要があります。そのために、ListView.getFirstVisiblePosition()およびListView.getLastVisiblePosition()を使用します。これは、現在表示されているアダプタ行を示します。ここで

は左のアイコンとアイコンの右にあるテキストの2つの行を持つアダプタ行レイアウトの例です:

<?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); 
     } 

}

関連する問題