版權(quán)聲明:本文為博主原創(chuàng)文章告唆,未經(jīng)博主允許不得轉(zhuǎn)載镰官。
系列教程:Android開發(fā)之從零開始系列大家要是看到有錯(cuò)誤的地方或者有啥好的建議,歡迎留言評論
前言:項(xiàng)目開發(fā)中難免會敲很多重復(fù)的代碼蜈彼,尤其是像 Adapter(適配器)這種使用頻率高且格式基本一致的類案狠,這篇博客將以 RecyclerViewAdapter為例教大家如何自定義自己的模板代碼實(shí)現(xiàn)一鍵生成
一般RecyclerViewAdapter中會包含初始化方法味廊,ViewHolder,設(shè)置列表項(xiàng)點(diǎn)擊或長按的接口等等雷猪,下面是RecyclerViewAdapter的一個(gè)例子
package com.test.bauztest.BaseRecyclerViewAdapter;//你的包名
//各種類的導(dǎo)入
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;//聲明一個(gè)傳參類型為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: 為對應(yīng)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: 綁定組件的事件
// 如果設(shè)置了回調(diào)睛竣,則設(shè)置點(diǎn)擊事件
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();
}
}
基本每個(gè)Adapter都包含上面這些內(nèi)容,經(jīng)常復(fù)制粘貼然后再去改類名是非常影響開發(fā)效率的一件事求摇,我們可以利用Android Studio中設(shè)置模板代碼的功能來減輕我們的負(fù)擔(dān)射沟,進(jìn)入模板代碼設(shè)置有兩種方法
- 點(diǎn)擊File → Settings → Editor → File and Code Templates,然后點(diǎn)擊新增月帝,編寫我們的模板代碼即可躏惋,如圖
- 還有另外一種方法更加快捷,點(diǎn)擊File → New → Edit File Templates
因?yàn)槟0宕a要有通用性嚷辅,所以包名簿姨、類名以及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;
//用默認(rèn)的File Header即可
#parse("File Header.java")
public class ${NAME}<E> extends RecyclerView.Adapter<${NAME}.ViewHolder> {//包名也需要自動生成扁位,同時(shí)注意List傳參為泛型
private Context context;
private List<E> list;//數(shù)據(jù)集合
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: 為對應(yīng)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: 綁定組件的事件
// 如果設(shè)置了回調(diào)准潭,則設(shè)置點(diǎn)擊事件
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();
}
}
模板代碼設(shè)置好后,我們只需要簡單幾步即可自動生成RecyclerViewAdapter代碼
至此本篇教程到此結(jié)束域仇,如果大家看了感覺還不錯(cuò)麻煩點(diǎn)個(gè)贊刑然,你們的支持是我最大的動力~