单行显示Insight RecyclerView [英] Single Row Shows Insight RecyclerView

查看:72
本文介绍了单行显示Insight RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have recyclerview and it shows just single records instead of two records. I have searched many post on google and found to modify height of listview items from "match_parent " To "Wrap_Content" hence I did the same but still it shows single records.

孟买男性活动

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

CustomeAdapter is:

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)

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;
    }
}

}

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

Where I am wrong don't know but it shows single records instead of two records.

推荐答案

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

As per your current code, your RecyclerView will display both record for both wrap_content and match_parent. You can see second record if you will scroll your list.

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

Also You are getting this type of wired output because your are using the same layout as a Activity Layout and Adapter's item layout. So adapter will bind image, name etc.. but it will also display a blank RecyclerView for each row.

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

So the solution is just keep your RecyclerView inside activity_mumbai_male.xml and create new xml file for your RecyclerView item and inflate that layout xml file inside your adapter. Your issue will be fixed.

请检查以下更正的代码.

Please check below corrected code.

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

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中的代码

Replace your code with my below code in 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;
}

这篇关于单行显示Insight RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆