RecyclerView的基本功能介紹:
1. 經過setLayoutManager方法能夠設置佈局,幾個子類佈局以下:java
-> LinearLayoutManager(ListView形式的佈局)android
-> GridLayoutManager(GridView形式的佈局)app
-> StaggeredGridLayoutManager(瀑布流形式的佈局,其實本質仍是GridView,只是每一個Item的高度不同)dom
2. 經過addItemDecoration設置Item之間的分割線ide
3. 經過setItemAnimator設置添加,刪除Item時的動畫佈局
本文因爲須要展現ListView,Vertical GridView,Horizontal GridView和Staggered(瀑布流),因此就不設置Item之間的分割線,直接在XML中設置Item的間距替代。動畫
主界面的佈局(activity_main):ui
<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):this
<?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:spa
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中已經有體現