Code Monkey home page Code Monkey logo

recyclerviewgallery's Introduction

RecyclerViewGallery

通过RecyclerView封装实现的Gallery,并优化了回调

一 效果图


rvgallery.gif

二 使用方法


最新版本号

  1. 在你root的gradle中添加引用
allprojects {
    repositories {
    ...
    maven { url "https://jitpack.io" }
    }
 }

然后在module的gradle中添加引用

dependencies {
    compile 'com.github.kaxi4it:RecyclerViewGallery:1.3'
}
  1. 在你的布局文件中添加xml代码如:
<com.guyj.rvgallery.RvGallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

然后需要继承RvGalleryAdapter并实现一一实现里面的方法如:

class MyAdapter extends RvGalleryAdapter<String,MyAdapter.MyViewHolder> {
    public MyAdapter(Context context) {
        super(context);
    }
    @Override
    protected MyViewHolder setViewHolder(View viewImage) {
        return new MyViewHolder(viewImage);
    }
    @Override
    public int setViewLayoutID() {
        return R.layout.item_image;
    }
    @Override
    protected void convert(MyViewHolder holder, int position, String bean) {
        if (position%3==0){
            holder.img.setImageResource(R.mipmap.guide_2);
        }else{
            holder.img.setImageResource(R.mipmap.guide_1);
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        public MyViewHolder(View itemView) {
            super(itemView);
            img=(ImageView)itemView.findViewById(R.id.img);
        }
    }
}

最后在我们的Activity类中实现如下方法,即可正常使用该控件了:

 private void initGallery() {
        gallery.setAdapter(adapter=new MyAdapter(this));
        adapter.setData(benas);
        gallery.setOnItemSelectedListener(new RvGallery.OnItemSelectedListener() {
            @Override
            public void itemSelected(int position) {
                Toast.makeText(MainActivity.this, "index="+position, Toast.LENGTH_SHORT).show();
            }
        });
    }

补充说明: RvGalleryAdapter<String,MyAdapter.MyViewHolder>中string是你数据集合的泛型类型,viewholder则是你的item的viewholder类型,由于我的viewholder为了方便写在了adapter中,正常情况下,建议拆分开。

recyclerviewgallery's People

Contributors

kaxi4it avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

871793066

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.