如何从列表视图中获取项目,使其在Android中不使用回收视图?
我当前有一个按钮行,该行在我的项目\u exercise.xml中有条件地显示:如何从列表视图中获取项目,使其在Android中不使用回收视图?,android,android-listview,Android,Android Listview,我当前有一个按钮行,该行在我的项目\u exercise.xml中有条件地显示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item_exercise" android:layout_width="
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_exercise"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
>
<RelativeLayout
android:id="@+id/text_row"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/exercise_name_value"
style="@style/h5"
android:text="@string/no_exercise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/item_nav_image"
/>
<ImageView
android:id="@+id/item_nav_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="12dp"
android:src="@drawable/ic_action_expand"
android:contentDescription="@string/nav_expand_collapse"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/buttons_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text_row"
android:visibility="gone"
>
<ImageButton
android:id="@+id/exercise_rename_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_mic"
android:contentDescription="@string/rename"
android:layout_marginStart="10dp"
/>
<ImageButton
android:id="@+id/exercise_delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_discard"
android:contentDescription="@string/delete"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
</RelativeLayout>
我以为我的displayNavRightly()方法可以解决问题,但事实并非如此。我是否需要在对象中存储UI的“状态”?或者有更好的方法吗?最简单的方法是放弃循环使用视图,并每次膨胀一个新视图。但这对性能不好 总的来说,holder模式用于避免在视图相同时调用findviewbyd来查找视图。因此,您不应该在支架上显示rithmId和NavID 之后,您应该设置视图的状态。如果按钮Rows Visibly可以更改,则可以为每个视图设置该按钮。在您的情况下,看起来您希望隐藏或显示按钮。所以你的数据(在你的例子中是ActiveRithm)应该记住这一点,而不是你的持有者 因此,当您获取对象时,使用它来设置当前视图的当前状态
final ActiveRithm rithm = (ActiveRithm) getItem(position);
if (rithm.showingButtons) {
holder.buttonsRow.setVisibility(View.VISIBLE);
} else {
holder.buttonsRow.setVisibility(View.GONE);
}
然后在单击中,不要操纵持有者,而是操纵持有者视图和数据状态
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rithm.showingButtons = !rithm.showingButtons;
if (rithm.showingButtons) {
holder.buttonsRow.setVisibility(View.VISIBLE);
} else {
holder.buttonsRow.setVisibility(View.GONE);
}
}
});
谢谢我担心我将不得不这样做…为了UI的目的修改POJO让人感觉不舒服,有没有更好的模式可以这样做?你可以让适配器记住选定的索引。List selectedRows=新建ArrayList();然后在getView中检查它们是否被选中。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rithm.showingButtons = !rithm.showingButtons;
if (rithm.showingButtons) {
holder.buttonsRow.setVisibility(View.VISIBLE);
} else {
holder.buttonsRow.setVisibility(View.GONE);
}
}
});