框架整理系列十(BaseRecycleViewAdapter)

RecycleView 分割线

    compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'

使用:

recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
        .drawable(R.drawable.sample)
        .size(15)
        .build());

recyclerView.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.RED)
                .sizeResId(R.dimen.divider)
                .marginResId(R.dimen.leftmargin, R.dimen.rightmargin)
                .build());

Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));
recyclerView.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this).paint(paint).build());

参考:
https://github.com/yqritc/RecyclerView-FlexibleDivider

RecycleView Adapter

引用


    compile 'com.android.support:design:25.0.1'

1 .BaseJSonArrayRecycleViewAdapter.java

/**
 * 作者:周洲 on 2017/4/16 21:23
 * 

* 邮箱:[email protected] */ import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * 作者:周洲 on 2017/3/8 09:38 *

* 邮箱:[email protected] */ public abstract class BaseRecycleViewAdapter extends RecyclerView.Adapter { private OnItemClickListener onItemClickListener; private OnItemClickListener onItemLongClickListener; private List datalist = new ArrayList<>(); @Override public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false); return new SuperViewHolder(view); } @Override public void onBindViewHolder(SuperViewHolder holder, final int position) { onBindItemHolder(holder, position); if (onItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(v, position); } }); } if (onItemLongClickListener != null) { holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemLongClickListener.onItemClick(v, position); return false; } }); } } @Override public int getItemCount() { return datalist.size(); } /** * 添加 * * @param datalist */ public void setDatalist(Collection datalist) { this.datalist.clear(); this.datalist.addAll(datalist); notifyDataSetChanged(); } public List getDatalist() { return datalist; } public abstract int getLayoutId(); public abstract void onBindItemHolder(SuperViewHolder holder, int position); public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public void setOnItemLongClickListener(OnItemClickListener onItemLongClickListener) { this.onItemLongClickListener = onItemLongClickListener; } /** * ViewHolder基类 */ class SuperViewHolder extends RecyclerView.ViewHolder { private SparseArray views; public SuperViewHolder(View itemView) { super(itemView); this.views = new SparseArray<>(); } @SuppressWarnings("unchecked") public T getView(int viewId) { View view = views.get(viewId); if (view == null) { view = itemView.findViewById(viewId); views.put(viewId, view); } return (T) view; } } public interface OnItemClickListener { void onItemClick(View view, int position); } }``` *2. OnItemClickListener.java*

import android.view.View;

/**

  • Click
    */
    public interface OnItemClickListener {
    void onItemClick(View view, int position);
    }

*3. BaseRecycleViewAdapter.java* 待完善

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.interfaces.OnItemClickListener;

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

/**

public abstract class BaseRecycleViewAdapter extends RecyclerView.Adapter {
private OnItemClickListener onItemClickListener;
private OnItemClickListener onItemLongClickListener;
private List datalist = new ArrayList<>();
@Override
public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(),parent,false);
return new SuperViewHolder(view);
}

@Override
public void onBindViewHolder(SuperViewHolder holder, final int position) {
    onBindItemHolder(holder,position);
    if(onItemClickListener!=null){
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,position);
            }
        });
    }
    if (onItemLongClickListener!=null) {
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemLongClickListener.onItemClick(v,position);
                return false;
            }
        });
    }
}

@Override
public int getItemCount() {
    return datalist.size();
}

/**
 * 添加
 * @param datalist
 */
public void setDatalist(Collection datalist) {
    this.datalist.clear();
    this.datalist.addAll(datalist);
    notifyDataSetChanged();
}


public List getDatalist(){
    return  datalist;
}

public abstract int getLayoutId();

public abstract void onBindItemHolder(SuperViewHolder holder, int position);


public void setOnItemClickListener(OnItemClickListener onItemClickListener){
    this.onItemClickListener = onItemClickListener;
}

public void setOnItemLongClickListener(OnItemClickListener onItemLongClickListener){
    this.onItemLongClickListener  = onItemLongClickListener;
}

}


*3. 调用*

import android.widget.TextView;

import com.lihua.oa.R;
import com.lihua.oa.base.BaseJSonArrayRecycleViewAdapter;
import com.lihua.oa.base.SuperViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

/**

public class NonghuInfoRecycleAdapter extends BaseJSonArrayRecycleViewAdapter {

@Override
public int getLayoutId() {
    return R.layout.nonghu_list_item;
}

@Override
public void onBindItemHolder(SuperViewHolder holder, int position) {
    try {
        JSONObject jsonObject = getDataList().getJSONObject(position);
        ((TextView)holder.getView(R.id.dataInfo)).setText(jsonObject.getString("infodata"));
        ((TextView)holder.getView(R.id.dataName)).setText(jsonObject.getString("infoname"));
        ((TextView)holder.getView(R.id.nick)).setText(jsonObject.getString("nick"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

福利:
![](http://upload-images.jianshu.io/upload_images/3492461-2a41c6f31b34706e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(框架整理系列十(BaseRecycleViewAdapter))