RecyclerView 在有限的窗口中展示大量数据集

需要添加依赖库
compile 'com.android.support:recyclerview-v7:23.1.1'
package com.zdsoft.recyclerview1129;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    private List list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inieView();
        initDate();

    }

    private void inieView() {
        rv = (RecyclerView) findViewById(R.id.rv);
    }

    private void initDate() {
        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(this, getList());
        rv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        /**
         * LinearLayoutManager:像listview一样展示
         * GridLayoutManager:像Gridview一样展示
         * StaggeredGridLayoutManager:瀑布流展示方式
         */
        rv.setAdapter(adapter);
    }

    /**
     * 数据源
     *
     * @return
     */
    private List getList() {
        list = new ArrayList();
        Item i1 = new Item();
        i1.setImg(R.drawable.a1);
        list.add(i1);

        Item i2 = new Item();
        i2.setImg(R.drawable.a2);
        list.add(i2);

        Item i3 = new Item();
        i3.setImg(R.drawable.a4);
        list.add(i3);
        Item i4 = new Item();
        i4.setImg(R.drawable.a4);
        list.add(i4);

        Item i5 = new Item();
        i5.setImg(R.drawable.a2);
        list.add(i5);

        Item i6 = new Item();
        i6.setImg(R.drawable.a3);
        list.add(i6);

        Item i7 = new Item();
        i7.setImg(R.drawable.a4);
        list.add(i7);

        Item i8 = new Item();
        i8.setImg(R.drawable.q4);
        list.add(i8);
        return list;
    }
}

package com.zdsoft.recyclerview1129;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/11/29.
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter {


    private Context context;
    private List list;

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setTag(myViewHolder);
        return myViewHolder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.iv_img.setImageDrawable(ContextCompat.getDrawable(context, list.get(position).getImg()));

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_img;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);

        }
    }


}

package com.zdsoft.recyclerview1129;

/**
 * Created by Administrator on 2016/11/29.
 */
public class Item {
    private int img;

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }


}




    







    


你可能感兴趣的:(Android,控件)