进阶篇-用户界面:3.RecyclerView

Posted 安卓随笔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进阶篇-用户界面:3.RecyclerView相关的知识,希望对你有一定的参考价值。

1.使用RecyclerView

     RecyclerView是support.v7包中用来替代传统的ListView布局的,它比ListView更加轻便和易用。

     在使用RecyclerView时首先要 右键项目->open module settings->Dependencies标签->添加一个库 com.android.support:recyclerview-v7:23.3.0。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = new RecyclerView(this);
        setContentView(rv);

        rv.setLayoutManager(new LinearLayoutManager(this));

        rv.setAdapter(new RecyclerView.Adapter() {

            class ViewHoder extends RecyclerView.ViewHolder {

                private TextView tv;

                public ViewHoder(TextView itemView) {
                    super(itemView);

                    tv = itemView;

                }

                public TextView getTv() {
                    return tv;
                }
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHoder(new TextView(parent.getContext()));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                  ViewHoder vh = (ViewHoder) holder;
                vh.getTv().setText(data[position]);
            }

            @Override
            public int getItemCount() {
                return data.length;
            }
            private String[] data = new String[]{"test1","test2","test3"};
        });
    }
}

该demo实现的功能是在RecyclerView里面呈现test1,test2,test3。

2.使用资源文件自定义列表项

     新建一个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="match_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/tvTitle"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/tvContent"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

其中有两个组件,一个大的标题,一个小的内容。

 

创建一个自定义类CellData:

CellData.java:

/**
 * Created by lzc on 16/6/30.
 */
public class CellData {
    public CellData(String title,String content){
        this.title = title;
        this.content = content;
    }

    public String title="title";
    public String content="content";


}

将Adaper分离出来,成类

MyAdapter.java:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by lzc on 16/6/30.
 */
class MyAdapter extends RecyclerView.Adapter {

    class ViewHoder extends RecyclerView.ViewHolder {

        private View root;
        private TextView tvTitle,tvContent;
        public ViewHoder(View root) {
            super(root);
            tvTitle = (TextView) root.findViewById(R.id.tvTitle);
            tvContent = (TextView)root.findViewById(R.id.tvContent);
        }

        public TextView getTvContent() {
            return tvContent;
        }

        public TextView getTvTitle() {
            return tvTitle;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHoder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHoder vh = (ViewHoder) holder;
        CellData cd = data[position];
        vh.getTvTitle().setText(cd.title);
        vh.getTvContent().setText(cd.content);
    }

    @Override
    public int getItemCount() {
        return data.length;
    }
    private CellData[] data = new CellData[]{new CellData("title1","content1"),new CellData("title2","content2")};

}

 

 

   MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = new RecyclerView(this);
        setContentView(rv);

        rv.setLayoutManager(new LinearLayoutManager(this));

        rv.setAdapter(new MyAdapter());
    }

}

3.RecyclerView的布局样式

    RecyclerView可以使用其他的布局

在MainActivity.java中修改:

rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true));//水平布局并且反转
rv.setLayoutManager(new GridLayoutManager(this,4));//表格布局 4为显示的列数

 

以上是关于进阶篇-用户界面:3.RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章

Django进阶篇

进阶篇-用户界面:3.android中的基本布局-layout

微信小程序开发笔记 进阶篇④——getPhoneNumber 获取用户手机号码(小程序云)

SQL注入进阶篇一php代码审计

从0系统学Android--3.6 RecyclerView

iOS开发 - 第02篇 - UI进阶 - 08 - 私人通讯录