RecycleView在eclipse的初体验

时间:2021-12-20 15:50:16

在sdk中找到v7包

\sdk\extras\android\support\v7\recyclerview

导入工程

Import\Android\Existing Android Code Into Workspace

将导入的recycleview作为依赖库Library

工程上右键properties,勾选is Library,Apply,ok

将recycleview\libs\android_support_v7_recycleview.jar包复制粘贴到主工程的libs文件夹下

在layout中使用<android.support.v7.widget.RecyclerView/>标签

MainActivity

 package com.jiatu.recyclerviewtest;

 import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private RecyclerView recyclerView;
private List<Person> list = new ArrayList<Person>();
private LinearLayoutManager mLayoutManager;
// private GridLayoutManager mGridLayoutManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_test_rv);
recyclerView.setHasFixedSize(true); // 固定item的高或者宽,提高性能
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDecoration(this, MyDecoration.VERTICAL_LIST)); mLayoutManager = new LinearLayoutManager(this);
// 设置list的方向,默认是垂直
// mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // 5,spanCount:列数
// mGridLayoutManager = new GridLayoutManager(this, 5); recyclerView.setLayoutManager(mLayoutManager); initData();
setPersonAdapter(); } /**
* 设置适配器
*/
private void setPersonAdapter() {
final PersonAdapter adapter = new PersonAdapter(this, list);
adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() { @Override
public boolean onItemLongClick(int position) {
adapter.addData(position);
Toast.makeText(MainActivity.this, "吼吼吼吼吼", Toast.LENGTH_SHORT).show();
return true;
} @Override
public void onItemClick(int position) {
adapter.removeData(position);
Toast.makeText(MainActivity.this, "哈哈哈哈哈", Toast.LENGTH_SHORT).show();
}
}); recyclerView.setAdapter(adapter);
} /**
* RecyclerView的单击和长按事件监听
*
* @author fans created on 2016年11月3日
*
*/
public interface OnRecyclerViewListener {
void onItemClick(int position); boolean onItemLongClick(int position);
} /**
* 适配器
*
* @author fans created on 2016年11月3日
*
*/
class PersonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private OnRecyclerViewListener onRecyclerViewListener; public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
this.onRecyclerViewListener = onRecyclerViewListener;
} private List<Person> list = new ArrayList<Person>();
private Context context; public PersonAdapter(Context context, List<Person> list) {
this.context = context;
this.list = list;
} /**
* 增加一个item
*
* @param position
*/
public void addData(int position) {
list.add(position, new Person("fans+", 23));
notifyItemInserted(position);
notifyItemRangeChanged(position, list.size());
} /**
* 删减一个item
*
* @param position
*/
public void removeData(int position) {
list.remove(position);
notifyDataSetChanged();
notifyItemRemoved(position);
notifyItemRangeChanged(position, list.size());
} @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new PersonViewHolder(view);
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
PersonViewHolder holder = (PersonViewHolder) viewHolder;
holder.position = i;
Person person = list.get(i);
holder.tv.setText(person.name); } @Override
public int getItemCount() {
return list.size();
} /**
* 自定义holder
*
* @author fans created on 2016年11月4日
*
*/
class PersonViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
public View rootView;
TextView tv;
public int position; public PersonViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.list_item);
rootView = itemView.findViewById(R.id.recycler_view_test_item_person_view);
rootView.setOnClickListener(this);
rootView.setOnLongClickListener(this);
} @Override
public void onClick(View v) {
if (null != onRecyclerViewListener) {
onRecyclerViewListener.onItemClick(position);
}
} @Override
public boolean onLongClick(View v) {
if (null != onRecyclerViewListener) {
return onRecyclerViewListener.onItemLongClick(position);
}
return false;
}
} } /**
* 填充数据
*/
private void initData() {
for (int i = 0; i < 10; i++) {
list.add(new Person("fans" + i, 10 + i));
}
} class Person {
public String name;
public int age; public Person(String name, int age) {
super();
this.name = name;
this.age = age;
} } /**
* 自定义的分割线
*
* @author fans created on 2016年11月4日
*
*/
class MyDecoration extends RecyclerView.ItemDecoration { private Context mContext;
private Drawable mDivider;
private int mOrientation;
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; // 我们通过获取系统属性中的listDivider来添加,在系统中的AppTheme中设置
public final int[] ATRRS = new int[] { android.R.attr.listDivider }; public MyDecoration(Context context, int orientation) {
this.mContext = context;
final TypedArray ta = context.obtainStyledAttributes(ATRRS);
this.mDivider = ta.getDrawable(0);
ta.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, RecyclerView.State state) {
if (mOrientation == HORIZONTAL_LIST) {
drawVerticalLine(c, parent, state);
} else {
drawHorizontalLine(c, parent, state);
}
} // 画横线, 这里的parent其实是显示在屏幕显示的这部分
public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i); // 获得child的布局信息
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);
// Log.d("wnw", left + " " + top + " "+right+" "+bottom+" "+i);
}
} // 画竖线
public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i); // 获得child的布局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
} // 由于Divider也有长宽高,每一个Item需要向下或者向右偏移
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == HORIZONTAL_LIST) {
// 画横线,就是往下偏移一个分割线的高度
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
// 画竖线,就是往右偏移一个分割线的宽度
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
}

layout

 <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="${relativePackage}.${activityClass}" > <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_test_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbccaa"
android:scrollbars="vertical" /> </RelativeLayout>

item

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view_test_item_person_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="16sp"
android:orientation="vertical" > <TextView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#08da1d"
android:gravity="center"
android:textColor="#f7f4f7"
android:textSize="20sp" /> </LinearLayout>