recylerview~

传承者(Inheritors)打造共同进步生态圈!!!

转载:http://blog.csdn.net/lmj623565791/article/details/38173061

效果图

这里写图片描述

activity_index_gallery_itme
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="120dp"
    android:background="@android:color/white"
    android:layout_height="120dp">
<ImageView
    android:id="@+id/index_gallery_item_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="5dp"
    android:scaleType="centerCrop"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/index_gallery_item_text"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/index_gallery_item_image"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:textColor="#ff0000"
        android:textSize="12dp"
        android:text="some info"
        />
</RelativeLayout>
main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.testapplication.Main2Activity">
  <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher"
        />

  </FrameLayout>

  <com.example.administrator.testapplication.MyRecyclerview
      android:id="@+id/id_recyclerview_horizontal"
      android:layout_width="match_parent"
      android:layout_height="120dp"
      android:layout_centerVertical="true"
      android:background="#ff0000"
      android:scrollbars="none"
      ></com.example.administrator.testapplication.MyRecyclerview>
</LinearLayout>
GalleryAdapter
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
  private LayoutInflater mInflater;
    private List<Integer> mDatas;
    private OnItemClickLitener mOnItemClickListener;


    public interface OnItemClickLitener{
        void onItemClick(View view,int position);
    }

    public void setmOnItemClickListener(OnItemClickLitener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public GalleryAdapter(Context context,List<Integer> datas){
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = mInflater.inflate(R.layout.activity_index_gallery_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.mImg = (ImageView)view.findViewById(R.id.index_gallery_item_image);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder,final int position) {
        holder.mImg.setImageResource(mDatas.get(position));

        //设置点击事件
        if(mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        ImageView mImg;

    }
}

MyRecycleview

public class MyRecyclerview extends RecyclerView  {


    public MyRecyclerview(Context context, @Nullable 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);
    }

    @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 boolean onTouchEvent(MotionEvent e) {
        if(e.getAction() == MotionEvent.ACTION_MOVE){
            mCurrentView =  getChildAt(0);
            if(mItemScrollChangeListener != null){
                mItemScrollChangeListener.onChange(mCurrentView,getChildPosition(mCurrentView));
            }
        }


        return super.onTouchEvent(e);
    }
}
调用
public class Main2Activity extends Activity {
private RecyclerView mRecyclerView;
    private GalleryAdapter mAdapter;
    private ImageView imageView;

    private List<Integer> mDatas = new ArrayList<>(Arrays.asList(R.mipmap.a,
            R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e,R.mipmap.f,R.mipmap.g,
            R.mipmap.h,R.mipmap.l));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main2);

        imageView = (ImageView) findViewById(R.id.content);
        //得到控件
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
        //设置布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(manager);

        //设置适配器
        mAdapter = new GalleryAdapter(this,mDatas);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setmOnItemClickListener(new GalleryAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
             //   Toast.makeText(Main2Activity.this,position+"", Toast.LENGTH_SHORT).show();
                imageView.setImageResource(mDatas.get(position));
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值