我的ListView
包含TextView
,每个TextView旁边有两个ImageViews
,它们最初设置为不可见。
现在,我希望click
上button
处理列表,并根据准备好的模式设置每个列表项的2个图像视图的可见性。我做了这个,但屏幕上没有显示更改。
这是我的项目列表xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listitems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/yes_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/no_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<TextView
android:id="@+id/paragraph_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/yes_image"
android:layout_toRightOf="@id/yes_image"
android:textColor="@color/paragraph_items_colro"
android:textSize="16sp"
android:textStyle="normal" />
</RelativeLayout>
这是我的主要活动xml文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textDirection="ltr"
tools:context=".MainActivity">
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@android:color/transparent"
android:divider="@color/colorAccent"
android:dividerHeight="1dp"
android:listSelector="#0f0"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@id/mylist">
<ImageButton
android:id="@+id/button_checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/checkout_9"
android:src="@drawable/checkout_9"
app:layout_constraintTop_toBottomOf="@id/mylist"
tools:layout_editor_absoluteX="106dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
这是按钮OnclickHandler:
ImageButton btn_check = findViewById(R.id.button_checkout);
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataItem resultDataItem;
ListView result_lv = findViewById(R.id.mylist);
ArrayAdapter<DataItem> listAdapter = (ArrayAdapter<DataItem>)
result_lv.getAdapter();
for (int i = 0; i < arrayAdapter.getCount(); i++) {
resultDataItem = (DataItem) arrayAdapter.getItem(i);
View listItem = arrayAdapter.getView(i, null, result_lv);
if (i == resultDataItem.getSortPosition() - 1) {
ImageView imv = listItem.findViewWithTag(i + 200);
int tag = (int) imv.getTag();
imv.setImageResource(R.drawable.yes);
imv.setVisibility(View.VISIBLE);
TextView tv = listItem.findViewWithTag(i + 100);
tv.setTextColor(Color.RED);
} else {
ImageView imv = listItem.findViewById(R.id.no_image);
imv.setImageResource(R.drawable.no);
imv.setVisibility(View.VISIBLE);
}
}
}
});