如何在回收者视图中删除空列表项? [英] how to remove empty list item in recycler views?

查看:68
本文介绍了如何在回收者视图中删除空列表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回收站视图来显示具有多个视图的项目.每当我启动该应用程序时,都会显示一些空白.与我的清单项目完全相同. 我提供了屏幕截图,请仔细检查.

I'm using recycler view to show items with multiple views. whenever I launch the app some white space is being showed. which is exactly equal to my list items. I provided the screenshot please go through it.

我认为我的代码没有问题. 以下是我的代码 recyclerview

I think there is no problem with my code. following is my code recyclerview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".activities.GroupView">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_group_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

适配器代码

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Objects;

import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.pojo.PdfItemHolder;
import pdfshare.hemanthreddy.com.pdfshare.pojo.RequestPojo;

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

final int PDF = 0,REQUEST = 1;
List<Object> list;
OnClickListener onClickListener;
Context context;

public GroupContentAdapter(List<Object> list, OnClickListener onClickListener, Context context) {
    this.list = list;
    this.onClickListener = onClickListener;
    this.context = context;
}

public class PdfHolder extends RecyclerView.ViewHolder {

    TextView pdfName,pdfDescription,Helped,uploadedBy;
    Button downloadPdf,viewPdf;
    View container;
    CardView cardView;
    public PdfHolder(View itemView) {
        super(itemView);
        container = itemView;
        uploadedBy = (TextView) itemView.findViewById(R.id.pdf_info_uplodedby);
        pdfName = (TextView) itemView.findViewById(R.id.pdf_name);
        pdfDescription = (TextView) itemView.findViewById(R.id.pdf_description);
        Helped = (TextView) itemView.findViewById(R.id.helped);
        downloadPdf = (Button) itemView.findViewById(R.id.download_pdf);
        viewPdf = (Button) itemView.findViewById(R.id.view_pdf);
        cardView = (CardView) itemView.findViewById(R.id.card_view_pdf_row);

    }
}
public class RequestHolder extends RecyclerView.ViewHolder {
    View container;
    TextView name,request;
    public RequestHolder(View itemView) {
        super(itemView);
        container = itemView;
        name = (TextView) itemView.findViewById(R.id.requested_name);
        request = (TextView) itemView.findViewById(R.id.request);
    }
}
public interface OnClickListener{
    public void OnItemClick(View view,int position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    switch (viewType) {
        case PDF:
            View v1 = inflater.inflate(R.layout.pdf_row_view,parent,false);
            viewHolder = new PdfHolder(v1);
            break;
        case REQUEST:
            View v2 = inflater.inflate(R.layout.group_request_row_view,parent,false);
            viewHolder = new RequestHolder(v2);
            break;
        default:
            View v3 = inflater.inflate(R.layout.pdf_row_view,parent,false);
            viewHolder = new PdfHolder(v3);
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if(holder.getItemViewType() == PDF)
    {

        PdfItemHolder pdfItemHolder = (PdfItemHolder) list.get(position);
        PdfHolder pdfHolder = (PdfHolder) holder;
        if(!TextUtils.isEmpty(pdfItemHolder.getPdfName())) {
            pdfHolder.pdfName.setText(pdfItemHolder.getPdfName());
            pdfHolder.pdfDescription.setText(pdfItemHolder.getPdfDescription());
            pdfHolder.Helped.setText(pdfItemHolder.getHelped());
            pdfHolder.uploadedBy.setText(pdfItemHolder.getUplodedBy());
            pdfHolder.downloadPdf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "downloading pdf bitch", Toast.LENGTH_LONG).show();
                }
            });
            pdfHolder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.OnItemClick(v, position);
                }
            });
        }
        else
        {
            Toast.makeText(context,"empty",Toast.LENGTH_LONG).show();
            Log.e("empty","empty");
            pdfHolder.container.setVisibility(View.GONE);
            pdfHolder.pdfName.setVisibility(View.GONE);
            pdfHolder.pdfDescription.setVisibility(View.GONE);
            pdfHolder.Helped.setVisibility(View.GONE);
            pdfHolder.downloadPdf.setVisibility(View.GONE);
            pdfHolder.uploadedBy.setVisibility(View.GONE);
            pdfHolder.viewPdf.setVisibility(View.GONE);
        }
    }
    else
    {
        RequestHolder requestHolder = (RequestHolder) holder;
        RequestPojo requestPojo = (RequestPojo) list.get(position);
        requestHolder.name.setText(requestPojo.getName());
        requestHolder.request.setText(requestPojo.getRequest());

    }
}

@Override
public int getItemCount() {
    return list.size();
}
@Override
public int getItemViewType(int position)
{
    if(list.get(position) instanceof PdfItemHolder)
        return PDF;
    else if(list.get(position) instanceof RequestPojo)
        return REQUEST
        ;
    return -1;
}
}

我还检查了堆栈溢出以删除空间,并向适配器添加了一些代码以检查数据是否为空,但效果不佳. 以下是行视图的代码`group_row_view代码

also I checked stack overflow to remove spaces and I added some code to adapter to check wheather the data is empty or not, it didn't worked well. following Is code of row views`group_row_view code

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Requested By : "
            android:id="@+id/textView7" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Name"
            android:id="@+id/requested_name"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/textView7"
            android:layout_toEndOf="@+id/textView7" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#c0c0c0"

            android:id="@+id/view1"
            android:layout_below="@+id/textView7"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/request"
            android:lines="2"
            android:layout_below="@+id/view1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>
    </RelativeLayout>
    </android.support.v7.widget.CardView>

Pdf_row_view的代码

code of Pdf_row_view

 </LinearLayout>`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
    android:id="@+id/card_view_pdf_row"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="3dp"
    card_view:contentPadding="2dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="pdfName"
            android:id="@+id/pdf_name"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"

            android:textSize="25dp"
            android:lines="1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="pdf descriptionl"
            android:id="@+id/pdf_description"
            android:layout_below="@+id/pdf_name"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:lines="2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View Pdf"
            android:id="@+id/view_pdf"
            android:layout_alignTop="@+id/download_pdf"
            android:layout_toRightOf="@+id/pdf_info_uplodedby"
            android:layout_toEndOf="@+id/pdf_info_uplodedby" />
        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Download Pdf"
            android:id="@+id/download_pdf"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/pdf_description" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="helped"
            android:id="@+id/helped"
            android:textSize="15dp"
            android:layout_alignBottom="@+id/view_pdf"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#c0c0c0"
            android:layout_alignTop="@+id/view_pdf"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/view" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#c0c0c0"

            android:id="@+id/view1"
            android:layout_below="@+id/view_pdf"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Upladed by : "
            android:id="@+id/pdf_info_uplodedby"
            android:layout_below="@+id/view_pdf"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>


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

 </RelativeLayout>

请帮助我..谢谢

推荐答案

在所有列表项布局文件中:

In all of your list item layout files:

pdf_row_view.xml, group_request_row_view.xml, pdf_row_view.xml 

设置其根布局的layout_height="wrap_content"

这篇关于如何在回收者视图中删除空列表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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