亚洲AVI,黑人巨茎大战欧美白妇,初高中生洗澡自慰高清网站,欧美日韩无砖专区一中文字

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)

RecyclerView怎么在Android中使用

RecyclerView怎么在Android中使用?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、筠連網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、筠連網(wǎng)絡(luò)營(yíng)銷、筠連企業(yè)策劃、筠連品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供筠連建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com

首先ListView與RecyclerView兩者非常相似,兩者提供view都是依賴適配器。只不過(guò)就是5.0版本推出RecyclerView后,Google將adapter和viewHolder做了一系列的優(yōu)化和封裝。不像之前為了復(fù)用Listview里面的converView,要類似在getView里面實(shí)現(xiàn)下列的代碼:

RecyclerView怎么在Android中使用

上面代碼看起來(lái)挺眼熟吧~

二、對(duì)比RecyclerView,google進(jìn)行的優(yōu)化

在RecyclerView依賴的適配器中,無(wú)論是適配器還是ViewHolder,從源碼我們可以看出,都存在RecyclerView的匿名內(nèi)部類。相對(duì)于Listview,RecyclerView內(nèi)置了多級(jí)緩存、RecyclerViewPool(從線程的角度,可以理解成類似線程池的東西,即多個(gè)RecyclerView可以公用一個(gè)view)、ViewHolder(已經(jīng)實(shí)現(xiàn)了復(fù)用,相對(duì)于Listview的BaseAdapter中g(shù)etView方法需要開發(fā)者自己引入復(fù)用問(wèn)題方便很多)等等。這里我們簡(jiǎn)單說(shuō)下兩個(gè)方法:

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
public void onBindViewHolder(ViewHolder holder, int position)

在以前的BaseAdapter中,所有視圖加載、數(shù)據(jù)綁定以及復(fù)用,都需要我們直接在getView里面進(jìn)行操作。onCreateViewHolder負(fù)責(zé)視圖加載并且內(nèi)部完成復(fù)用,onBindViewHolder負(fù)責(zé)數(shù)據(jù)綁定并且內(nèi)部完成一系列的緩存機(jī)制。這里滿足了視圖層與邏輯層的分離,典型的mvp模式。

三、RecyclerView的頭部與尾部實(shí)現(xiàn)

RecyclerView不像ListView擁有addHeaderView()與addFooterView()的方法簡(jiǎn)單添加頭部尾部即可,而且RecyclerView也沒(méi)有像ListView的列表點(diǎn)擊監(jiān)聽方法(setItemOnclickListener),這里我也不明白為什么官方會(huì)取消了這些獨(dú)有的屬性,不過(guò)我們依然可以在onBindViewHolder方法中進(jìn)行事件綁定!

具體頭部與尾部實(shí)現(xiàn)方法,這里有個(gè)訣竅,這里先看一個(gè)方法:

public int getItemViewType(int position)

getItemViewType方法是在執(zhí)行onCreateViewHolder(ViewGroup parent, int viewType)前回調(diào)用viewType,目的是為了根據(jù)viewType不同創(chuàng)建不同的視圖。我們可以通過(guò)在onCreateViewHolder創(chuàng)建視圖的時(shí)候,對(duì)viewType進(jìn)行判斷,如果添加了頭部,在position = 0的時(shí)候回調(diào)頭部的viewType給onCreateViewHolder,從而創(chuàng)建頭部。尾部創(chuàng)建方法于此類同,直接看下代碼,適配器的實(shí)現(xiàn):

package cn.wsy.recyclerdemo;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wsy on 2016/8/4.
 */
public class MyAdapter extends RecyclerView.Adapter {

  private RecyclerView mRecyclerView;

  private List data = new ArrayList<>();
  private Context mContext;

  private View VIEW_FOOTER;
  private View VIEW_HEADER;

  //Type
  private int TYPE_NORMAL = 1000;
  private int TYPE_HEADER = 1001;
  private int TYPE_FOOTER = 1002;

  public MyAdapter(List data, Context mContext) {
    this.data = data;
    this.mContext = mContext;
  }

  @Override
  public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_FOOTER) {
      return new MyHolder(VIEW_FOOTER);
    } else if (viewType == TYPE_HEADER) {
      return new MyHolder(VIEW_HEADER);
    } else {
      return new MyHolder(getLayout(R.layout.item_list_layout));
    }
  }

  @Override
  public void onBindViewHolder(MyHolder holder, int position) {
    if (!isHeaderView(position) && !isFooterView(position)) {
      if (haveHeaderView()) position--;
      TextView content = (TextView) holder.itemView.findViewById(R.id.item_content);
      TextView time = (TextView) holder.itemView.findViewById(R.id.item_time);
      content.setText(data.get(position));
      time.setText("2016-1-1");
    }
  }

  @Override
  public int getItemCount() {
    int count = (data == null ? 0 : data.size());
    if (VIEW_FOOTER != null) {
      count++;
    }

    if (VIEW_HEADER != null) {
      count++;
    }
    return count;
  }

  @Override
  public int getItemViewType(int position) {
    if (isHeaderView(position)) {
      return TYPE_HEADER;
    } else if (isFooterView(position)) {
      return TYPE_FOOTER;
    } else {
      return TYPE_NORMAL;
    }
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    try {
      if (mRecyclerView == null && mRecyclerView != recyclerView) {
        mRecyclerView = recyclerView;
      }
      ifGridLayoutManager();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private View getLayout(int layoutId) {
    return LayoutInflater.from(mContext).inflate(layoutId, null);
  }

  public void addHeaderView(View headerView) {
    if (haveHeaderView()) {
      throw new IllegalStateException("hearview has already exists!");
    } else {
      //避免出現(xiàn)寬度自適應(yīng)
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      headerView.setLayoutParams(params);
      VIEW_HEADER = headerView;
      ifGridLayoutManager();
      notifyItemInserted(0);
    }

  }

  public void addFooterView(View footerView) {
    if (haveFooterView()) {
      throw new IllegalStateException("footerView has already exists!");
    } else {
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      footerView.setLayoutParams(params);
      VIEW_FOOTER = footerView;
      ifGridLayoutManager();
      notifyItemInserted(getItemCount() - 1);
    }
  }

  private void ifGridLayoutManager() {
    if (mRecyclerView == null) return;
    final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
          ((GridLayoutManager) layoutManager).getSpanSizeLookup();
      ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
          return (isHeaderView(position) || isFooterView(position)) ?
              ((GridLayoutManager) layoutManager).getSpanCount() :
              1;
        }
      });
    }
  }

  private boolean haveHeaderView() {
    return VIEW_HEADER != null;
  }

  public boolean haveFooterView() {
    return VIEW_FOOTER != null;
  }

  private boolean isHeaderView(int position) {
    return haveHeaderView() && position == 0;
  }

  private boolean isFooterView(int position) {
    return haveFooterView() && position == getItemCount() - 1;
  }


  public static class MyHolder extends RecyclerView.ViewHolder {

    public MyHolder(View itemView) {
      super(itemView);
    }
  }

}

四、實(shí)現(xiàn)方法

簡(jiǎn)單的初始化RecycerView,以及設(shè)置適配器,如下:

  private void initRecyc() {
//    mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new MyAdapter(data, this);
    mRecyclerView.setAdapter(adapter);

    adapter.addFooterView(LayoutInflater.from(this).inflate(R.layout.item_footer_layout,null));
    adapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.item_header_layout,null));
  }

五、注意的問(wèn)題

筆者在添加頭部尾部的時(shí)候,發(fā)現(xiàn)在配置RecyclerView,如果模式是配置GridLayoutManager的時(shí)候,發(fā)現(xiàn)頭部會(huì)跑到第一格,也就是不是自己想要獨(dú)立一行的效果,這里貼上關(guān)鍵代碼,可以解決(簡(jiǎn)單數(shù)學(xué)問(wèn)題啦哈~):

  private void ifGridLayoutManager() {
    if (mRecyclerView == null) return;
    final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
          ((GridLayoutManager) layoutManager).getSpanSizeLookup();
      ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
          return (isHeaderView(position) || isFooterView(position)) ?
              ((GridLayoutManager) layoutManager).getSpanCount() :
              1;
        }
      });
    }
  }

看完上述內(nèi)容,你們掌握RecyclerView怎么在Android中使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


文章標(biāo)題:RecyclerView怎么在Android中使用
URL分享:http://news.spvevtbd.cn/article/ggoghh.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP