1. 程式人生 > >android開發:RecyclerView的簡單使用

android開發:RecyclerView的簡單使用

簡介

  • 本文內容是記錄如何使用RecyclerView

注意:具體程式碼中的一下變數是從一個專案中複製過來的,如果要在自己專案中使用,需要修改成自己專案需要的變數。

第一步:專案中app目錄的build.gradle檔案新增依賴

  compile 'com.android.support:recyclerview-v7:25.3.1'
  compile 'com.zhy:base-rvadapter:3.0.3'

第二步:新增RecyclerView佈局檔案


    <android.support.v7.widget.RecyclerView
android:id="@+id/rcv_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"/>

第三步:介面卡佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="fill_parent" android:layout_height="wrap_content" android:clickable="false" android:focusable="false" android:orientation="horizontal" >
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false"
android:focusable="false" android:gravity="center_horizontal" android:orientation="vertical" >
<ImageView android:id="@+id/order_item_pic_iv" android:layout_width="75dp" android:layout_height="75dp" android:layout_margin="5dp" android:scaleType="fitCenter" android:src="@drawable/image_loading" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" > <TextView android:id="@+id/book_item_price_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥10" android:textColor="@color/titlebar" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/order_item_pic_iv" android:text="/份" android:textColor="@android:color/darker_gray" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:clickable="false" android:focusable="false" android:orientation="vertical" > <TextView android:id="@+id/order_item_name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:padding="5dp" android:singleLine="true" android:text="青椒炒蛋" android:textSize="18sp" /> <TextView android:id="@+id/order_item_desc_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:text="經典菜式,美味可口" android:textColor="@android:color/darker_gray" /> <TextView android:id="@+id/order_item_date_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:padding="5dp" android:singleLine="true" android:text="2016-01-04 08:25:00 至2016-01-07 09:00:00" android:textColor="@android:color/darker_gray" /> <TextView android:id="@+id/order_item_limit_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:text="5/10" android:textColor="@android:color/darker_gray" /> <TextView android:id="@+id/book_item_sure_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/book_submit_btn_selector" android:clickable="true" android:focusable="true" android:gravity="center" android:text="預定" android:textColor="@android:color/white" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:clickable="false" android:focusable="false" android:gravity="center_vertical" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:orientation="horizontal" android:visibility="gone" > <ImageButton android:id="@+id/book_item_decrease_ibtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/book_item_decress" android:clickable="true" android:focusable="true" android:padding="10dp" android:scaleType="fitCenter" /> <TextView android:id="@+id/book_item_num_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="1" android:textStyle="bold" /> <ImageButton android:id="@+id/book_item_add_ibtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/book_item_add" android:clickable="true" android:focusable="true" android:padding="10dp" android:scaleType="fitCenter" /> </LinearLayout> </LinearLayout> </LinearLayout>

第四步:介面卡java檔案

public class BookDinnerAdater extends CommonAdapter<Book> {

    private String TAG = BookDinnerAdater.class.getSimpleName();
    private ButtonInterface buttonInterface;
    private MenuSelectInterface menuSelectInterface;
    private MenuCancelInterface menuCancelInterface;

    public BookDinnerAdater(Context context, int layoutId, List<Book> datas) {
        super(context, layoutId, datas);
    }



    /**
     *按鈕點選事件需要的方法
     */
    public void setButtonOnclick(ButtonInterface buttonInterface){
        this.buttonInterface=buttonInterface;
    }
    /**
     *選擇控制元件點選事件需要的方法
     */
    public void setMenuSelectOnclick(MenuSelectInterface menuSelectedInterface){
        this.menuSelectInterface=menuSelectedInterface;
    }
    /**
     *取消控制元件點選事件需要的方法
     */
    public void setMenuCancelOnclick(MenuCancelInterface menuCancelInterface){
        this.menuCancelInterface = menuCancelInterface;
    }

    @Override
    protected void convert(ViewHolder holder,Book book, int position) {

        holder.setText(R.id.order_item_name_tv,book.getName());
//        holder.setText(R.id.order_item_pic_iv,book.getPhoto());
        holder.setText(R.id.order_item_desc_tv,book.getDesc());
        String limit = book.getTotal()+"/"+book.getLimit();
        holder.setText(R.id.order_item_limit_tv,limit);
        holder.setText(R.id.book_item_price_tv,"¥"+book.getPrice());
        String date =  book.getBegindate()+" "+book.getBegintime()+" 至 "+
                book.getEnddate()+" "+book.getEndtime();
        holder.setText(R.id.order_item_date_tv,date);
        TextView book_item_sure_btn = holder.getView(R.id.book_item_sure_btn);
        book_item_sure_btn.setText("預訂");
        book_item_sure_btn.setTextColor(Color.WHITE);
        book_item_sure_btn.setEnabled(true);


        //預訂
        holder.setOnClickListener(R.id.book_item_sure_btn, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    /**
     * 按鈕點選事件對應的介面
     */
    public interface ButtonInterface{
        void onclick(View view, int position);
    }
    /**
     *  取消點選事件對應的介面
     */
    public interface MenuSelectInterface{
        void onclick(View view, int position);
    }
    /**
     * 選擇控制元件點選事件對應的介面
     */
    public interface MenuCancelInterface{
        void onclick(View view, int position);
    }


}

設定RecyclerView關聯介面卡

private List<T> mData = new ArrayList<T>();
private void initData() {
        mRecyclerView.setHasFixedSize(true);
        //設定佈局管理器
        linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //新增分割線
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new BookDinnerAdater(this, R.layout.list_item_menu, mData);
        mAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder,  int position) {
;

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });



//        initHeaderAndFooter();

        mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                Log.i(Tag,"onLoadMoreRequested");


            }
        });

        mRecyclerView.setAdapter(mAdapter);

        mAdapter.notifyDataSetChanged();
    }
    /**
    * 初始化頭部,僅供參考
    */
     private void initHeaderAndFooter() {
        //裝飾者模式
        mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);

        View header = LayoutInflater.from(getActivity()).inflate(R.layout.item_course, mRecyclerView, false);
        TextView tv_section = (TextView) header.findViewById(R.id.tv_section);
        TextView tv_course_name = (TextView) header.findViewById(R.id.tv_course_name);
        TextView tv_classes_name = (TextView) header.findViewById(R.id.tv_classes_name);
        TextView tv_teacher_name = (TextView) header.findViewById(R.id.tv_teacher_name);
        TextView tv_course_state = (TextView) header.findViewById(R.id.tv_course_state);
        tv_section.setText("節次");
        tv_course_name.setText("課程");
        tv_classes_name.setText("班級");
        tv_teacher_name.setText("教師");
        tv_course_state.setText("狀態");

        mHeaderAndFooterWrapper.addHeaderView(header);

    }