RecyclerView适配器布局未显示图标

时间:2016-05-02 13:42:58

标签: android android-recyclerview imageview android-adapter recycler-adapter

首先,这里有一个截图(对不起油漆编辑感到遗憾):

enter image description here

你可能已经注意到,只有中间的线有一个图标(绿色圆圈),但是,我正在设置所有行的图标,或者我想我是。 图标可以是绿色或红色,第一行应显示绿色图标,最后一行显示红色图标,但没有一个显示任何内容,只显示第二行,所以我的问题是,为什么只有一行显示图标当然,我怎么能解决它?

这是我的适配器代码:

//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();
    }
}

这里是活动布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dl_menu"
    android:elevation="7dp"
    tools:context=".view.activity.EstablishmentActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_refresh_establishments"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_establishments"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </android.support.v7.widget.RecyclerView>

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_menu_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffffff">

    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.DrawerLayout>

项目行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    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>

        <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
            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>

修改 我尝试编辑图标的大小,它反映在所有行的大小,所以我想ImageView是在laoyut,但没有图像。

编辑2 红色和绿色点添加: enter image description here enter image description here

但是我不认为图像是问题,因为第一行应该显示绿点,但它不会。

编辑3 这是我的观察者:

public class EstablishmentViewHolder extends RecyclerView.ViewHolder {

    public ImageView ivIcon;
    public TextView tvName;
    public ImageView ivWorkingStatus;
    public TextView tvOpeningTime;
    public TextView tvClosingTime;
    public TextView tvPlaceDistance;
    public TextView tvType;

    public byte idContractType;

    public EstablishmentViewHolder(View itemView, byte idContractType {
        super(itemView);

        this.ivIcon = (ImageView) itemView.findViewById(R.id.iv_establishment_icon);
        this.tvName = (TextView) itemView.findViewById(R.id.tv_establishment_name);
        this.ivWorkingStatus = (ImageView) itemView.findViewById(R.id.iv_establishment_working_status_icon);
        this.tvOpeningTime = (TextView) itemView.findViewById(R.id.tv_establishment_opening_time);
        this.tvClosingTime = (TextView) itemView.findViewById(R.id.tv_establishment_closing_time);
        this.tvPlaceDistance = (TextView) itemView.findViewById(R.id.tv_establishment_place_distance);
        this.tvType = (TextView) itemView.findViewById(R.id.tv_establishment_type);
        this.idContractType = idContractType;
    }
}

4 个答案:

答案 0 :(得分:1)

请注意linearlayout根目录中的match_parent以及现在是线性布局的相对布局。

<?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>

答案 1 :(得分:1)

问题是您将图像视图的左侧对齐到文本视图的右侧!!!

<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

答案 2 :(得分:1)

我认为这是图片开头的margin_horizo​​ntal_big导致麻烦。请尝试删除边距并对齐父节点并尝试。

此外,如果您在if条件内的任何位置以编程方式使视图不可见,则还必须实现else代码,否则适配器也会将该可见性状态复制到相邻列表项。

答案 3 :(得分:0)

您尚未在onBindViewHolder中的任何位置设置图片资源。在onBindViewHolder中直接设置图像资源,或者将if条件移出方法setUpPayContractTypeAction

注意方法 setUpPayContractTypeAction ,其中包含以下if条件,并且您在 onClick 事件中调用了方法setUpPayContractTypeAction,这意味着将在项目点击事件上设置图像资源。 / p>

if (item.isOpened()) {

  holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_green);

} else {

  holder.ivWorkingStatus.setImageResource(R.drawable.img_dot_red);

}