RecyclerView的基础使用
本文讲解一下RecylerView的基础功能的使用,其实我也不是非常的理解,自己的理解贴出来,大家指正指正。代码和效果参考慕课网和以下的博客: http://blog.csdn.net/lmj623565791/article/details/45059587
RecyclerView的基本功能介绍:
1. 通过setLayoutManager方法可以设置布局,几个子类布局如下:
-> LinearLayoutManager(ListView形式的布局)
-> GridLayoutManager(GridView形式的布局)
-> StaggeredGridLayoutManager(瀑布流形式的布局,其实本质还是GridView,只是每个Item的高度不一样)
2. 通过addItemDecoration设置Item之间的分割线
3. 通过setItemAnimator设置添加,删除Item时的动画
本文由于需要展示ListView,Vertical GridView,Horizontal GridView和Staggered(瀑布流),所以就不设置Item之间的分割线,直接在XML中设置Item的间距替代。
主界面的布局(activity_main):
<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" > <LinearLayout android:id="@+id/id_layout_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@android:color/darker_gray"> <Button android:id="@+id/id_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="增加" /> <Button android:id="@+id/id_delete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" /> <Button android:id="@+id/id_menu" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="菜单>" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/id_layout_top" /> </RelativeLayout>
Item的布局文件(item_recyclerview):
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:layout_margin="3dp" android:background="@drawable/selector" > <TextView android:id="@+id/id_num" android:layout_width="70dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="1" /> </FrameLayout>
主界面Activity:
package com.example.testrecyclerview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.example.testrecyclerview.SimpleAdapter.OnItemClickListener; public class MainActivity extends Activity implements android.view.View.OnClickListener { private RecyclerView mRecyclerView; private List<String> mDatas; private SimpleAdapter mAdapter; private StaggeredAdapter mStaggeredAdapter; private Button mAddBtn; private Button mDeleteBtn; private Button mMenuBtn; private AlertDialog.Builder mBuilder; private DialogInterface.OnClickListener mListener; private int mType = 0; private final int SIMPLE = 0; private final int STAGGERED = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); addListener(); // 设置布局 mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); // 设置动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); } protected void initData() { mDatas = new ArrayList<String>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i); } } private void initView() { mAddBtn = (Button) findViewById(R.id.id_add); mDeleteBtn = (Button) findViewById(R.id.id_delete); mMenuBtn = (Button) findViewById(R.id.id_menu); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); mAdapter = new SimpleAdapter(this, mDatas); mStaggeredAdapter = new StaggeredAdapter(this, mDatas); mRecyclerView.setAdapter(mAdapter); mListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // ListView mType = SIMPLE; mRecyclerView.setAdapter(mAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager( MainActivity.this, LinearLayoutManager.VERTICAL, false)); break; case 1: // Vertical GridView mType = SIMPLE; mRecyclerView.setAdapter(mAdapter); mRecyclerView .setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)); break; case 2: // Horizontal GridView mType = SIMPLE; mRecyclerView.setAdapter(mAdapter); mRecyclerView .setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL)); break; case 3: // Staggered mType = STAGGERED; mRecyclerView.setAdapter(mStaggeredAdapter); mRecyclerView .setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)); break; default: break; } } }; String[] menu = { "ListView", "Vertical GridView", "Horizontal GridView", "Staggered" }; mBuilder = new AlertDialog.Builder(this).setTitle("Menu").setItems( menu, mListener); } private void addListener() { mAddBtn.setOnClickListener(this); mDeleteBtn.setOnClickListener(this); mMenuBtn.setOnClickListener(this); mAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemLongClick(View view, int position) { mAdapter.deleteItem(position); Toast.makeText(MainActivity.this, "long click:" + position, Toast.LENGTH_SHORT).show(); } @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, "click:" + position, Toast.LENGTH_SHORT).show(); } }); mStaggeredAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemLongClick(View view, int position) { mStaggeredAdapter.deleteItem(position); Toast.makeText(MainActivity.this, "long click:" + position, Toast.LENGTH_SHORT).show(); } @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, "click:" + position, Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.id_add: if (mType == SIMPLE) { mAdapter.addItem(1); } else { mStaggeredAdapter.addItem(1); } break; case R.id.id_delete: if (mType == SIMPLE) { mAdapter.deleteItem(1); } else { mStaggeredAdapter.deleteItem(1); } break; case R.id.id_menu: mBuilder.show(); break; default: break; } } }
代码中有两个Adapter,分别是SimpleAdapter和StaggeredAdapter,后一个Adapter主要是给瀑布流布局使用,因为其item的高度是随机的。
SimpleAdapter继承RecyclerView中的内置Adapter, 并且RecyclerView中强制用户使用ViewHolder,用户需要写一个RecyclerView中的ViewHolder:
package com.example.testrecyclerview; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.TextView; public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> { protected List<String> mList; protected Context mContext; protected LayoutInflater mInflater; protected OnItemClickListener mClickListener; public SimpleAdapter(Context context, List<String> list){ this.mContext = context; this.mInflater = LayoutInflater.from(context); if (list != null){ this.mList = list; }else { mList = new ArrayList<String>(); } } @Override public int getItemCount() { // TODO Auto-generated method stub return mList.size(); } @Override public void onBindViewHolder(final SimpleViewHolder holder, final int position) { holder.mTextView.setText(mList.get(position)); if (mClickListener != null){ holder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int pos = holder.getPosition(); mClickListener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getPosition(); mClickListener.onItemLongClick(holder.itemView, pos); return false; } }); } } @Override public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int arg1) { View view = mInflater.inflate(R.layout.item_recyclerview, parent, false); SimpleViewHolder holder = new SimpleViewHolder(view); return holder; } public void addItem(int position){ mList.add(position, "Insert one"); notifyItemInserted(position); } public void deleteItem(int position){ mList.remove(position); notifyItemRemoved(position); } public void setOnItemClickListener(OnItemClickListener listener){ this.mClickListener = listener; } class SimpleViewHolder extends ViewHolder{ TextView mTextView; public SimpleViewHolder(View view) { super(view); mTextView = (TextView) view.findViewById(R.id.id_num); } } public interface OnItemClickListener{ void onItemClick(View view, int position); void onItemLongClick(View view, int position); } }
瀑布流的Adapter,继承自SimpleAdapter:
package com.example.testrecyclerview; import java.util.ArrayList; import java.util.List; import com.example.testrecyclerview.SimpleAdapter.SimpleViewHolder; import android.content.Context; 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.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.TextView; public class StaggeredAdapter extends SimpleAdapter { protected List<Integer> mHeights; public StaggeredAdapter(Context context, List<String> list){ super(context, list); mHeights = new ArrayList<Integer>(); for (int i=0; i<mList.size(); i++){ mHeights.add((int) (100 + Math.random()*300)); } } @Override public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int arg1) { View view = mInflater.inflate(R.layout.item_recyclerview, parent, false); SimpleViewHolder holder = new SimpleViewHolder(view); return holder; } @Override public void onBindViewHolder(final SimpleViewHolder holder, final int position) { LayoutParams lp = holder.mTextView.getLayoutParams(); lp.height = mHeights.get(position); holder.mTextView.setLayoutParams(lp); holder.mTextView.setText(mList.get(position)); if (mClickListener != null){ holder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int pos = holder.getPosition(); mClickListener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getPosition(); mClickListener.onItemLongClick(holder.itemView, pos); return false; } }); } } }
需要注意的是,RecyclerView并没有实现点击事件,这需要我们自己写接口并去调用,这一点在Adapter中已经有体现