Recycler View不填充宽度

时间:2016-02-05 04:07:18

标签: android android-recyclerview

我有一个回收站视图,我在其中显示自定义行,但行没有填充宽度。我添加了match_parent宽度,但它只是包装内容。

我的回收者视图 - :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.transenigma.iskconapp.events"
    tools:showIn="@layout/app_bar_events">

    <view
        android:id="@+id/recycler_view"
        class="android.support.v7.widget.RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="-16dp"
        android:layout_marginRight="-17dp" />

    <!--<ProgressBar-->
        <!--android:id="@+id/progress_bar"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_centerInParent="true" />-->
</RelativeLayout>

自定义行是 - :

<?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="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:cardview="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="80dp">

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

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true">
                <com.transenigma.iskconapp.BlurImageView
                    android:id="@+id/myEventImg"
                    android:layout_width="115dp"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/mayapur" />
                <TextView
                    android:id="@+id/myAboveDay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="-8dp"
                    android:textSize="50dp"
                    android:text="Hari"
                    android:gravity="center_horizontal"/>

                <TextView
                    android:id="@+id/myAboveMonth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:textSize="15dp"
                    android:layout_marginBottom="4dp"
                    android:text="month"
                    android:gravity="center_horizontal"/>

            </FrameLayout>

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

                <TextView
                    android:id="@+id/myEventTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#222"
                    android:textSize="23dp"
                    android:text="Title"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginBottom="10dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="City"
                    android:textColor="#222"
                    android:id="@+id/myEventCity"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="2dp" />

                <!--<TextView-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:textAppearance="?android:attr/textAppearanceSmall"-->
                <!--android:text="Date"-->
                <!--android:id="@+id/myEventDate"-->
                <!--android:textColor="#222"-->
                <!--android:layout_marginLeft="8dp"-->
                <!--android:layout_marginTop="2dp" />-->

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/myEventObjectId"
                android:visibility="invisible"
                android:layout_gravity="center_horizontal" />

        </LinearLayout>

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

</LinearLayout>

我的自定义视图持有人类 - :

public class CustomViewHolder extends RecyclerView.ViewHolder{
        protected ImageView myEventImg;
        protected TextView myEventTitle,myEventCity, myEventObjectId, myAboveDay, myAboveMonth;//,myEventDate

        public CustomViewHolder(View view) {
            super(view);
            this.myEventImg = (ImageView) view.findViewById(R.id.myEventImg);
            this.myEventTitle = (TextView) view.findViewById(R.id.myEventTitle);
            this.myEventCity = (TextView) view.findViewById(R.id.myEventCity);
            //this.myEventDate = (TextView) view.findViewById(R.id.myEventDate);
            this.myEventObjectId = (TextView) view.findViewById(R.id.myEventObjectId);
            this.myAboveDay = (TextView) view.findViewById(R.id.myAboveDay);
            this.myAboveMonth = (TextView) view.findViewById(R.id.myAboveMonth);
        }
    }

以下是整个Adapter类 - :

package com.transenigma.iskconapp;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;

import java.util.List;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
    ProgressDialog progress;
    private List<EventsInfo> feedItemList;
    private Context mContext;
    private int mPreviousPosition = 0;
//    OnItemClickListener mItemClickListener;
    public MyRecyclerAdapter(Context context, List<EventsInfo> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }
    @Override
    public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_event_row, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyRecyclerAdapter.CustomViewHolder holder, int position) {

        EventsInfo feedItem = feedItemList.get(position);

        //Setting text view title
        holder.myEventTitle.setText(Html.fromHtml(feedItem.getMyEventTitle()));
        holder.myEventCity.setText(Html.fromHtml(feedItem.getMyEventCity()));
        //holder.myEventDate.setText(Html.fromHtml(feedItem.getMyEventDate()));
        holder.myEventObjectId.setText(feedItem.getEventObjectId());
        String[] parts = (feedItem.getMyEventDate()).split(" ");
        holder.myAboveDay.setText(parts[0]);
        holder.myAboveMonth.setText(parts[1]);
        Log.i("Radhe", "Object Id  is " + holder.myEventObjectId.getText());
        //progress = ProgressDialog.show( mContext, "Please Wait!","We are downloading events", true );
        loadImages( feedItem.getImageFile(), holder.myEventImg);
        //holder.myEventImg.setImageResource(R.drawable.img);

        /*******************Animation**************************/
        if (position > mPreviousPosition) {
            AnimationUtils.animateSunblind(holder, true);
//            AnimationUtils.animate1(holder, true);
//            AnimationUtils.animate(holder,true);
        } else {
            AnimationUtils.animateSunblind(holder, false);
//            AnimationUtils.animate1(holder, false);
//            AnimationUtils.animate(holder, false);
        }
        mPreviousPosition = position;

    }
    private void loadImages(ParseFile thumbnail, final ImageView img) {

        if (thumbnail != null) {
            thumbnail.getDataInBackground(new GetDataCallback() {
                @Override
                public void done(byte[] data, ParseException e) {
                    if (e == null) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                        img.setImageBitmap(bmp);
                        //progress.dismiss();
                    } else {
                    }
                }
            });
        } else {
            img.setImageResource(R.drawable.mayapur);
        }

    }

    @Override
    public int getItemCount() {
        return (null != feedItemList ? feedItemList.size() : 0);
    }


    public class CustomViewHolder extends RecyclerView.ViewHolder{
        protected ImageView myEventImg;
        protected TextView myEventTitle,myEventCity, myEventObjectId, myAboveDay, myAboveMonth;//,myEventDate

        public CustomViewHolder(View view) {
            super(view);
            this.myEventImg = (ImageView) view.findViewById(R.id.myEventImg);
            this.myEventTitle = (TextView) view.findViewById(R.id.myEventTitle);
            this.myEventCity = (TextView) view.findViewById(R.id.myEventCity);
            //this.myEventDate = (TextView) view.findViewById(R.id.myEventDate);
            this.myEventObjectId = (TextView) view.findViewById(R.id.myEventObjectId);
            this.myAboveDay = (TextView) view.findViewById(R.id.myAboveDay);
            this.myAboveMonth = (TextView) view.findViewById(R.id.myAboveMonth);
        }
    }

}

3 个答案:

答案 0 :(得分:17)

这是因为您添加了适配器下的onCreateViewHolder,如下所示

 @Override
    public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_event_row, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }

如下所示进行更改:

@Override
    public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_event_row, parent, false);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
            return viewHolder;
}

答案 1 :(得分:2)

当您在ViewHolder方法中夸大onCreateViewHolder()的布局时,您会在null来电中传递parent inflate()个参数,因此,Inflater没有任何东西可以与宽度相匹配,并且它会恢复到包装。更改inflate()电话,如下所示:

View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.custom_event_row, parent, false);

答案 2 :(得分:0)

您的RecyclerView位于RelativeLayout内,其中有一些填充。

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

检查这些填充的值是什么。或者你可以删除这些行并尝试。 基于提供的xml,所有内容都有宽度的“match_parent”属性,因此它应该采用除填充之外的整个宽度。