单行显示Insight RecyclerView

时间:2019-04-27 07:05:14

标签: android android-recyclerview

我有recyclerview,它仅显示单个记录,而不是两个记录。我在Google上搜索了很多帖子,发现将listview项目的高度从“ match_parent”更改为“ Wrap_Content”,因此我做了同样的事情,但仍然显示单个记录。

孟买男性活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MumbaiMale">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/myImg"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/age"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/education"/>

    </LinearLayout>



</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            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:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:scrollbars="vertical"
            android:id="@+id/recyclerView" />

</LinearLayout>

CustomeAdapter是:

package com.maheshwaghela.mahesh.rukhivivah;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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


private MumbaiMale.IconData[] data;

public CustomAdapter (MumbaiMale.IconData[] data) {
    this.data = data;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View listItem= layoutInflater.inflate(R.layout.activity_mumbai_male, parent, false);
    ViewHolder viewHolder = new ViewHolder(listItem);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.name.setText(data[position].getPname());
    holder.age.setText(data[position].getPage());
    holder.education.setText(data[position].getPedu());
    holder.imageView.setImageResource(data[position].getImgId());
}

@Override
public int getItemCount() {
    return data.length;
}



public static class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public TextView name;
    public TextView age; public TextView education;

    public ViewHolder(View itemView) {
        super(itemView);
        this.imageView = (ImageView) itemView.findViewById(R.id.myImg);
        this.name = (TextView) itemView.findViewById(R.id.name);
        this.age=(TextView) itemView.findViewById(R.id.age);
        this.education=(TextView) itemView.findViewById(R.id.education);
      }
   }

}

孟买男(Mumbai_Male.Java)

package com.maheshwaghela.mahesh.rukhivivah;

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


public class MumbaiMale extends  AppCompatActivity {



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


    IconData[] data = new IconData[] {
            new IconData("Name:- Mahesh K. Waghela","Age:-45","Education:-B.com", R.drawable.dilip333),
            new IconData ("Name:- Sunil K. Waghela","Age:-33","Education:-S.S.C.",R.drawable.hiteshhh)

    };

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    CustomAdapter adapter=new CustomAdapter(data);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);



}

public class IconData {
    private String pname;
    private String page;
    private String pedu;
    private int imgId;

    public IconData(String name, String age, String edu, int imgId) {
        this.pname = name;
        this.page= age;
        this.pedu= edu;
        this.imgId = imgId;
    }

    public String getPname()
    {
        return pname;
    }

    public void setPname(String name)
    {
        this.pname = name;
    }


    public String getPage()
    {
        return  page;
    }

    public void setPage(String age)
    {
        this.page=age;
    }


    public String getPedu()
    {
        return pedu;
    }

    public void setMyPedu(String edu)
    {
        this.pedu=edu;
    }


    public int getImgId()
    {
        return imgId;
    }

    public void setImgId(int imgId)
    {
        this.imgId = imgId;
    }
}

}

我错在哪里不知道,但它显示的是单个记录而不是两个记录。

2 个答案:

答案 0 :(得分:1)

根据您当前的代码,您的RecyclerView将同时显示wrap_contentmatch_parent的记录。如果您要滚动列表,则可以看到第二条记录。

此外,由于使用的布局与“活动布局”和“适配器”的项目布局相同,因此您将获得这种有线输出。因此,适配器将绑定图像,名称等。但是,每行还将显示空白RecyclerView

因此解决方案是将RecyclerView保留在activity_mumbai_male.xml内,并为您的RecyclerView项目创建新的xml文件,然后在适配器内填充该布局xml文件。您的问题将得到解决。

请检查下面的更正代码。

  

activity_mumbai_male.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MumbaiMale">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:scrollbars="vertical" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>
  

item_list.xml

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

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="90dp"
        android:layout_height="90dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/education"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>
  

用下面的代码替换CustomAdapter.xml中的代码

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View listItem = layoutInflater.inflate(R.layout.item_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(listItem);
    return viewHolder;
}

答案 1 :(得分:0)

您正在使用RecyclerView,但是我找不到关于Row的xml,您已将文本设置为您在activity_mumbai.xml中提供的Simple Single TextView,要创建recyclerview,您需要创建另一个xml作为一行,表示RecyclerView的单个项目,并在该视图中打印xml数据。

row_item_view.xml

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

    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/myImg"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/age"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/education"/>

    </LinearLayout>



</LinearLayout>

并使用此新xml在您的自定义适配器中更改SetContentView。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View listItem= layoutInflater.inflate(R.layout.row_item_view, parent, false);
    ViewHolder viewHolder = new ViewHolder(listItem);
    return viewHolder;
}

这将解决您的问题。