1.特点
1.不关心Item是否显示
LayoutManager
2.不关心Item 之间如何分隔
控制分隔效果 - - ItemDecoration
下载地址https://gist.github.com/alexfu/0f464fc3742f134ccd1e3.不关注Item增加与删除的动画效果
ItemAnimation 删除动画的效果
4.只关注如何回收和复用View
2.主要相关的几个类
- Adapter
- ViewHolder
- LayoutManager (显示风格,(纵横)listView、gridView、瀑布流)
- ItemDecoration (分隔效果)
- ItemAnimation (内部Item增删的动画效果)
3.简单实用(实现listView)
3.1布局中加入RecyclerView
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_recyclerView"
>
3.2ViewHolder 类
public class MySimpleAdapter extends RecyclerView.Adapter<MyViewHolder> {
private LayoutInflater mInflater;
private Context mContext;
private List<String> mDatas;
public MySimpleAdapter(Context context, List<String> datas) {
this.mContext = context;
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = mInflater.inflate(R.layout.item_single_textview,viewGroup,false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
myViewHolder.tv.setText(mDatas.get(i));
}
@Override
public int getItemCount() {
return mDatas.size();
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
// 一般会有当前item 所有的控件
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.id_tv);
}
}
3.3 Item 布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_margin="3dp"
android:background="#44ff0000"
>
<TextView
android:layout_width="72dp"
android:layout_height="match_parent"
android:id="@+id/id_tv"
android:gravity="center"
/>
</FrameLayout>
3.4 MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> mDatas;
private MySimpleAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initViews();
mAdapter = new MySimpleAdapter(this,mDatas);
mRecyclerView.setAdapter(mAdapter);
//设置RecyclerView的布局管理 //展现为listView 的Style
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayout.VERTICAL,false);
mRecyclerView.setLayoutManager(manager);
//设置RecyclerView之间的分割线
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL_LIST));
}
private void initDatas() {
mDatas = new ArrayList<String>();
for (int i=0;i<10;i++){
mDatas.add("miss" + i);
}
}
private void initViews() {
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerView);
}
}
3.5 分割线(divider_02)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="4dp"/>
<gradient android:startColor="#ffff0000"
android:centerColor="#ff0000ff"
android:endColor="#ff00ff00"
android:type="linear"/>
</shape>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:listDivider">@drawable/divider_02</item>
</style>
</resources>
4.GridView
//gridView 展示
public void grid(View view){
mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
}
//gridView 横向显示
public void gridh(View view){
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,
StaggeredGridLayoutManager.HORIZONTAL));
}
5.瀑布流
5.1 控制高度MySimpleAdapter,MyViewHolder转换成内部类
private List<Integer> mHeights;
public MySimpleAdapter(Context context, List<String> datas) {
this.mContext = context;
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
mHeights = new ArrayList<Integer>();
for (int i=0; i<mDatas.size();i++){
mHeights.add((int)(100+Math.random()*300));
}
}
5.2 瀑布流
//gridView 瀑布流
public void gridh(View view){
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL));
}
增加和删除Item的方法
Adapter里面
public void addData(int pos){
mDatas.add(pos,"insert one");
notifyItemInserted(pos);
}
public void deleteData(int pos){
mDatas.remove(pos);
notifyItemRemoved(pos);
}
方便调用
Activity中
public void add(View view){
mAdapter.addData(1);
}
public void delete(View view){
mAdapter.deleteData(1);
}
7.添加监听
7.1 在Adapter类中添加
public interface OnItemClickListener{
void onItemClick(View view,int postion);
void onItemLongClick(View view,int postion);
}
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener){
this.mOnItemClickListener = listener;
}
7.2 在onBindViewHolder
public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {
myViewHolder.tv.setText(mDatas.get(i));
if (mOnItemClickListener != null){
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnItemClickListener.onItemClick(myViewHolder.itemView,i);
}
});
//longClick
myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mOnItemClickListener.onItemLongClick(myViewHolder.itemView,i);
return false;
}
});
}
}
7.3 监听的实现
mAdapter = new MySimpleAdapter(this,mDatas);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter.setOnItemClickListener(new MySimpleAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int postion) {
Toast.makeText(MainActivity.this, "Click:"+postion, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int postion) {
Toast.makeText(MainActivity.this, "LongClick:"+postion, Toast.LENGTH_SHORT).show();
}
});
}
7.4 存在问题,Click postion 不对,愿意:notifyInsert..()不会刷新所有的view
在点击事件之后,加入
public void onClick(View view) {
int layoutPosition = myViewHolder.getLayoutPosition();
mOnItemClickListener.onItemClick(myViewHolder.itemView,layoutPosition);
}