我有一个带有ViewHolder模式的RecyclerView适配器。我的布局中有5个ImageView,我需要根据服务器的响应来控制可见性。我得到的响应是逗号分隔的字符串(例如:1,4,3)。在这种情况下,我必须显示第1,第4和第3张图像。
图像视图的布局是:
<LinearLayout
android:id="@+id/ll_call_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/text_exp"
android:visibility="visible">
<ImageView
android:id="@+id/equity_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/equity_icon"
android:layout_marginEnd="5dp"
android:visibility="gone"/>
<ImageView
android:id="@+id/futures_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/futures_icon"
android:layout_marginEnd="5dp"
android:visibility="gone"/>
<ImageView
android:id="@+id/options_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/options_icon"
android:layout_marginEnd="5dp"
android:visibility="gone"/>
<ImageView
android:id="@+id/commodity_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/commodity_icon"
android:layout_marginEnd="5dp"
android:visibility="gone"/>
<ImageView
android:id="@+id/currency_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/currency_icon"
android:layout_marginEnd="5dp"
android:visibility="gone"/>
</LinearLayout>
我正在设置图像的可见性如下
//analystList[position].getAnalystCallTypes() gives the comma seperated string(eg. 1, 3)
String[] splitCallType = analystList[position].getAnalystCallTypes().trim().split(",\\s+");
for (int i = 0; i<splitCallType.length; i++) {
setCallTypeBadge(holder, splitCallType[i]);
}
private void setCallTypeBadge(ViewHolder holder, String position) {
switch (position) {
case "1":
holder.equityBadge.setVisibility(View.VISIBLE);
break;
case "2":
holder.futuresBadge.setVisibility(View.VISIBLE);
break;
case "3":
holder.optionsBadge.setVisibility(View.VISIBLE);
break;
case "4":
holder.commodityBadge.setVisibility(View.VISIBLE);
break;
case "5":
holder.currencyBadge.setVisibility(View.VISIBLE);
break;
}
/*if (position.equalsIgnoreCase("1")){
holder.equityBadge.setVisibility(View.VISIBLE);
}
if (position.equalsIgnoreCase("2")){
holder.futuresBadge.setVisibility(View.VISIBLE);
}
if (position.equalsIgnoreCase("3")){
holder.optionsBadge.setVisibility(View.VISIBLE);
}
if (position.equalsIgnoreCase("4")){
holder.commodityBadge.setVisibility(View.VISIBLE);
}
if (position.equalsIgnoreCase("5")){
holder.currencyBadge.setVisibility(View.VISIBLE);
}*/
}
问题是它只设置响应中第一个元素的可见性。即如果字符串是1,2,3,它将1的可见性设置为可见但不是2和3。
答案 0 :(得分:0)
为什么LinearLayout的高度和宽度设置为20dp?根据布局方向,其中一个至少不应该是100dp / match_parent吗?