版权声明:本文为博主原创文章,未经博主允许不得转载。
系列教程:Android开发之从零开始系列大家要是看到有错误的地方或者有啥好的建议,欢迎留言评论
前言:项目开发中难免会敲很多重复的代码,尤其是像 Adapter(适配器)这种使用频率高且格式基本一致的类,这篇博客将以 RecyclerViewAdapter为例教大家如何自定义自己的模板代码实现一键生成
一般RecyclerViewAdapter中会包含初始化方法,ViewHolder,设置列表项点击或长按的接口等等,下面是RecyclerViewAdapter的一个例子
package com.test.bauztest.BaseRecyclerViewAdapter;//你的包名
//各种类的导入
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import java.util.Map;
//类的File Header
/**
* Created by anlia on 2017/10/30.
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private Context context;
private List<Map<String,String>> list;//声明一个传参类型为Map<String,String>的list
private OnItemClickListener mOnItemClickListener;
class ViewHolder extends RecyclerView.ViewHolder{
// TODO: 声明组件
public ViewHolder(View view) {
super(view);
// TODO: 注册组件,view.findViewById(R.id.xxx)
}
}
public MyRecyclerViewAdapter(Context context,List<Map<String,String>> list) {
this.context = context;
this.list = list;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view , int position);
}
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
this.mOnItemClickListener = mOnItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// TODO: 为对应itemViewId赋值,例:R.layout.xxx
int itemViewId = 0;
ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(itemViewId, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// TODO: 绑定组件的事件
// 如果设置了回调,则设置点击事件
if (mOnItemClickListener != null)
{
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
}
基本每个Adapter都包含上面这些内容,经常复制粘贴然后再去改类名是非常影响开发效率的一件事,我们可以利用Android Studio中设置模板代码的功能来减轻我们的负担,进入模板代码设置有两种方法
- 点击File → Settings → Editor → File and Code Templates,然后点击新增,编写我们的模板代码即可,如图
- 还有另外一种方法更加快捷,点击File → New → Edit File Templates
因为模板代码要有通用性,所以包名、类名以及List的传参我们都不能写死,上面的Adapter例子需要略微修改,注意模板代码中修改的地方
//自动生成包名
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
//用默认的File Header即可
#parse("File Header.java")
public class ${NAME}<E> extends RecyclerView.Adapter<${NAME}.ViewHolder> {//包名也需要自动生成,同时注意List传参为泛型
private Context context;
private List<E> list;//数据集合
private OnItemClickListener mOnItemClickListener;
class ViewHolder extends RecyclerView.ViewHolder{
// TODO: 声明组件
public ViewHolder(View view) {
super(view);
// TODO: 注册组件,view.findViewById(R.id.xxx)
}
}
public ${NAME}(Context context,List<E> list) {
this.context = context;
this.list = list;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view , int position);
}
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
this.mOnItemClickListener = mOnItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// TODO: 为对应itemViewId赋值,例:R.layout.xxx
int itemViewId = 0;
ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(itemViewId, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// TODO: 绑定组件的事件
// 如果设置了回调,则设置点击事件
if (mOnItemClickListener != null)
{
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
}
模板代码设置好后,我们只需要简单几步即可自动生成RecyclerViewAdapter代码
至此本篇教程到此结束,如果大家看了感觉还不错麻烦点个赞,你们的支持是我最大的动力~