【问题标题】:RecyclerView items not displaying as expectedRecyclerView 项目未按预期显示
【发布时间】:2020-05-22 18:05:14
【问题描述】:

您好,很抱歉没有在此页面上添加图片,我还没有足够的声誉

This is how it's supposed to lookthis 是它在 recyclerView 中的显示方式,如您所见,我希望左侧的图像与右侧的居中文本,我该如何实现?

这是项目布局的代码

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    >

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imgRowCompany"
        android:layout_width="115dp"
        android:layout_height="80dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="false"
        android:src="@drawable/library_icon"

        android:visibility="visible"
        app:civ_border_color="@color/Blue"
        app:civ_border_width="2.5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtRowCompanyName"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:text="CompanyName"
        android:textColor="#000000"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imgRowCompany"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

recyclerAdapter 代码:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.recycler_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    }

    @Override//Number od items to be returned
    public int getItemCount() {
        return 10;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
       CircleImageView imageRowCompany;
       TextView companyName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageRowCompany=itemView.findViewById(R.id.imgRowCompany);
            companyName=itemView.findViewById(R.id.txtRowCompanyName);
        }
    }
}

以及recyclerView代码:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CompanyLibrary"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    您发布的图片在 RTL 设备上。端到端和开始是正常工作的方式,如果你想以你的方式显示,那么使用

    app:layout_constraintRight_toRightOf="parent" instead of 
    app:layout_constraintEnd_toEndOf="parent"
    

    app:layout_constraintLeft_toLeftOf="parent" instead of 
    app:layout_constraintStart_toStartOf="parent"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-08
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多