Android-->RecyclerView的上手教程(模

2017-04-27  本文已影响184人  angcyo

效果图:

这里写图片描述
考虑到图片体积, 上一个和下一个的按钮,没有截图出来.

RecyclerView使用教程

1:创建RecyclerView,也可以在布局中创建

recyclerView = new RecyclerView(this);

2:创建LinearLayoutManager对象,这个对象可以设置 垂直和水平滚动的方向

manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.HORIZONTAL);//设置方向为横向

3:创建一个RecyclerView.ViewHolder对象,用来保存每个Item的View对象

public final class MyViewHolder extends RecyclerView.ViewHolder {

    public ImageView img;//我这里只有一张图片

    public MyViewHolder(View itemView) {
        img = (ImageView) itemView;
    }

}

4:创建一个RecyclerView.Adapter<ViewHolder>对象

public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {

    @Override
    public int getItemCount() {//返回item的数量
        return imgs.length;
    }

    @Override
    public void onBindViewHolder(//这里设置Item中View的值
            android.support.v7.widget.RecyclerView.ViewHolder arg0, int arg1) {

        ((MyViewHolder) arg0).img.setImageResource(imgs[arg1]);

    }

    @Override
    public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(
            ViewGroup arg0, int arg1) {//创建Item的View,并交给ViewHolder管理
        ImageView img = new ImageView(MainActivity.this);
        img.setLayoutParams(new LayoutParams(getWidth(), 400));

        return new MyViewHolder(img);
    }
}

5:最后一步:

adapter = new MyAdapter();
recyclerView.setLayoutManager(manager);//关联布局管理,水平方向或者垂直方向
recyclerView.setAdapter(adapter);//关联数据,需要展示的数据

源代码下载: http://download.csdn.net/detail/angcyo/8792845

源码:

public class MainActivity extends ActionBarActivity {

    RecyclerView recyclerView;
    LinearLayoutManager manager;
    RecyclerView.Adapter adapter;
    RelativeLayout rootView;

    int[] imgs = new int[] { R.drawable.dvd_decrypter, R.drawable.eac,
            R.drawable.elo, R.drawable.norton_alt, R.drawable.elo_alt2,
            R.drawable.elster_formular, R.drawable.eset_endpoint_security };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rootView = (RelativeLayout) findViewById(R.id.root);

        recyclerView = new RecyclerView(this);
        recyclerView.setHasFixedSize(true);
        manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);

        adapter = new MyAdapter();
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);

        recyclerView.setBackgroundColor(Color.parseColor("#3E3E3E"));

        RelativeLayout.LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, 400);
        rootView.addView(recyclerView, params);
        recyclerView.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                    int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int width = getWidth() / 2; // 图片的一半宽
                    int offset = recyclerView.computeHorizontalScrollOffset()
                            % getWidth() > width ? getWidth()
                            - recyclerView.computeHorizontalScrollOffset()
                            % getWidth() : -recyclerView
                            .computeHorizontalScrollOffset() % getWidth();

                    recyclerView.smoothScrollBy(offset, 0);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                // Log.e(dx + "", dy + "");
            }
        });
    }

    public final class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView;
        }

    }

    public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {

        @Override
        public int getItemCount() {
            return imgs.length;
        }

        @Override
        public void onBindViewHolder(
                android.support.v7.widget.RecyclerView.ViewHolder arg0, int arg1) {

            ((MyViewHolder) arg0).img.setImageResource(imgs[arg1]);

        }

        @Override
        public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(
                ViewGroup arg0, int arg1) {
            ImageView img = new ImageView(MainActivity.this);
            img.setLayoutParams(new LayoutParams(getWidth(), 400));

            return new MyViewHolder(img);
        }
    }

    public int getWidth() {
        Log.e("width", recyclerView.getMeasuredWidth() / 3 + "");
        return recyclerView.getMeasuredWidth() / 3;
    }

    public void prev(View view) {
        recyclerView.smoothScrollBy(-getWidth(), 0);
    }

    public void next(View view) {
        recyclerView.smoothScrollBy(getWidth(), 0);
    }
}

至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.

上一篇下一篇

猜你喜欢

热点阅读