我的Android成长之路(10)----利用recyclerView实现横向滑动

利用recyclerView实现横向滑动。

下面是adapter:

public class HengAdapter extends RecyclerView.Adapter implements View.OnClickListener{

    private List list ;
    private Context mContext ;
    private View holder ;

    private OnRecyclerViewItemClickListener  mItemClickListener = null ;
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data);
    }

    public WuliuHAdapter(Context context,List list){
        this.mContext = context ;
        this.list = list ;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener){
        this.mItemClickListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        holder = LayoutInflater.from(
                mContext).inflate(R.layout.adapter_wuliu_gridview, parent,
                false);
        return new MyViewHolder(holder);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTitle.setText(list.get(position));
        holder.mTitle.setOnClickListener(this);
        holder.mTitle.setTag(position+"");
    }

    public void setList(List list){
        this.list = list ;
    }

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

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mItemClickListener.onItemClick(v,(String)v.getTag());
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView mTitle;
        public MyViewHolder(View view)
        {
            super(view);
            mTitle = (TextView) view.findViewById(R.id.adapter_wuliu_grid_title);
        }

    }
}

然后是在activity中调用:

mHAdapter = new HengAdapter(this,list) ;
final StaggeredGridLayoutManager layoutManager= new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager (layoutManager);
mHAdapter.setHasStableIds(true);
mRecyclerView.setAdapter(mHAdapter);


你可能感兴趣的:(Android)