Android RecyclerView适配器布局未显示图标
首先,这里有一个屏幕截图(很抱歉,画图编辑不好): 正如你可能已经注意到的,只有中间的一行有一个图标(绿色圆圈),然而,我正在设置所有行的图标,或者我认为我是。 图标可以是绿色或红色,第一行应该显示绿色图标,最后一行应该显示红色图标,但它们都没有显示任何内容,只有第二行,所以我的问题是,为什么只有一行正确显示图标,当然,我如何修复它 下面是我的适配器代码:Android RecyclerView适配器布局未显示图标,android,imageview,android-adapter,android-recyclerview,Android,Imageview,Android Adapter,Android Recyclerview,首先,这里有一个屏幕截图(很抱歉,画图编辑不好): 正如你可能已经注意到的,只有中间的一行有一个图标(绿色圆圈),然而,我正在设置所有行的图标,或者我认为我是。 图标可以是绿色或红色,第一行应该显示绿色图标,最后一行应该显示红色图标,但它们都没有显示任何内容,只有第二行,所以我的问题是,为什么只有一行正确显示图标,当然,我如何修复它 下面是我的适配器代码: //imports and stuff public class EstablishmentAdapter extends Re
//imports and stuff
public class EstablishmentAdapter extends RecyclerView.Adapter<EstablishmentViewHolder> {
private ArrayList<EstablishmentModel> establishmentList;
private Context context;
private boolean hasPermission;
private int selectedPosition = 0;
public EstablishmentAdapter(ArrayList<EstablishmentModel> establishmentList, Context context, boolean hasPermission) {
this.establishmentList = establishmentList;
this.context = context;
this.hasPermission = hasPermission;
}
@Override
public EstablishmentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
EstablishmentViewHolder viewHolder;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_adapter_establishment, parent, false);
viewHolder = new EstablishmentViewHolder(v, (byte)0, hasPermission);
return viewHolder;
}
@Override
public void onBindViewHolder(EstablishmentViewHolder holder, final int position) {
final EstablishmentModel item = establishmentList.get(position);
Log.d(((EstablishmentActivity)context).logTagDebug, "Position: "+ String.valueOf(position));
Log.d(((EstablishmentActivity)context).logTagDebug, "ViewHolder: "+ String.valueOf(holder.getAdapterPosition()));
setUpTextFieldsItemRow(holder, item);
new ImageDonwloaderTask(holder.ivIcon).execute(item.getIconUrl());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyItemChanged(selectedPosition);
selectedPosition = position;
notifyItemChanged(selectedPosition);
//Se a gente botar o full, fazer switch case aqui
setUpPayContractTypeAction(item);
}
});
}
private void setUpTextFieldsItemRow(EstablishmentViewHolder holder, EstablishmentModel item) {
holder.tvClosingTime.setText(item.getClosingTime());
holder.tvOpeningTime.setText(item.getOpeningTime());
holder.tvName.setText(item.getName());
holder.tvType.setText(item.getType());
if (item.isOpened()) {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_green);
} else {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_red);
}
}
private void setUpPayContractTypeAction(EstablishmentModel item) {
if(item.isOpened()) {
PaymentInfoViewModel p = new PaymentInfoViewModel();
p.setIdEstablishment(item.getIdEstablishment());
p.setNameEstablishment(item.getName());
Intent intent = new Intent(context, CardNumberActivity.class);
intent.putExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO, p);
context.startActivity(intent);
}
else {
EstablishmentActivity activity = ((EstablishmentActivity) context);
String title = String.format(context.getString(R.string.str_msg_error_closed_establishment_title),
item.getName());
String message = String.format(context.getString(R.string.str_msg_error_closed_establishment_msg),
item.getName(), item.getOpeningTime(), item.getClosingTime());
activity.buildAlert(title, message).show();
}
}
@Override
public int getItemCount() {
return establishmentList.size();
}
}
请注意linearlayout根中的match_父级和现在是linearlayout的相对布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_establishment_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="@dimen/margin_horizontal_small"
android:layout_marginLeft="@dimen/margin_horizontal_small"
android:layout_marginEnd="@dimen/margin_horizontal_small"
android:layout_marginRight="@dimen/margin_horizontal_small"
android:layout_marginTop="@dimen/margin_vertical_small">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_establishment_name"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_large"
android:text="Bar Alternativo"
android:textColor="@color/black">
</TextView>
<LinearLayout
android:layout_marginTop="@dimen/margin_vertical_small"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_establishment_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_tiny"
android:text="Pub / Bar"
android:textColor="@color/gray_60_percent">
</TextView>
<ImageView
android:id="@+id/iv_establishment_working_status_icon"
android:src="@drawable/img_dot_green"
android:layout_marginStart="@dimen/margin_horizontal_big"
android:layout_marginLeft="@dimen/margin_horizontal_big"
android:layout_toRightOf="@+id/tv_establishment_type"
android:layout_toEndOf="@+id/tv_establishment_type"
android:layout_width="17dp"
android:layout_height="17dp" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_establishment_opening_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:textColor="@color/gray_30_percent"
android:text="19:00"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_separator_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:text="@string/str_screen_txt_hyphen"
android:textColor="@color/gray_30_percent"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_closing_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:text="02:00"
android:textColor="@color/gray_30_percent"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_place_distance"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/margin_horizontal_normal"
android:layout_marginStart="@dimen/margin_horizontal_normal"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
>
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
您尚未在onBindViewHolder中的任何位置设置图像资源。直接在onBindViewHolder中设置图像资源,或将if条件移出方法setUpPayContractTypeAction 只需注意方法setUpPayContractTypeAction,它包含以下if条件,并且您在onClick事件中调用了方法setUpPayContractTypeAction,这意味着将在项目单击事件中设置图像资源
if (item.isOpened()) {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_green);
} else {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_red);
}
问题是您将图像视图的左侧与文本视图的右侧对齐
<RelativeLayout
android:layout_marginTop="@dimen/margin_vertical_small"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_establishment_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_tiny"
android:text="Pub / Bar"
android:textColor="@color/gray_60_percent"
>
</TextView>
<ImageView
android:id="@+id/iv_establishment_working_status_icon"
android:src="@drawable/img_dot_green"
android:layout_marginStart="@dimen/margin_horizontal_big"
android:layout_marginLeft="@dimen/margin_horizontal_big"
android:layout_toRightOf="@+id/tv_establishment_type"
android:layout_toEndOf="@+id/tv_establishment_type"
android:layout_width="17dp"
android:layout_height="17dp" />
</RelativeLayout>
随着文本的增长,您将看不到任何图像。尝试使用重力右侧的Linearlayout,并将textview的权重设置为1我认为是图像开头的水平边距太大造成了问题。请尝试删除边距并对齐到父级的右侧,然后重试
此外,如果要以编程方式使视图在if条件中的任何位置不可见,也必须实现else代码,否则适配器也会将该可见性状态复制到相邻的列表项。是否可以发布列表项的xml?是,请稍等-编辑:DoneMy首先想到的是包含
iv\u建立\工作\状态\图标的相对布局太短,无法显示图标。为了进行测试,请尝试将图标的高度和宽度设置为5dp或其他什么,而不是17dp,并查看它是否显示upI这样做了,只有第二行一直显示图标,看起来有什么东西在隐藏它。您没有完全发布holder类(至少从您实例化它的方式来看)。不管怎么说,很难说是怎么回事。你最好从小事做起,删除与有问题的ImageView相关的代码(任何代码),将其设置为基本可绘制(如你的应用程序图标),这样你就可以看到它,然后从那里开始,直到问题出现。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_establishment_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="@dimen/margin_horizontal_small"
android:layout_marginLeft="@dimen/margin_horizontal_small"
android:layout_marginEnd="@dimen/margin_horizontal_small"
android:layout_marginRight="@dimen/margin_horizontal_small"
android:layout_marginTop="@dimen/margin_vertical_small">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_establishment_name"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_large"
android:text="Bar Alternativo"
android:textColor="@color/black">
</TextView>
<LinearLayout
android:layout_marginTop="@dimen/margin_vertical_small"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_establishment_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_tiny"
android:text="Pub / Bar"
android:textColor="@color/gray_60_percent">
</TextView>
<ImageView
android:id="@+id/iv_establishment_working_status_icon"
android:src="@drawable/img_dot_green"
android:layout_marginStart="@dimen/margin_horizontal_big"
android:layout_marginLeft="@dimen/margin_horizontal_big"
android:layout_toRightOf="@+id/tv_establishment_type"
android:layout_toEndOf="@+id/tv_establishment_type"
android:layout_width="17dp"
android:layout_height="17dp" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_establishment_opening_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:textColor="@color/gray_30_percent"
android:text="19:00"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_separator_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:text="@string/str_screen_txt_hyphen"
android:textColor="@color/gray_30_percent"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_closing_time"
android:layout_marginTop="@dimen/margin_vertical_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
android:text="02:00"
android:textColor="@color/gray_30_percent"
>
</TextView>
<TextView
android:id="@+id/tv_establishment_place_distance"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_marginLeft="@dimen/margin_horizontal_normal"
android:layout_marginStart="@dimen/margin_horizontal_normal"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_very_tiny"
>
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
if (item.isOpened()) {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_green);
} else {
holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_red);
}
<RelativeLayout
android:layout_marginTop="@dimen/margin_vertical_small"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_establishment_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_tiny"
android:text="Pub / Bar"
android:textColor="@color/gray_60_percent"
>
</TextView>
<ImageView
android:id="@+id/iv_establishment_working_status_icon"
android:src="@drawable/img_dot_green"
android:layout_marginStart="@dimen/margin_horizontal_big"
android:layout_marginLeft="@dimen/margin_horizontal_big"
android:layout_toRightOf="@+id/tv_establishment_type"
android:layout_toEndOf="@+id/tv_establishment_type"
android:layout_width="17dp"
android:layout_height="17dp" />
</RelativeLayout>