****************************************RecyclerViewHorizontal*************************
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" > <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:background="#FF0000" android:scrollbars="none" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="120dp" android:layout_height="120dp" android:background="@drawable/item_bg02" > <ImageView android:id="@+id/id_index_gallery_item_image" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_margin="5dp" android:scaleType="centerCrop" /> <TextView android:id="@+id/id_index_gallery_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/id_index_gallery_item_image" android:layout_centerHorizontal="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:text="some info" android:textColor="#ff0000" android:textSize="12dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/item_bg" android:state_pressed="true"></item> <item android:drawable="@android:color/white" ></item> </selector>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#AA333333"/> </shape>
package com.example.recyclerviewdemo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.Window; import android.widget.Toast; import com.example.recyclerviewdemo.GalleryAdapter.OnItemClickLitener; public class MainActivity extends Activity { private RecyclerView mRecyclerView; private GalleryAdapter mAdapter; private List<Integer> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initDatas(); // 得到控件 mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal); // 设置布局管理器--水平方向 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); // 设置适配器 mAdapter = new GalleryAdapter(this, mDatas); mAdapter.setOnItemClickLitener(new OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show(); } }); mRecyclerView.setAdapter(mAdapter); } private void initDatas() { mDatas = new ArrayList<Integer>(Arrays.asList(R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.l)); } }
package com.example.recyclerviewdemo; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; 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 GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> { /** * ItemClick的回调接口 */ public interface OnItemClickLitener { void onItemClick(View view, int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } /***************************************************/ private LayoutInflater mInflater; private List<Integer> mDatas; public GalleryAdapter(Context context, List<Integer> datats) { mInflater = LayoutInflater.from(context); mDatas = datats; } public static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View inflateView) { super(inflateView); } ImageView mImg; TextView mTxt; } @Override public int getItemCount() { return mDatas.size(); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = mInflater.inflate(R.layout.activity_index_gallery_item, viewGroup, false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.mImg = (ImageView) view .findViewById(R.id.id_index_gallery_item_image); return viewHolder; } @Override public void onBindViewHolder(final ViewHolder viewHolder, final int i) { viewHolder.mImg.setImageResource(mDatas.get(i)); // 如果设置了回调,则设置点击事件 if (mOnItemClickLitener != null) { viewHolder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mOnItemClickLitener.onItemClick(viewHolder.itemView, i); } }); } } }
************************************************Vertical*******************************************
导入V7包下的类
package com.example.recyclerview; public class SampleModel { private String sampleText; public SampleModel(String sampleText) { this.sampleText = sampleText; } public void setSampleText(String sampleText) { this.sampleText = sampleText; } public String getSampleText() { return sampleText; } }
package com.example.recyclerview; import java.util.ArrayList; public class DemoApp { // 获取要显示的数据(初始化数据) public static ArrayList<SampleModel> getSampleData(int size) { ArrayList<SampleModel> sampleData = new ArrayList<SampleModel>(size); for (int i = 0; i < size; i++) { // 每一项数据后面都有相应的序号 sampleData.add(new SampleModel("新的列表项<" + i + ">")); } return sampleData; } }
package com.example.recyclerview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.support.v7.widget.RecyclerView; import android.view.View; public class SampleDivider extends RecyclerView.ItemDecoration { // 默认分隔条Drawable资源的ID private static final int[] ATTRS = { android.R.attr.listDivider }; // 分隔条Drawable对象 private Drawable mDivider; public SampleDivider(Context context) { TypedArray a = context.obtainStyledAttributes(ATTRS); // 获取系统提供的分隔条Drawable对象 mDivider = a.getDrawable(0); // 回收TypedArray所占用的空间 a.recycle(); } // 在该方法中绘制了所有列表项之间的分隔条 @Override public void onDrawOver(Canvas c, RecyclerView parent) { // 获取列表项距离左边缘的距离 int left = parent.getPaddingLeft(); // 获取列表项距离右边缘的距离 int right = parent.getWidth() - parent.getPaddingRight(); // 获取列表项的总数 int childCount = parent.getChildCount(); // 开始绘制所有列表项之间的分隔线 for (int i = 0; i < childCount; i++) { // 获得当前的列表项 View child = parent.getChildAt(i); // 获取当前列表项的布局参数信息 RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); // 计算分隔条左上角的纵坐标,字体的底部 int top = child.getBottom() + params.bottomMargin; // 计算分隔条右下角的纵坐标 int bottom = top + mDivider.getIntrinsicHeight(); // 设置分隔条绘制的位置 mDivider.setBounds(left, top, right, bottom); // 开始绘制当前列表项下方的分隔条 mDivider.draw(c); } } }
package com.example.recyclerview; import java.util.ArrayList; import java.util.Random; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class SampleRecyclerAdapter extends RecyclerView.Adapter<SampleRecyclerAdapter.ViewHolder> { //获取数据源 private final ArrayList<SampleModel> sampleData = DemoApp.getSampleData(20); // 用于创建控件 @Override public ViewHolder onCreateViewHolder(ViewGroup parentViewGroup, int i) { // list_basic_item.xml布局文件中只包含一个<LinearLayer>标签,在该标签中包含 // 了一个<TextView>标签 // item是LinearLayout对象 View item = LayoutInflater.from(parentViewGroup.getContext()).inflate( R.layout.list_basic_item, parentViewGroup, false); return new ViewHolder(item); } // 为控件设置数据 @Override public void onBindViewHolder(ViewHolder viewHolder, final int position) { // 获取当前item中显示的数据 final SampleModel rowData = sampleData.get(position); // 设置要显示的数据 viewHolder.textViewSample.setText(rowData.getSampleText()); viewHolder.itemView.setTag(rowData); } @Override public int getItemCount() { return sampleData.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textViewSample; public ViewHolder(View itemView) { super(itemView); textViewSample = (TextView) itemView .findViewById(R.id.textViewSample); } } }
package com.example.recyclerview; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取RecyclerView对象 final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // 创建线性布局管理器(默认是垂直方向) final LinearLayoutManager layoutManager = new LinearLayoutManager(this); // 为RecyclerView指定布局管理对象 recyclerView.setLayoutManager(layoutManager); // 创建列表项分隔线对象 final RecyclerView.ItemDecoration itemDecoration = new SampleDivider( this); // 为RecyclerView控件指定分隔线对象 recyclerView.addItemDecoration(itemDecoration); String s = "a"; final SampleRecyclerAdapter sampleRecyclerAdapter = new SampleRecyclerAdapter(); recyclerView.setAdapter(sampleRecyclerAdapter); } }
<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=".MainActivity" > <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" tools:listitem="@layout/list_basic_item" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="horizontal" > <TextView android:id="@+id/textViewSample" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:fontFamily="sans-serif-light" android:padding="30dp" android:textSize="20sp" android:text="sample" /> </LinearLayout>
*******************************************RecyclerViewVertical*****************************
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" > <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#44ff0000" android:layout_height="wrap_content" > <TextView android:id="@+id/id_num" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="1" /> </FrameLayout>
package com.example.recyclerview; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private RecyclerView mRecyclerView; private List<String> mDatas; private HomeAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); /** * 分割线 */ mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); } protected void initData() { mDatas = new ArrayList<String>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i); } } class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( MainActivity.this).inflate(R.layout.item, parent, false)); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } class MyViewHolder extends ViewHolder { TextView tv; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.id_num); } } } }
package com.example.recyclerview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; public class DividerItemDecoration extends RecyclerView.ItemDecoration { /** * 通过读取系统主题中的 android.R.attr.listDivider作为Item间的分割线 */ private static final int[] ATTRS = new int[] { android.R.attr.listDivider }; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; private Drawable mDivider; private int mOrientation; /** * 构造器 */ public DividerItemDecoration(Context context, int orientation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); setOrientation(orientation); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } public void drawVertical(Canvas c, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); //得到条目的数量 final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { //得到 final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } public void drawHorizontal(Canvas c, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } /** * 获取到listDivider以后,该属性的值是个Drawable,在getItemOffsets中,outRect去设置了绘制的范围。onDraw中实现了真正的绘制 */ @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } }
<style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:listDivider">@drawable/divider_bg</item> </style>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:centerColor="#ff00ff00" android:endColor="#ff0000ff" android:startColor="#ffff0000" android:type="linear" /> <size android:height="4dp"/> </shape>