发布于 2017-09-13 20:05:07 | 25 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android RecycleView添加head配置封装的实例的相关资料,这里提供实例帮助大家实现这样的功能,需要的朋友可以参考下

Android RecycleView添加head配置封装的实例

这个是把RecycleView的适配器给封装了,直接调用就可以了,还添加了可以添加head头部功能,很赞的,今天记下来,下次直接用 

实例代码:


package com.wwl.android; 
 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.StaggeredGridLayoutManager; 
import android.view.View; 
import android.view.ViewGroup; 
 
import java.util.ArrayList; 
 
 
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
 
  public static final int TYPE_HEADER = 0; 
  public static final int TYPE_NORMAL = 1; 
 
  private ArrayList<T> mDatas = new ArrayList<>(); 
 
  private View mHeaderView; 
 
  private OnItemClickListener mListener; 
 
  public void setOnItemClickListener(OnItemClickListener li) { 
    mListener = li; 
  } 
 
  public void setHeaderView(View headerView) { 
    mHeaderView = headerView; 
    notifyItemInserted(0); 
  } 
 
  public View getHeaderView() { 
    return mHeaderView; 
  } 
 
  public void addDatas(ArrayList<T> datas) { 
    mDatas.addAll(datas); 
    notifyDataSetChanged(); 
  } 
 
  @Override 
  public int getItemViewType(int position) { 
    if(mHeaderView == null) return TYPE_NORMAL; 
    if(position == 0) return TYPE_HEADER; 
    return TYPE_NORMAL; 
  } 
 
  @Override 
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) { 
    if(mHeaderView != null && viewType == TYPE_HEADER) return new Holder(mHeaderView); 
    return onCreate(parent, viewType); 
  } 
 
 
  @Override 
  public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { 
    if(getItemViewType(position) == TYPE_HEADER) return; 
 
    final int pos = getRealPosition(viewHolder); 
    final T data = mDatas.get(pos); 
    onBind(viewHolder, pos, data); 
 
    if(mListener != null) { 
      viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
          mListener.onItemClick(pos, data); 
        } 
      }); 
    } 
  } 
 
  @Override 
  public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
 
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); 
    if(manager instanceof GridLayoutManager) { 
      final GridLayoutManager gridManager = ((GridLayoutManager) manager); 
      gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
        @Override 
        public int getSpanSize(int position) { 
          return getItemViewType(position) == TYPE_HEADER 
              ? gridManager.getSpanCount() : 1; 
        } 
      }); 
    } 
  } 
 
  @Override 
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) { 
    super.onViewAttachedToWindow(holder); 
    ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); 
    if(lp != null 
        && lp instanceof StaggeredGridLayoutManager.LayoutParams 
        && holder.getLayoutPosition() == 0) { 
      StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; 
      p.setFullSpan(true); 
    } 
  } 
 
  public int getRealPosition(RecyclerView.ViewHolder holder) { 
    int position = holder.getLayoutPosition(); 
    return mHeaderView == null ? position : position - 1; 
  } 
 
  @Override 
  public int getItemCount() { 
    return mHeaderView == null ? mDatas.size() : mDatas.size() + 1; 
  } 
 
  public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType); 
  public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data); 
 
  public class Holder extends RecyclerView.ViewHolder { 
    public Holder(View itemView) { 
      super(itemView); 
    } 
  } 
 
  public interface OnItemClickListener<T> { 
    void onItemClick(int position, T data); 
  } 
} 

  然后在外面自己的适配器中直接继承就可以了


package com.wwl.android; 
 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
 
/** 
 * wwl 
 */ 
public class MyAdapter extends BaseRecyclerAdapter<String> { 
 
  @Override 
  public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) { 
    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); 
    return new MyHolder(layout); 
  } 
 
  @Override 
  public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, String data) { 
    if(viewHolder instanceof MyHolder) { 
      ((MyHolder) viewHolder).text.setText(data); 
    } 
  } 
 
  class MyHolder extends BaseRecyclerAdapter.Holder { 
    TextView text; 
    public MyHolder(View itemView) { 
      super(itemView); 
      text = (TextView) itemView.findViewById(R.id.text); 
    } 
  } 
} 
 

 然后在类中调用,


public class MainActivity extends AppCompatActivity { 
 
  private RecyclerView mRecyclerView; 
  private RecyclerView.LayoutManager mLayoutManager; 
  private MyAdapter mAdapter; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    mRecyclerView = (RecyclerView) findViewById(R.id.list); 
//    mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
    mLayoutManager = new GridLayoutManager(this, 2); 
//    mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
    mRecyclerView.addItemDecoration(new GridItemDecoration(this, true)); 
 
    mAdapter = new MyAdapter(); 
    mRecyclerView.setAdapter(mAdapter); 
    mAdapter.addDatas(generateData()); 
    setHeader(mRecyclerView); 
    mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<String>() { 
      @Override 
      public void onItemClick(int position, String data) { 
        Toast.makeText(MainActivity.this, position + "," + data, Toast.LENGTH_SHORT).show(); 
      } 
    }); 
  } 
 
  private void setHeader(RecyclerView view) { 
    View header = LayoutInflater.from(this).inflate(R.layout.header, view, false); 
     
    mAdapter.setHeaderView(header); 
  } 
 
  private ArrayList<String> generateData() { 
    ArrayList<String> data = new ArrayList<String>() { 
      { 
        for(int i=0;i<21;i++) add("数据" + i); 
      } 
    }; 
    return data; 
  } 
   
} 

 这样就OK啦。 

布局文件:

R.layout.activity_main 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <android.support.v7.widget.RecyclerView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
 
</LinearLayout> 

header.xml 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical"> 
 
  <ImageView 
    android:layout_gravity="center_horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher"/> 
 
</LinearLayout> 

   item.xml 


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:padding="20dp" 
  android:orientation="vertical"> 
 
  <TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="数据"/> 
 
</RelativeLayout> 

以上就是Android RecycleView添加head配置封装的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



相关阅读 :
Android RecycleView添加head配置封装的实例
Android ListView里控件添加监听方法的实例详解
Android OKHTTP的单例和再封装的实例
Android 中在有序广播中添加自定义权限的实例
android实现添加耳机状态图标的方法
Android中用MediaRecorder进行录影的实例代码
android实现蓝牙文件发送的实例代码,支持多种机型
Android截屏保存png图片的实例代码
Android 数据库打包随APK发布的实例代码
Android中截取当前屏幕图片的实例代码
Android 通过当前经纬度获得城市的实例代码
Android NDK开发之:配置环境的详解
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务