Recycle-view Inflating Different Row :- Getting Exception While Binding The Data
Solution 1:
@Joshua shorted out my problem..
For the other users i am posting here is the code for inflating the different rows (3 rows) in the RecycleView
And it works fine , check the below lines of the code:- I am posting my full code here please check:-
Here RecyleClass.java
is my main class
package com.tv.practise.recycleview;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.tv.practise.R;
import java.util.ArrayList;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/publicclassRecyleClassextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_main);
RecyclerViewrecycler_vw= (RecyclerView)findViewById(R.id.recycler_vw);
ArrayList<RecycleBen> arrayList = newArrayList<>();
for (inti=0;i<=25;i++)
{
RecycleBenbean=newRecycleBen();
if(i%2==0)
{
bean.setType_row("1");
bean.setName("First element");
bean.setImage_url("http://www.androhub.com/wp-content/uploads/2015/09/staggeredrecyclerview_banner.jpg");
}
elseif(i%3==0)
{
bean.setType_row("2");
bean.setName("Second element");
bean.setImage_url("http://i.stack.imgur.com/snB84.png");
}
else
{
bean.setType_row("3");
bean.setName("Third element");
bean.setImage_url("http://inducesmile.com/wp-content/uploads/2015/05/gridbanner.jpg");
}
arrayList.add(bean);
}
RecyclerView.LayoutManagermLayoutManager=newLinearLayoutManager(this);
recycler_vw.setLayoutManager(mLayoutManager);
recycler_vw.setItemAnimator(newDefaultItemAnimator());
recycler_vw.setAdapter(newRecycleDataAdapter(this, arrayList));
}
}
Here is the my layout
for the RecyleClass.java
that is recycle_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:id="@+id/recycler_vw"android:layout_width="match_parent"android:layout_height="wrap_content"android:clipToPadding="false"
/></LinearLayout>
Gradles entry for the Recycleview
with CardView
and Glide
// CardView
compile 'com.android.support:cardview-v7:23.4.0'// RecyclerView
compile 'com.android.support:recyclerview-v7:23.4.0'// For the glide libraray
compile 'com.github.bumptech.glide:glide:3.7.0'
And below is my the getter
and setter
class that is RecycleBen.java
package com.tv.practise.recycleview;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/publicclassRecycleBen {
privateString type_row;
privateString name;
privateString image_url;
publicStringgetType_row() {
return type_row;
}
publicvoidsetType_row(String type_row) {
this.type_row = type_row;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicStringgetImage_url() {
return image_url;
}
publicvoidsetImage_url(String image_url) {
this.image_url = image_url;
}
}
And at the last my Adapter class that is RecycleDataAdapter.java
package com.tv.practise.adapter;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/publicclassRecycleDataAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<RecycleBen> data;
publicclassSimpleTextextendsRecyclerView.ViewHolder {
TextView first_data_tv;
publicSimpleText(View v) {
super(v);
this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
}
}
publicclassSimpleImageextendsRecyclerView.ViewHolder {
ImageView second_data_iv;
ProgressBar second_pb;
publicSimpleImage(View v) {
super(v);
this.second_data_iv = (ImageView) v.findViewById(R.id.second_data_iv);
this.second_pb = (ProgressBar)v.findViewById(R.id.second_pb);
}
}
publicclassSimpleImageWithTextextendsRecyclerView.ViewHolder {
TextView third_data_tv;
ImageView third_iv;
ProgressBar third_pb;
publicSimpleImageWithText(View v) {
super(v);
this.third_data_tv = (TextView) v.findViewById(R.id.third_data_tv);
this.third_iv = (ImageView) v.findViewById(R.id.third_iv);
this.third_pb = (ProgressBar)v.findViewById(R.id.third_pb);
}
}
publicRecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
this.mContext = mContext;
this.data = data;
}
@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if(viewType==1)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_first_item, parent, false);
returnnewSimpleText(itemView
);
}
elseif(viewType==2)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_fsecond_item, parent, false);
returnnewSimpleImage(itemView);
}
else
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_third_item, parent, false);
returnnewSimpleImageWithText(itemView);
}
}
@OverridepublicvoidonBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
RecycleBenbean= data.get(position);
if(holder.getItemViewType()==1)
{
((SimpleText)holder).first_data_tv.setText(bean.getName());
}
elseif(holder.getItemViewType()==2)
{
finalSimpleImagesimple_holder= (SimpleImage)holder;
simple_holder.second_pb.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(bean.getImage_url())
.fitCenter()
.crossFade()
.listener(newRequestListener<String, GlideDrawable>() {
@OverridepublicbooleanonException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
simple_holder.second_pb.setVisibility(View.VISIBLE);
returnfalse;
}
@OverridepublicbooleanonResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
simple_holder.second_pb.setVisibility(View.GONE);
simple_holder.second_data_iv.setVisibility(View.VISIBLE);
returnfalse;
}
}).into(simple_holder.second_data_iv);;
}
else {
finalSimpleImageWithTextthird_holder= (SimpleImageWithText)holder;
third_holder.third_data_tv.setText(bean.getName());
third_holder.third_pb.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(bean.getImage_url())
.fitCenter()
.crossFade()
.listener(newRequestListener<String, GlideDrawable>() {
@OverridepublicbooleanonException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
third_holder.third_pb.setVisibility(View.VISIBLE);
returnfalse;
}
@OverridepublicbooleanonResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
third_holder.third_pb.setVisibility(View.GONE);
third_holder.third_iv.setVisibility(View.VISIBLE);
returnfalse;
}
}).into(third_holder.third_iv);;
}
}
@OverridepublicintgetItemViewType(int position) {
return Integer.parseInt(data.get(position).getType_row());
}
@OverridepublicintgetItemCount() {
return data.size();
}
}
As we are inflating the different rows in the Recycleview
, so we used here 3 layout which are as recycle_first_item.xml
,recycle_fsecond_item.xml
and last one recycle_third_item.xml
One by one i am showing all the layout xml
, which are as follow:-
recycle_first_item.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@+id/card_view"android:layout_width="match_parent"android:layout_height="100dp"android:layout_gravity="center"android:layout_margin="5dp"card_view:cardCornerRadius="4dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"><TextViewandroid:id="@+id/first_data_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@android:color/black"android:textSize="25sp"android:text="Hello Card" /></LinearLayout>
recycle_fsecond_item.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@+id/card_view"android:layout_width="match_parent"android:layout_height="150dp"android:layout_gravity="center"android:layout_margin="5dp"card_view:cardCornerRadius="4dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"
><ProgressBarandroid:id="@+id/second_pb"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"
/><ImageViewandroid:id="@+id/second_data_iv"android:layout_width="match_parent"android:layout_height="120dp"android:textColor="@android:color/black"android:layout_centerInParent="true"android:visibility="invisible"android:src="@drawable/bubble1" /></RelativeLayout></android.support.v7.widget.CardView>
recycle_third_item.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@+id/card_view"android:layout_width="match_parent"android:layout_height="150dp"android:layout_gravity="center"android:layout_margin="5dp"card_view:cardCornerRadius="4dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"><TextViewandroid:id="@+id/third_data_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@android:color/black"android:textSize="25sp"android:layout_centerInParent="true"android:text="Hello Card" /><ProgressBarandroid:id="@+id/third_pb"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"android:layout_below="@+id/third_data_tv"
/><ImageViewandroid:id="@+id/third_iv"android:layout_width="match_parent"android:layout_below="@+id/third_data_tv"android:layout_height="150dp"android:src="@drawable/bubble2"android:layout_centerInParent="true"android:visibility="invisible"
/></RelativeLayout></android.support.v7.widget.CardView>
Solution 2:
The reason isonCreateViewHolder(ViewGroup parent, int viewType)
. The parameter is view type but not position. Using int listViewItemType = getItemViewType(viewType)
is incorrect because position
should be passed to getItemViewType
.
In short, you should use viewType
directly and remove listViewItemType
in onCreateViewHolder
.
Post a Comment for "Recycle-view Inflating Different Row :- Getting Exception While Binding The Data"