RecyclerView仅显示一个项目

时间:2017-07-19 15:12:27

标签: android android-recyclerview

我正在使用RecyclerView但是当在recyclerview中查看的项目只显示列表中的第一项时 这里是RecyclerView适配器

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

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewname" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewprice" />
</LinearLayout>

这里是名为recyclerviewrow.xml

的布局
 package com.example.abdelmagied.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class Main3Activity extends AppCompatActivity {
       public RecyclerView mRecyclerView;
        public RecyclerView.LayoutManager mymanager;
        public RecyclerView.Adapter recyclerViewAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main3);
            Bundle bundle =  getIntent().getExtras();
            ArrayList<items> myitems = bundle.getParcelableArrayList("myarray");
            ArrayList<items> go = new ArrayList<items>();
             go.add(new items("mobile" , " blablabl" , "655"));
            go.add(new items("labtop" , " blablabl" , "655"));
            go.add(new items("sony" , "blablbl" , "655"));
            go.add(new items("mobile" , " blablabla" , "655"));
            go.add(new items("mobile" , "blablabla" , "655"));
            mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerId);
            mymanager = new LinearLayoutManager(this);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(mymanager);
            recyclerViewAdapter = new RecyclerAdapter(this , go);
            mRecyclerView.setAdapter(recyclerViewAdapter);

        }
    }

这里是Main3Activity.xml

{{1}}

3 个答案:

答案 0 :(得分:2)

问题是match_parent,将您的父项android:layout_height更改为wrap_content。

现在你也会有物品,但它会在底部,如果你滚动你可以看到第二项。

所以这样改变,

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

答案 1 :(得分:0)

在recyclerviewrow.xml中,将LinearLayout高度更改为wrap_content。

答案 2 :(得分:0)

您还可以为LinearLayout提供minWidth并将宽度设置为wrap_content

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

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewname" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewprice" />
</LinearLayout>