欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android RecyclerView使用方法詳解

 更新時(shí)間:2017年07月27日 15:46:55   作者:寶寶寶的博客  
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Android RecyclerView使用方法,供大家參考,具體內(nèi)容如下

1、RecyclerView 是在Android support - v7 里面提供的 新的列表組件,用來替代傳統(tǒng)的ListView。

. 要使用RecyclerView 需要給我工程添加 support:recycle-v7 的支持: app 右鍵 - Open Module Settings - Dependencies(依賴項(xiàng)) - 點(diǎn) + 號(hào) - 添加一個(gè)庫 upport:recycle-v7  - 代碼實(shí)現(xiàn)

. 給TextView 呈現(xiàn)數(shù)據(jù)

public class MainActivity extends AppCompatActivity {
  //創(chuàng)建RecyclerView
  private RecyclerView recyclerView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    recyclerView = new RecyclerView(this);
    //把它當(dāng)做Activity的內(nèi)容布局
    setContentView(recyclerView);

    //設(shè)置RecyclerView的布局
    recyclerView.setLayoutManager(newLinearLayoutManager(this));

    //為 RecyclerView填充內(nèi)容 - 創(chuàng)建一個(gè) Adapter - 需要重寫三個(gè)函數(shù)
    recyclerView.setAdapter(newRecyclerView.Adapter() {

      //首先自定義一個(gè)類繼承RecyclerView.ViewHolder 實(shí)現(xiàn)構(gòu)造函數(shù)
      class ViewHolerextends RecyclerView.ViewHolder{

        // 在ViewHolder 里面綁定子對(duì)象的視圖
        private TextViewtv;

        public ViewHoler(TextView itemView) {
          super(itemView);
          tv = itemView;//通過這種方式 TextView就可以跟ViewHolder進(jìn)行關(guān)聯(lián)
        }

        //在外界公開一個(gè)函數(shù) getTV() ,用它來返回這個(gè)TextView
        public TextView getTV(){
          return tv;
        }
      }
      //創(chuàng)建 ViewHolder的方法
      @Override
      public RecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {

        return new ViewHoler(newTextView(parent.getContext()));
      }
      //onBindViewHolder 可以對(duì)TextView進(jìn)行賦值, - 在外界進(jìn)行配置
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, intposition) {
        ViewHoler vh = (ViewHoler) holder;
        vh.getTV().setText("Item " + position);
      }
      //獲取RecyclerView子對(duì)象的數(shù)量
      @Override
      public int getItemCount() {
        return 1000;
      }
    });
  }
}

. 用數(shù)組 承載 展示數(shù)據(jù)

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, intposition) {
ViewHoler vh = (ViewHoler) holder;
vh.getTV().setText(data[position]);
}

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

    /*寫一個(gè)數(shù)組呈現(xiàn)出來 從網(wǎng)絡(luò)一系列的途徑獲取的數(shù)據(jù)都是數(shù)組呈現(xiàn)出來*/
    private String[] data = new String[]{"hello","wang","xiaobao"};
  });
}

2、使用資源文件自定義列表項(xiàng)

. 因?yàn)槲覀冊(cè)谥笆褂米远x列表項(xiàng)的方式直接寫程序,在很多時(shí)候你會(huì)發(fā)現(xiàn)如果直接在程序里面寫界面,最終修改是非常麻煩的,所以我們要學(xué)會(huì)使用資源文件來配置。

. 新建一個(gè)資源文件   layout - New - Layout resource file - list_cell - 添加兩個(gè)TextView

然后

class MyAdapter extends RecyclerView.Adapter {

  /* new RecyclerView.Adapter() 可以轉(zhuǎn)移到單獨(dú)的文件里去
   * 在Adapter()內(nèi)部: 點(diǎn)擊右鍵 - Refactor(重構(gòu)) - Move -移到一個(gè)類里面去.
   * 它會(huì)全自動(dòng)的把匿名類 提取成一個(gè)內(nèi)部類。
   *
   * 之后再繼續(xù)移動(dòng)到一個(gè)單獨(dú)的文件里面。
   */
  class ViewHolerextends RecyclerView.ViewHolder {
    private Viewroot; //暫無意義,參考上面案例,可以實(shí)現(xiàn)與外界連接
    private TextView tvTitle,tvContent;

    public ViewHoler(View root) {
      super(root);
      //我們知道傳進(jìn)來的布局就是list_cell;創(chuàng)建之后就可以獲取到這兩個(gè)控件。
      tvTitle = root.findViewById(R.id.tv_title);
      tvContent = root.findViewById(R.id.tv_content);
    }
    //需要添加兩個(gè)get方法被外界訪問到的
    public TextViewgetTvTitle() {
      return tvTitle;
    }

    public TextViewgetTvContent() {
      return tvContent;
    }
  }

  @Override
  public RecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {
    /*
    *之后要?jiǎng)?chuàng)建View,不是new TextView()我們要換種方式,根據(jù)一個(gè)資源進(jìn)行創(chuàng)建,使用LayoutInflater.from
    * LayoutInflater : 布局解釋器,用布局解釋器解析一個(gè)布局,布局首先傳進(jìn)來的是一個(gè)資源,資源就是建立的cell
    * 第二項(xiàng):是我們創(chuàng)建的布局的根對(duì)象,這里傳 null ,通過這種方式我們就創(chuàng)建了這種布局。然后獲取控件
    */
    return new ViewHoler(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell,null));
  }

  @Override//分別來進(jìn)行配置 //我們來創(chuàng)建一個(gè)數(shù)據(jù)對(duì)象。創(chuàng)建構(gòu)造函數(shù)
  public void onBindViewHolder(RecyclerView.ViewHolder holder, intposition) {
    ViewHoler vh = (ViewHoler) holder;
    //首先獲取到這些數(shù)據(jù)
    Cell_Data cd = data[position];
    vh.getTvTitle().setText(cd.title);
    vh.getTvContent().setText(cd.title);
  }

  //創(chuàng)建完對(duì)象之后,它是一個(gè)CellData[]
  private Cell_Data[]data = new Cell_Data[]{newCell_Data("王小寶","hhh"),newCell_Data("新聞","這個(gè)新聞不錯(cuò)")};

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

//創(chuàng)建一個(gè)數(shù)據(jù)對(duì)象的
//列表項(xiàng)數(shù)據(jù)
public class Cell_Data {

  public Cell_Data(String title,String content) {
    this.title= title;
    this.content = content;
  }

  public String title = "title";
  public String content = "content";
}

3、RecyclerView 的布局樣式

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  recyclerView = new RecyclerView(this);
  setContentView(recyclerView);
  //true ;是否翻轉(zhuǎn) 可以左右拖動(dòng) - 線性布局
  recyclerView.setLayoutManager(newLinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true));
  //創(chuàng)建一個(gè)表格布局默認(rèn)是垂直方向的可以上下 
  recyclerView.setLayoutManager(newGridLayoutManager(this,3));

  recyclerView.setAdapter(newMyAdapter());

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論