使用CardLayout和RecyclerView的项目无法正确显示

时间:2018-11-22 18:30:43

标签: android

我目前正在关注本教程:enter image description here

我试图在“回收者视图”中列出许多卡,但是由于某种原因,在遵循了教程和提供的代码后,看起来只有一张卡,而所有内容都在一张卡中。查看图片:

{{3}}

显示initData方法中定义的所有对象,但是正如我提到的,似乎所有这些对象都只添加到一张卡中了吗?任何帮助将不胜感激!

quiz_item_ui.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_photo"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_name"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@id/person_photo"
            android:layout_alignParentTop="true"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_age"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_toEndOf="@id/person_photo"
            android:layout_below="@+id/person_name"/>

    </RelativeLayout>

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

activity_home.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="16dp"
    tools:context=".HomeActivity">

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

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


</LinearLayout>

QuizItemAdapter.java

    public class QuizItemAdapter extends RecyclerView.Adapter<QuizItemAdapter.QuizItemViewHolder>
{

    public static class QuizItemViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;

        QuizItemViewHolder(View itemView)
        {
            super(itemView);
            cv = itemView.findViewById(R.id.cv);
            personName = itemView.findViewById(R.id.person_name);
            personAge = itemView.findViewById(R.id.person_age);
            personPhoto = itemView.findViewById(R.id.person_photo);

        }
    }
    List<QuizItem> items;

    QuizItemAdapter(List<QuizItem> items)
    {
        this.items = items;
    }

    @Override
    public QuizItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
    {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quiz_item_ui,viewGroup,false);
        QuizItemViewHolder qi = new QuizItemViewHolder(v);
        return qi;
    }

    @Override
    public void onBindViewHolder(QuizItemViewHolder quizItemViewHolder, int i)
    {
        quizItemViewHolder.personName.setText(items.get(i).name);
        quizItemViewHolder.personAge.setText(items.get(i).age);
        quizItemViewHolder.personPhoto.setImageResource(items.get(i).photoId);
    }

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

HomeActivity.java

    public class HomeActivity extends AppCompatActivity
{
    private List<QuizItem> qItems;
    private RecyclerView rv;

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

        rv = findViewById(R.id.rv);
        LinearLayoutManager lm = new LinearLayoutManager(this);
        rv.setLayoutManager(lm);
        rv.setHasFixedSize(true);

        initData();
        initAdapter();
    }
    private void initData()
    {
        qItems = new ArrayList<>();
        qItems.add(new QuizItem("Emma Wilson","23 years old",R.drawable.ic_location));
        qItems.add(new QuizItem("Emma Wilson","23 years old",R.drawable.ic_location));
        qItems.add(new QuizItem("Emma Wilson","23 years old",R.drawable.ic_location));

    }

    private void initAdapter()
    {
        QuizItemAdapter a = new QuizItemAdapter(qItems);
        rv.setAdapter(a);
    }


}

谢谢!

1 个答案:

答案 0 :(得分:1)

卡片在那里-您可以在侧面之间发现微小的三角形阴影。阴影需要一些空间来绘制自身。您需要从主布局中删除填充,并为卡片的布局添加一些边距。

<LinearLayout 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"
    android:padding="16dp"    // remove this padding
    tools:context=".HomeActivity">

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"    // add margin here
    android:id="@+id/cv"/>