android - fragment/activity中使用 recyclerview

访问量: 124

参考:  https://developer.android.com/guide/topics/ui/layout/recyclerview

recyclerview是listview的升级版. 为了提高执行效率和用户体验. 

重点是3个内容: 

1. recyclerview: 最外层的东西,包含了骨架

2. adapter  :  用来把数据 和 recyclerview组合到一起的

3. viewholder: 代表列表中的一行. 

下面是一个例子:

1. 创建一个adapter : 

public class MyOrderBookAdapter extends RecyclerView.Adapter<MyOrderBookAdapter.MyViewHolder>  {

  private String[] myDataSet;

  public static class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView amount;

    public MyViewHolder(TextView amount){
      super(amount);
      this.amount = amount;
    }
  }

  public MyOrderBookAdapter(String[] myDataSet) {
    this.myDataSet = myDataSet;
  }

  /**
   * step1.负责创建list,这个应该是一个
   */
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    TextView v = (TextView) (LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false));
    MyViewHolder holder = new MyViewHolder(v);
    return holder;
  }

  /**
   * step2.负责展示数据
   */
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.amount.setText(this.myDataSet[position]);
  }

  @Override
  public int getItemCount() {
    return myDataSet.length;
  }

}

2. 在fragment, activity中调用:

   // 定义后, 在 oncreate  中调用该方法
    private void initAskOrderBook(){

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        ask_order_book.setLayoutManager(layoutManager);

        String[] data = { "aaa", "bbb", "ccc"};
        RecyclerView.Adapter myAdapter = new MyOrderBookAdapter(data);
        ask_order_book.setAdapter(myAdapter);
    }

3. 创建一个my_text_view.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:textSize="18sp"
    android:text="this is my button!"
    >
</TextView>

运行即可. 

下面是adapter 加载XML的情况: 

public class MyOrderBookAdapter extends RecyclerView.Adapter<MyOrderBookAdapter.MyViewHolder> {

  // step3. 这里必定是一个list
  private List<OrderBookBean.ContentBean.DataBean.AskOrderBean> ask_orders;


  /**
   * step1.负责创建list
   */
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RelativeLayout orderLayout = (RelativeLayout) (LayoutInflater.from(parent.getContext())
            .inflate(R.layout.deep_sell_progress, parent, false));

    View view = View.inflate(parent.getContext(), R.layout.deep_sell_progress, null);
    return new MyViewHolder(view);
  }

  /**
   * step2.负责展示数据
   */
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {

    OrderBookBean.ContentBean.DataBean.AskOrderBean askOrder = this.ask_orders.get(position);
    holder.ask_order_book.setProgress(position);
    holder.ask_order_amount.setText(askOrder.amount);
    holder.ask_order_price.setText(askOrder.price);
  }

  @Override
  public int getItemCount() {
    return ask_orders == null ? 0 : this.ask_orders.size();
  }

  public static class MyViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.ask_order_book)
    ProgressBar ask_order_book;

    @BindView(R.id.ask_order_price)
    TextView ask_order_price;

    @BindView(R.id.ask_order_amount)
    TextView ask_order_amount;

    @BindView(R.id.ask_order_index)
    TextView ask_order_index;

    public MyViewHolder(View view){
      super(view);
      ButterKnife.bind(this, view);
    }
  }

  public MyOrderBookAdapter(List<OrderBookBean.ContentBean.DataBean.AskOrderBean> ask_orders) {
    this.ask_orders = ask_orders;
  }

}

订阅/RSS Feed

Subscribe

分类/category