@Joshua缩短了我的问题..
对于其他用户,我在这里发布的是用于膨胀不同行的代码(3 rows) in the RecycleView
它工作正常,检查以下代码行:-我在这里发布我的完整代码,请检查:-
Here RecyleClass.java
是我的主要课程
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.
*/
public class RecyleClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_main);
RecyclerView recycler_vw = (RecyclerView)findViewById(R.id.recycler_vw);
ArrayList<RecycleBen> arrayList = new ArrayList<>();
for (int i = 0;i<=25;i++)
{
RecycleBen bean = new RecycleBen();
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");
}
else if(i%3==0)
{
bean.setType_row("2");
bean.setName("Second element");
bean.setImage_url("https://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.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recycler_vw.setLayoutManager(mLayoutManager);
recycler_vw.setItemAnimator(new DefaultItemAnimator());
recycler_vw.setAdapter(new RecycleDataAdapter(this, arrayList));
}
}
这是我的layout
为了RecyleClass.java
那是recycle_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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.RecyclerView
android:id="@+id/recycler_vw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
/>
</LinearLayout>
Gradles条目为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'
下面是我的getter
and setter
类即RecycleBen.java
package com.tv.practise.recycleview;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecycleBen {
private String type_row;
private String name;
private String image_url;
public String getType_row() {
return type_row;
}
public void setType_row(String type_row) {
this.type_row = type_row;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
}
最后我的适配器类是RecycleDataAdapter.java
package com.tv.practise.adapter;
/**
* Created by Ravindra Kushwaha on 10/10/16.
*/
public class RecycleDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<RecycleBen> data;
public class SimpleText extends RecyclerView.ViewHolder {
TextView first_data_tv;
public SimpleText(View v) {
super(v);
this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
}
}
public class SimpleImage extends RecyclerView.ViewHolder {
ImageView second_data_iv;
ProgressBar second_pb;
public SimpleImage(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);
}
}
public class SimpleImageWithText extends RecyclerView.ViewHolder {
TextView third_data_tv;
ImageView third_iv;
ProgressBar third_pb;
public SimpleImageWithText(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);
}
}
public RecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
this.mContext = mContext;
this.data = data;
}
@Override
public 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);
return new SimpleText(itemView
);
}
else if(viewType==2)
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_fsecond_item, parent, false);
return new SimpleImage(itemView);
}
else
{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_third_item, parent, false);
return new SimpleImageWithText(itemView);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
RecycleBen bean = data.get(position);
if(holder.getItemViewType()==1)
{
((SimpleText)holder).first_data_tv.setText(bean.getName());
}
else if(holder.getItemViewType()==2)
{
final SimpleImage simple_holder = (SimpleImage)holder;
simple_holder.second_pb.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(bean.getImage_url())
.fitCenter()
.crossFade()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
simple_holder.second_pb.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(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);
return false;
}
}).into(simple_holder.second_data_iv);;
}
else {
final SimpleImageWithText third_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(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (e instanceof UnknownHostException)
third_holder.third_pb.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(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);
return false;
}
}).into(third_holder.third_iv);;
}
}
@Override
public int getItemViewType(int position) {
return Integer.parseInt(data.get(position).getType_row());
}
@Override
public int getItemCount() {
return data.size();
}
}
当我们对不同的行进行充气时Recycleview
,所以我们在这里使用3 layout分别是recycle_first_item.xml
,recycle_fsecond_item.xml
和最后一个recycle_third_item.xml
我一一展示了所有的布局xml
,如下:-
recycle_first_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns: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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android: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.CardView xmlns: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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ProgressBar
android:id="@+id/second_pb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
/>
<ImageView
android: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.CardView xmlns: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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android: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" />
<ProgressBar
android:id="@+id/third_pb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_below="@+id/third_data_tv"
/>
<ImageView
android: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>
And finally the result is as below :-