一、adapter
package com.example.zyl.myrecyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by zyl on 17-7-19.
*/
public class ResolveInfoAdapter extends
RecyclerView.Adapter<ResolveInfoAdapter.ViewHolder>{
private LayoutInflater mInflater;
private List<String> mDatas;
public ResolveInfoAdapter(Context context, List<String> datas){
mInflater = LayoutInflater.from(context);
mDatas = datas;
}
public interface OnItemClickListener{
void onItemClick(ViewHolder view,int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View arg0){
super(arg0);
}
ImageView mImg;
TextView mTxt;
}
@Override//什么时候调用呢?
public ResolveInfoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.gallery_item,parent,false);
ResolveInfoAdapter.ViewHolder viewHolder = new ResolveInfoAdapter.ViewHolder(view);
viewHolder.mImg = (ImageView) view.findViewById(R.id.id_index_gallery_item_image);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
//设置监听事件
if(onItemClickListener != null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(holder,position);
}
});
}
}
@Override
public int getItemCount() {
return mDatas.size();
}
}
二、RecycleView
package com.example.zyl.myrecyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;
import android.view.View;
import org.w3c.dom.Attr;
/**
* Created by zyl on 16-11-15.
*/
public class MyRecyclerview extends RecyclerView{
/**
* 记录当前第一个View
*/
private View mCurrentView;
private OnItemScrollChangeListener mItemScrollChangeListener;
public void setOnItemScrollChangeListener(
OnItemScrollChangeListener mItemScrollChangeListener)
{
this.mItemScrollChangeListener = mItemScrollChangeListener;
}
public interface OnItemScrollChangeListener
{
void onChange(View view, int position);
}
public MyRecyclerview(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
super.onLayout(changed, l, t, r, b);
mCurrentView = getChildAt(0);
if (mItemScrollChangeListener != null)
{
mItemScrollChangeListener.onChange(mCurrentView,
getChildPosition(mCurrentView));
}
}
@Override
public void onScrollStateChanged(int arg0)
{
}
/**
*
* 滚动时,判断当前第一个View是否发生变化,发生才回调
*/
@Override
public void onScrolled(int arg0, int arg1)
{
View newView = getChildAt(0);
if (mItemScrollChangeListener != null)
{
if (newView != null && newView != mCurrentView)
{
mCurrentView = newView ;
mItemScrollChangeListener.onChange(mCurrentView,
getChildPosition(mCurrentView));
}
}
}
}