首页 > 解决方案 > 无法将 RecyclerView 宽度设为匹配父级

问题描述

无论我做什么,Recycler View 都只占据屏幕的前半部分。谁能建议如何让 Recyclerview 占据整个屏幕的宽度?

这就是它在 Android Studio 中显示的内容

这就是它在 Android Studio 中向我展示的内容

但这是设备上的实际输出。它全部包裹在设备的左侧。

但这是设备上的实际输出。 它全部包裹在设备的左侧。

活动.java

public class RecordLogs extends AppCompatActivity {

private RecyclerView recyclerView;
private RecordsAdapter adapter;
private List<Record> recordList;
private ProgressBar progressBar;

private FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record_logs);

    progressBar = findViewById(R.id.progressbar);


    recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager((this)));
    recordList = new ArrayList<>();


    adapter = new RecordsAdapter(this, recordList);

    recyclerView.setAdapter(adapter);

    db = FirebaseFirestore.getInstance();

活动.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".RecordLogs">

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

    <ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
</RelativeLayout>

行.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="15sp"

                tools:text="26/7/2018" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Date"
                android:textSize="10sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/qty"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="15sp"


                tools:text="5" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:text="Qty. L"
                android:textSize="10sp" />

        </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/rate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#000"
                    android:textSize="15sp"


                    tools:text="25" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:text="Rate ₹"
                    android:textSize="10sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/total"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#000"
                    android:textSize="15sp"

                    tools:text="125" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:text="Total ₹"
                    android:textSize="10sp" />

            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

适配器.java

public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.RecordViewHolder> {

private Context mCtx;
private List<Record> recordList;

public RecordsAdapter(Context mCtx, List<Record> recordList) {
    this.mCtx = mCtx;
    this.recordList = recordList;
}

@NonNull
@Override
public RecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.record_logs_card, null);
    return new RecordViewHolder(view);
}

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

    Record record = recordList.get(position);
    holder.date.setText(record.getDate());
    holder.qty.setText(String.valueOf(record.getQty()));
    holder.rate.setText(String.valueOf(record.getRate()));
    holder.total.setText(String.valueOf(record.getTotal()));
}

@Override
public int getItemCount() {
    return recordList.size();
}

class RecordViewHolder extends RecyclerView.ViewHolder {

    TextView date, qty, rate, total;


    public RecordViewHolder(View itemView) {
        super(itemView);

        date = itemView.findViewById(R.id.date);
        qty = itemView.findViewById(R.id.qty);
        rate = itemView.findViewById(R.id.rate);
        total = itemView.findViewById(R.id.total);
    }
}

}

标签: javaandroidxmlandroid-layoutandroid-recyclerview

解决方案


我找到了:

我必须在我的自定义适配器中更改以下代码行,如下所示:

View view = inflater.inflate(R.layout.record_logs_card, null);

View view = inflater.inflate(R.layout.record_logs_card, parent, false);`

推荐阅读