关闭

RecyclerView多条目展示

62人阅读 评论(0) 收藏 举报
分类:

我也不太懂这个代码的基本意思,有的地方还有疑惑,先把代码粘上后期再修改批注。
需要添加的依赖

 compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
  compile 'com.github.bumptech.glide:glide:4.0.0'

MainActivity类

public class MainActivity extends AppCompatActivity {
    private List<Moudle> list = new ArrayList<>();
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview3);

        for (int i = 0; i < 30; i++) {
            Moudle mon = new Moudle();
            mon.setName("惊悚故事" + i);
            mon.setTitle("Hello" + i);
            mon.setSort("http://cover2.u17i.com/2017/07/18899201_1499070616_3js3vV532Fev.sbig.jpg?1499421235");
            mon.setType(i);
            list.add(mon);
        }

        final Myadapter adapter = new Myadapter(list);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 12);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemviewType = adapter.getItemViewType(position); 
                switch (itemviewType) {
                    case 0:
                        return 12;
                    case 1:
                        return 4;
                    case 2:
                        return 4;
                    case 3:
                        return 4;
                    case 4:
                        return 4;
                    case 5:
                        return 6;
                    case 6:
                        return 6;
                    case 7:
                        return 6;
                    default:
                        return 6;
                }


            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);


    }
}

RecyclerView的适配器类

public class Myadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<Moudle> list;


    public Myadapter(List<Moudle> list) {
        this.list = list;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case 0:
                return new ViewHolderOne(getView(parent, R.layout.recycler));
            case 1:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 2:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 3:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 4:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 5:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 6:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            case 7:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));
            default:
                return new ViewHolderTwo(getView(parent, R.layout.recycler2));

        }


    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolderOne) {
            ((ViewHolderOne) holder).textView.setText(list.get(position).name);

        } else if (holder instanceof ViewHolderTwo) {
            ((ViewHolderTwo) holder).title.setText(list.get(position).title);
            ((ViewHolderTwo) holder).sort.setText(list.get(position).name);
            String picture = list.get(position).getSort();
            Glide.with(((ViewHolderTwo) holder).imageView).load(picture).into(((ViewHolderTwo) holder).imageView);

        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).type % 8;
    }

    private View getView(ViewGroup parent, int recycler) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(recycler, parent, false);
        return inflate;
    }

    static class ViewHolderOne extends RecyclerView.ViewHolder {


        TextView textView;

        public ViewHolderOne(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.text);
        }
    }

    static class ViewHolderTwo extends RecyclerView.ViewHolder {
        TextView title, sort;
        ImageView imageView;

        public ViewHolderTwo(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.images);
            title = (TextView) itemView.findViewById(R.id.title);
            sort = (TextView) itemView.findViewById(R.id.sort);
        }
    }
}

Moudle 属性类

public class Moudle {
    public  String title;
    public  String name;
    public  String sort;
    public  int type;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public void setType(int type) {
        this.type = type;
    }
}

activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.example.recyclerview3.MainActivity">
    <!--注意高设置成自适应,设置成充满的话一行就占一页啦-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

recycler布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ddd"/>

</LinearLayout>

recycler2布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dfdf"
    android:textSize="20sp"/>
    <TextView
        android:id="@+id/sort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="dfdf"/>
    <ImageView
        android:id="@+id/images"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1980次
    • 积分:347
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档