美文网首页
Android RecyclerView简单实践

Android RecyclerView简单实践

作者: 汽水味丶嗝 | 来源:发表于2018-11-11 22:40 被阅读0次

    emmmmm,写一个简单的RecyclerView的demo

    emmmmmmm

    直接贴代码吧!
    写之前得引入依赖 compile 'com.android.support:recyclerview-v7:26.1.0'
    (ps:版本与appcompat保持一致哦,有时候在不同版本的AS上会出错的)
    或者直接在布局编辑界面拖动Recycler控件,会自动添加依赖。

    首先写下activity_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"
            tools:context="com.oddtree.recycledemo.MainActivity"
            android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:id="@+id/recommend_items"
                android:background="#E6E6E6"/>
    
    </LinearLayout>
    
    之后再来写下RecyclerView中单个Item的layout

    加了点边框背景颜色啥的。

    <?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="145dp"
                  android:paddingLeft="25dp"
                  android:layout_weight="1"
                  android:background="@drawable/item_border">
        <ImageView
                android:layout_width="match_parent"
                android:layout_height="93dp"
                android:id="@+id/item_video_bc"
                android:paddingTop="3dp"
                android:layout_marginRight="25dp"/>
        <TextView
                android:layout_width="130dp"
                android:layout_height="32dp"
                android:paddingTop="1dp"
                android:textColor="@color/recommend_item_titlecl"
                android:textSize="13sp"
                android:id="@+id/item_title"/>
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="15dp"
                android:orientation="horizontal"
                android:id="@+id/item_info">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/recommend_item_read"
                    android:textSize="11sp"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/text_read"
                    android:textSize="11sp"/>
    
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/recommend_item_commented"
                    android:paddingLeft="35dp"
                    android:textSize="11sp"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textSize="11sp" 
                    android:id="@+id/text_commented"/>
        </LinearLayout>
    
    </LinearLayout>
    
    为了方便demo显示,写一个不是bean的bean
    package com.oddtree.recycledemo;
    
    public class IndexRecommedItem {
        private Integer itemBcId;
        private String itemTitle;
        private Integer itemRead;
        private Integer itemComment;
    
        public IndexRecommedItem(Integer itemBcId,
                                 String itemTitle,
                                 Integer itemRead,
                                 Integer itemComment) {
            this.itemBcId = itemBcId;
            this.itemTitle = itemTitle;
            this.itemRead = itemRead;
            this.itemComment = itemComment;
        }
    
        public Integer getItemBcId() {
            return itemBcId;
        }
    
        public void setItemBcId(Integer itemBcId) {
            this.itemBcId = itemBcId;
        }
    
        public String getItemTitle() {
            return itemTitle;
        }
    
        public void setItemTitle(String itemTitle) {
            this.itemTitle = itemTitle;
        }
    
        public Integer getItemRead() {
            return itemRead;
        }
    
        public void setItemRead(Integer itemRead) {
            this.itemRead = itemRead;
        }
    
        public Integer getItemComment() {
            return itemComment;
        }
    
        public void setItemComment(Integer itemComment) {
            this.itemComment = itemComment;
        }
    }
    
    下面继续写最重要的Adapter
    package com.oddtree.recycledemo;
    
    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;
    
    public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
    
        private List<IndexRecommedItem> itemList;
    
        static class ViewHolder extends RecyclerView.ViewHolder{
            ImageView itemBc;
            TextView itemTitle;
            TextView itemReadNum;
            TextView itemCommentNum;
    
            public ViewHolder(View view) {
                super(view);
                itemBc = view.findViewById(R.id.item_video_bc);
                itemTitle = view.findViewById(R.id.item_title);
                itemReadNum = view.findViewById(R.id.text_read);
                itemCommentNum = view.findViewById(R.id.text_commented);
            }
        }
    
        public RecycleAdapter(List<IndexRecommedItem> itemList) {
            this.itemList = itemList;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.index_recommend_item,
                    parent,false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            IndexRecommedItem item = itemList.get(position);
            holder.itemBc.setImageResource(item.getItemBcId());
            holder.itemTitle.setText(item.getItemTitle());
            //因为实体里写的是整型,需要转换下
            holder.itemReadNum.setText(item.getItemRead().toString());
            holder.itemCommentNum.setText(item.getItemComment().toString());
        }
    
        @Override
        public int getItemCount() {
            return itemList.size();
        }
    }
    
    万事俱备只欠MainActivity

    demo是做一个2列展示的view,为了方便日后增加东西,所以直接用了StaggeredGridLayoutManager。StaggeredGridLayoutManager构造的参数有两个,第一个表示列数,第二个表示横纵显示。

    package com.oddtree.recycledemo;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    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 List<IndexRecommedItem> recommendItems = new ArrayList<>();
        //示例用的数据
        private String title = "标题标题标题标题标题白哦提";
        public void initItems(){
            for (int i = 0; i<7; i++){
                IndexRecommedItem item = new IndexRecommedItem(R.drawable.video_part_picdemo1,
                        title+i,(i+1)*100,(i+2)*1000);
                recommendItems.add(item);
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            RecyclerView recommendRecycler = findViewById(R.id.recommend_items);
            initItems();
            StaggeredGridLayoutManager sglManager = new StaggeredGridLayoutManager(2,
                    StaggeredGridLayoutManager.VERTICAL);
            recommendRecycler.setLayoutManager(sglManager);
            RecycleAdapter adapter = new RecycleAdapter(recommendItems);
            recommendRecycler.setAdapter(adapter);
        }
    }
    
    运行截图,可以正常上下滑动:
    demo截图

    相关文章

      网友评论

          本文标题:Android RecyclerView简单实践

          本文链接:https://www.haomeiwen.com/subject/cgmffqtx.html