RecyclerView的基本用法二--滚动时RecyclerView之外的布局联动

一:布局文件activity_main.xml




    

        
    

    

二:RecyclerView中单个item的布局文件item.xml




    

    

三:自定义RecyclerView

/**
 * 加入手机屏幕的宽正好能放3个item,则RecyclerView的某刻的childCount的总数为3或4个。可通过getChildCount()得到
 */
public class MyRecyclerView extends RecyclerView
{

    public MyRecyclerView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    private View mCurrentView;

    /**
     * 滚动时回调的接口
     */
    private OnItemScrollChangeListener mItemScrollChangeListener;

    public void setOnItemScrollChangeListener(
            OnItemScrollChangeListener mItemScrollChangeListener)
    {
        this.mItemScrollChangeListener = mItemScrollChangeListener;
    }

    public interface OnItemScrollChangeListener
    {
        void onChange(View view, int position);
    }

    /**
     * 开始显示布局文件的时候执行,这里是为了初始化页面的图片,即布局完成后默认显示第一张图片
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        Log.i("yin","执行onLayout");
        super.onLayout(changed, l, t, r, b);
        /**
         * 假如手机屏幕的宽正好能放3个item,则RecyclerView的某刻的childCount的总数为3或4个。可通过getChildCount()得到
         * 但是其ChildPosition,即其某个子item的位置并不是0,1,2,3;而是根据资源list决定的,即
         * mDatas决定的,也就是RecyclerView待显示的资源item总数决定的。
         */
        mCurrentView = getChildAt(0);//总是获取到当前显示到屏幕的第一个可见的childItem

        if (mItemScrollChangeListener != null)
        {
            mItemScrollChangeListener.onChange(mCurrentView,
                    getChildPosition(mCurrentView));
        }
    }

    /**
     * 用户手势滑动过程中,不断更新显示出来的第一个item,将其大图放在imageView中
     * 但是手势滑动太快时,并不能及时更新大图,试一下
     * @param e
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {

        if (e.getAction() == MotionEvent.ACTION_MOVE)
        {
            Log.i("yin", "执行onTouchEvent的ACTION_MOVE:" + getChildCount());
            mCurrentView = getChildAt(0);
            // Log.e("TAG", getChildPosition(getChildAt(0)) + "");
            if (mItemScrollChangeListener != null)
            {
                mItemScrollChangeListener.onChange(mCurrentView,
                        getChildPosition(mCurrentView));

            }
            Log.i("yin","执行onTouchEvent的ACTION_MOVE贰:"+getChildCount());
        }
        return super.onTouchEvent(e);
    }

}

更好的自定义RecyclerView的方式

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);
//        this.addOnScrollListener(this);
    }

    @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)
    {
        Log.i("yin","执行onScrolled");
        View newView = getChildAt(0);

        if (mItemScrollChangeListener != null)
        {
            if (newView != null && newView != mCurrentView)
            {
                mCurrentView = newView ;
                mItemScrollChangeListener.onChange(mCurrentView,
                        getChildPosition(mCurrentView));

            }
        }

    }

}


四:RecyclerView的adapter

public class MyRecyclerViewAdapter extends
        RecyclerView.Adapter
{

    private LayoutInflater mInflater;
    private List mDatas;

    /**
     * 构造器
     * @param context
     * @param datats
     */
    public MyRecyclerViewAdapter(Context context, List datats)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    /**
     * 自己的ViewHolder必须继承RecyclerView.ViewHolder
     */
    public static class ViewHolder extends RecyclerView.ViewHolder
    {   //构造器参数是待显示的item的布局的view
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        ImageView mImg;
        TextView mTxt;
    }

    /**
     * 获取总的条目数
     * @return
     */
    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     * viewHolder类似于ListView的convertView,同样类似于BaseAdapter的getView()方法
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.recyclerview_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (ImageView) view
                .findViewById(R.id.id_index_gallery_item_image);
        return viewHolder;
    }

    /**
     * 给各个item绑定数据
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        viewHolder.mImg.setImageResource(mDatas.get(i));
        if(mOnItemClickLitener != null){
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView,i);
                }
            });
        }
    }
    /**
     * ItemClick的回调接口
     * @author zhy
     *
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}

五:在Activity中使用

public class MainActivity extends AppCompatActivity {


    private MyRecyclerView mRecyclerView;
    private MyRecyclerViewAdapter mAdapter;
    private List mDatas;
    private ImageView mImg ;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mImg = (ImageView) findViewById(R.id.id_content);

        mDatas = new ArrayList(Arrays.asList(R.drawable.a,
                R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,
                R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.l));

        mRecyclerView = (MyRecyclerView) findViewById(R.id.id_recyclerview_horizontal);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(this, mDatas);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setOnItemScrollChangeListener(new MyRecyclerView.OnItemScrollChangeListener()
        {
            @Override
            public void onChange(View view, int position)
            {
                Log.i("yin","开始刷新图片:"+position+",id:"+mDatas.get(position));
                mImg.setImageResource(mDatas.get(position));
            };
        });

        mAdapter.setOnItemClickLitener(new MyRecyclerViewAdapter.OnItemClickLitener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
                mImg.setImageResource(mDatas.get(position));
            }
        });

    }

}




你可能感兴趣的:(列表数据)