Translate

2015年12月3日 星期四

RecyclerView 使用方法



RecyclerView中提供了三種佈局管理器,分別是:

  • LinearLayoutManager(線性佈局,類似ListView)
  • GridLayoutManager(網格佈局)
  • StaggeredGridLayoutManager(瀑布流佈局)



Gradle:
compile 'com.android.support:recyclerview-v7:23.1.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=".MainActivity">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

MainActivity:

mRecyclerView = (RecyclerView) this.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new MyRecyclerAdapter(this,lists);
mRecyclerView.setAdapter(adapter);
取得RecylerView物件,設定佈局及adapter。
線性佈局:mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
網格佈局:mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
瀑布流佈局(直向):mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
設定項目動畫為預設:mRecyclerView.setItemAnimator(new DefaultItemAnimator());



MyRecyclerAdapter:
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private List lists;
    private Context context;
    private List heights;
    public MyRecyclerAdapter(Context context,List lists) {
        this.context = context;
        this.lists = lists;
    }    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);

        return viewHolder;
    }
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.mTv.setText(lists.get(position));
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                lists.remove(holder.getLayoutPosition());
                notifyItemRemoved(holder.getLayoutPosition());
                return false;
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"Onclick:"+holder.getLayoutPosition(),Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }
}
class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mTv;
    public MyViewHolder(View itemView) {
        super(itemView);
        mTv = (TextView) itemView.findViewById(R.id.textView);

    }
}
onBindViewHolder:當物件顯示於畫面時被調用,可利用此方法更新該物件之內容。
※使用adapter.notifyDataSetChanged()會重新調用onBindViewHolder。
RecyclerView.Adapter 除了adapter.notifyDataSetChanged()之外還提供幾個函數使用。
notifyItemChanged(int)
notifyItemInserted(int) 
notifyItemRemoved(int)
notifyItemMoved(int, int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)

新增:
lists.add(postion,name);
notifyItemInserted(postion);
將資料插入postion位置,對postion做更新。

刪除:
lists.remove(position);
notifyItemRemoved(position);


LinearLayoutManager

GirdLayoutManager

StaggeredGridLayoutManager

Demo:


沒有留言:

張貼留言