編輯:關於android開發
測試代碼:
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
基於anyrtc的sdk實現直播連麥互動,anyrtcsdk直播基於anyrtc的sdk實現直播連麥互動 前言 1.由於粘貼了較大的代碼,造成內容比較長,可能會花費您
Android進階中級教程——1.1 Git的本地使用詳解 Android進階中級教程——1.1 Git的本地使用詳解 標簽(空格分隔): And
Android——eclipse下運行android項目報錯 Conversion to Dalvik format failed with error 1解決,andr
Android studio環境搭建,androidstudio搭建首先要下載jdk,下載好以後配置環境變量,這裡略過,不會的可以百度搜索,這裡附上jdk下載地址:htt