测试代码:
activity_main.xml
<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"
tools:context="com.zzw.testrecyclerview.MainActivity" > <android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView> </RelativeLayout>
MainActivity.java:
package com.zzw.testrecyclerview; import java.util.ArrayList; import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; public class MainActivity extends Activity { RecyclerView mRecyclerView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ArrayList<String> mTitles=new ArrayList<String>();
for(int i=0;i<100;i++){
mTitles.add("测试数据--"+i);
} mRecyclerView=(RecyclerView) findViewById(R.id.mRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); MultipleItemAdapter adapter=new MultipleItemAdapter(this, mTitles);
mRecyclerView.setAdapter(adapter); } }
MultipleItemAdapter.java:
package com.zzw.testrecyclerview; import java.util.ArrayList; import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; public class MultipleItemAdapter extends Adapter<ViewHolder> { public static enum ITEM_TYPE {
ITEM_TYPE_IMAGE, ITEM_TYPE_TEXT
} private LayoutInflater mLayoutInflater;
private Context mContext;
private ArrayList<String> mTitle; public MultipleItemAdapter(Context context, ArrayList<String> titles) {
mLayoutInflater = LayoutInflater.from(context);
mContext = context;
mTitle = titles;
} @Override
public int getItemCount() { return mTitle == null ? 0 : mTitle.size();
} @Override
public int getItemViewType(int position) { return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal() : ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();
} @Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(holder instanceof TextViewHolder){
((TextViewHolder) holder).mTextView.setText(mTitle.get(position)); }else if(holder instanceof ImageViewHolder){ ((ImageViewHolder) holder).text.setText(mTitle.get(position));
}
} @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal()) {
return new ImageViewHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));
} else {
return new TextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
}
} public static class TextViewHolder extends ViewHolder { protected static final String TAG = "TextViewHolder"; TextView mTextView; public TextViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textView); itemView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Log.d(TAG, "TextViewHolder"+getPosition());
}
});
}
} public static class ImageViewHolder extends ViewHolder { protected static final String TAG = "ImageViewHolder"; ImageView mImage;
TextView text; public ImageViewHolder(View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image);
text=(TextView) itemView.findViewById(R.id.text);
itemView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Log.d(TAG, "ImageViewHolder"+getPosition());
}
});
}
} }
item_image.xml:
<?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:background="#2196F3"
android:orientation="vertical" > <ImageView
android:id="@+id/image"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" /> <TextView
android:id="@+id/text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp" /> </LinearLayout>
item_text.xml:
<?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:background="#E040FB"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_blue_light"
android:textSize="30sp" /> </LinearLayout>
相关问题解决:
使用android.support.v7.widget.RecyclerView出现 java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5004564.html