无法将RecyclerView宽度设置为匹配父级

时间:2018-06-09 14:59:53

标签: java android xml android-layout android-recyclerview

无论我做什么,

Recycler View只占据屏幕的上半部分。 任何人都可以建议如何让Recyclerview占据整个屏幕的宽度?

this is what it displays me in Android Studio

but this is the actual output on a device. It's all wrapped to the left side of the device.

activity.java

match.params.id || "no match!"

activity.xml

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

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

adapter.java

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

}

1 个答案:

答案 0 :(得分:0)

我找到了它:

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

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

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