Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 新控件RecyclerView

Android 新控件RecyclerView

編輯:關於Android編程

RecyclerView 是 android-support-v7-21 版本中新增的一個 Widgets。我們可以使用RecyclerView非常簡單的實現橫向豎向的ListView.GridView.以及瀑布流的效果.先看一下項目的實現的效果這裡寫圖片描述

是不是很炫?使用RecyclerView可以非常簡單的實現.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPlJlY3ljbGVyVmlldyDM2NDUPGJyIC8+DQoxLrK7udjQxEl0ZW3Kx7fxz9TKvtTa1f3It7XEzrvWw9LUvLDI57rOz9TKvjxiciAvPg0KMi6yu7nY0MRpdGVt1q685Mjnus631rjuPGJyIC8+DQozLrK7udjXokl0ZW3U9rzT0+vJvrP9tcS2r7ut0Ke5+zxiciAvPg0KNC7L/L32vfbWu8rHudjXosjnus672MrVus24tNPDaXRlbTxiciAvPg0KzPmz9rK/t9a0+sLro7o8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;"> mRecyclerView = (RecyclerView) findViewById(R.id.rv); //設置它的Adapter myRecyclerViewListAdapater = new MyRecyclerViewListAdapater(this,sourceDatas); mRecyclerView.setAdapter(myRecyclerViewListAdapater); //設置它的LayoutManage , 通過 LayoutManager去設置它的顯示方式 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecyclerView.setLayoutManager(linearLayoutManager); //設置分割線 // dividerItemDecoration = new DividerItemDecoration(this,LinearLayoutManager.VERTICAL); // mRecyclerView.addItemDecoration(dividerItemDecoration); //設置增加刪除Item的動畫 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); myRecyclerViewListAdapater.setOnItemClickListener(new MyRecyclerViewListAdapater.OnItemClickListener() { @Override public void onItemClickListener(View view, int position) { Toast.makeText(MainActivity.this,click : + position,Toast.LENGTH_SHORT).show(); } @Override public boolean onLongItemClickListener(View view, int position) { myRecyclerViewListAdapater.deleteItem(position); return false; } });

Adapter代碼

public class MyRecyclerViewListAdapater extends RecyclerView.Adapter {

    private Context context;
    protected List sourceDatas;
    private LayoutInflater layoutInflater;
    public OnItemClickListener mOnItemClickListener;

    public MyRecyclerViewListAdapater(Context context,List sourceDatas){
        this.context = context;
        this.sourceDatas = sourceDatas;
        layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public MyRecyclerViewListAdapater.MyListHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.item,viewGroup,false);
        MyListHolder myListHolder = new MyListHolder(view);

        return myListHolder;
    }

    @Override
    public void onBindViewHolder(final MyListHolder viewHolder, final int i) {
        viewHolder.textView.setText(sourceDatas.get(i));
        itemClick(viewHolder);
    }

    /**
     * ItemView點擊事件
     */
    protected void itemClick(final MyListHolder viewHolder){

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != mOnItemClickListener){
                    int postion = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClickListener(viewHolder.itemView,postion);
                }
            }
        });

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(null != mOnItemClickListener){
                    int position = viewHolder.getLayoutPosition();
                    return mOnItemClickListener.onLongItemClickListener(viewHolder.itemView,position);
                }
                return false;
            }
        });
    }

    /**
     * 方法返回item的數量
     * */
    @Override
    public int getItemCount() {
        return sourceDatas.size();
    }

    /**
     * 添加item
     */
    public void addItem(int position){
        sourceDatas.add(position,Add Item);
        notifyItemInserted(position);
    }

    /**
     * 刪除Item
     */
    public void deleteItem(int positon){
        sourceDatas.remove(positon);
        notifyItemRemoved(positon);
    }

    /**
     * 設置item點擊接口
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }


    /**
     * Recycler本身並沒有給我們提供ItemClick事件,這裡需要我們自己實現這個接口
     * 點擊接口
     * */
    public interface OnItemClickListener{
        //點擊事件
        public void onItemClickListener(View view,int position);
        //長按事件
        public boolean onLongItemClickListener(View view,int position);

    }


    class MyListHolder extends RecyclerView.ViewHolder{

        TextView textView;

        //繼承 RecyclerView.ViewHolder 必須要實現它的構造方法
        //可以看到在它的構造方法接收到一個View的參數,這個View就是我們創建的ItemView
        public MyListHolder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.tv_list_item);
        }
    }

}

布局:

 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved