如何删除回收站视图中的空列表项?

时间:2016-05-26 18:09:37

标签: android android-layout android-recyclerview android-cardview

我正在使用回收站视图来显示具有多个视图的项目。每当我启动应用程序时,都会显示一些空白区域。这完全等于我的列表项。 我提供了截图,请仔细阅读。

enter image description here

我认为我的代码没有问题。 以下是我的代码 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代码                          

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

 </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>

请帮帮我..谢谢

3 个答案:

答案 0 :(得分:2)

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

pdf_row_view.xml, group_request_row_view.xml, pdf_row_view.xml 

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

答案 1 :(得分:1)

删除org.apache.axis2.AxisFault: Unconnected sockets not implemented Unable to sendViaPost to url[my_http_endpoint] 并将match_parent设置为wrap_content,而不是列表项目布局的根目录。

例如,您的layout_height应该如下所示

pdf_row_view.xml

请参阅,我已在<?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="wrap_content" 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"> <!-- Other elements in your layout --> </android.support.v7.widget.CardView> </RelativeLayout> 列表项的根android:layout_height="wrap_content"中设置RelativeLayout

对其他列表项布局执行相同操作。

答案 2 :(得分:0)

只需将特定viewHolder的rootView的高度设置为0,就像这样 -

RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
rootView.setLayoutParams(layoutParams);

其中rootView与此相同 -

ViewHolder(View view) {
        super(view);
        rootView = view;
}

如果您想更清楚地了解这一点,请告诉我。