2016-02-05 2 views
1

У меня есть вид recycler, в котором я показываю пользовательскую строку, но строка не заполняет ширину. Я добавил match_parent ширину, но все же это просто упаковка содержимого.Recycler View не заполняет ширину

Мой утилизатор view-:

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

Мой заказ Просмотр Holder class-:

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

Вот весь class- адаптер :

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

} 
+0

Пожалуйста, пост ваш класс «ViewHolder». –

+0

вы хотите, чтобы кардвайс был полностью закрыт? Я думаю, что ваш BlurImageView не подходит, так? @pranav shukla –

+0

Ой, извините. Я имел в виду класс «Адаптер». Собственно, просто метод 'onCreateViewHolder()'. –

ответ

14

Это потому, что вы добавили 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; 
} 
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 для ширины, поэтому он должен иметь полную ширину, отличную от заполнения.

2

Когда вы раздуть макет по ViewHolder «s в методе onCreateViewHolder(), вы передаете null для параметра parent в inflate() аукнется, так Inflater не имеет никакого отношения, к которым в соответствии с шириной, и он возвращается к обертке. Изменение inflate() вызова следующим образом:

View view = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.custom_event_row, parent, false); 
Смежные вопросы