我有一个ListView定义如下:
<ListView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
ListView填充了许多像这样定义的项目行:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="8dp"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/picture"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:minHeight="48dp"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<ViewStub
android:id="@+id/controls_stub"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="@layout/quick_actions"
android:minHeight="48dp" />
</LinearLayout>
现在,使用自定义适配器填充此列表视图,该适配器需要两个字符串来填充必要的数据。我想要的是当用户点击listview项目时有一小排按钮膨胀,但当然,只有在该特定行下面,这是我用来尝试这个的代码:
private View.OnClickListener myCustomListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (stub == null) {
//get the reference to it
stub = (ViewStub) v.findViewById(R.id.controls_stub);
}
if (stub.getVisibility() != View.VISIBLE) {
//inflate and make visible if not already
stub.setVisibility(View.VISIBLE);
}
else if (stub.getVisibility() == View.VISIBLE) {
//if visible, then toggle visibility back to gone
stub.setVisibility(View.GONE);
}
}
};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHoldermHolder;
if (row == null) {
row = mInflater.inflate(R.layout.list_item, null);
mHolder = new ViewHolder();
mHolder.mName = (TextView) row.findViewById(android.R.id.text1);
mHolder.mPicture = (ImageView) row.findViewById(R.id.friends_picture);
row.setTag(mHolder);
}
else {
mHolder = (FriendListViewHolder) row.getTag();
}
mHolder.mName.setText(names[position]);
FacebookHelper.setProfileImage(row.getContext(), mIds[position], mHolder.mPicture);
row.setOnClickListener(myCustomListener);
return row;
}
我遇到的问题是,由于我正在给该行的存根充气,每一个(Gnexus案例中为10个)行,都有一个突出显示的项目 - 它应该只有一个。例如,如果有40个列表项,则当用户仅点击第一项时,第1行,第10行和第40行将使存根膨胀。
我如何仅为指定的行显示它,并且在重复使用同一行时不会影响更多视图?