zoukankan      html  css  js  c++  java
  • Android 控件研究 :RecyclerView

    参考资料:http://blog.csdn.net/xiaohanluo/article/details/52191868

    测试环境:Android 2.3.2

    【1】创建工程ReclerViewPractice

    【2】修改build.gradle,黄色部分为新添加语句。修改完毕后,点击右上角同步。

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
    applicationId "com.example.reclerviewpractice"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    }
    
    

      

    【3】修改布局文件activity_main.xml

    <?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">
    <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    </LinearLayout>

    【4】新建 activity_rv.xml 

    <?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
    android:id="@+id/item_tv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:text="item"/>
    </LinearLayout>

    【5】新建适配器,即新建类 :MyAdapter

     1 package com.example.reclerviewpractice;
     2 
     3 import android.support.v7.widget.RecyclerView;
     4 import android.view.LayoutInflater;
     5 import android.view.View;
     6 import android.view.ViewGroup;
     7 import android.widget.TextView;
     8 
     9 import java.util.ArrayList;
    10 
    11 /**
    12  * Created by Administrator on 2017/6/10 0010.
    13  */
    14 
    15 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
    16 
    17     private ArrayList<String> mData;
    18 
    19     public MyAdapter(ArrayList<String> data) {
    20         this.mData = data;
    21     }
    22 
    23     public void updateData(ArrayList<String> data) {
    24         this.mData = data;
    25         notifyDataSetChanged();
    26     }
    27 
    28     @Override
    29     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    30         // 实例化展示的view
    31         View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false);
    32         // 实例化viewholder
    33         ViewHolder viewHolder = new ViewHolder(v);
    34         return viewHolder;
    35     }
    36 
    37     @Override
    38     public void onBindViewHolder(ViewHolder holder, int position) {
    39         // 绑定数据
    40         holder.mTv.setText(mData.get(position));
    41     }
    42 
    43     @Override
    44     public int getItemCount() {
    45         return mData == null ? 0 : mData.size();
    46     }
    47 
    48     public static class ViewHolder extends RecyclerView.ViewHolder {
    49 
    50         TextView mTv;
    51 
    52         public ViewHolder(View itemView) {
    53             super(itemView);
    54             mTv = (TextView) itemView.findViewById(R.id.item_tv);
    55         }
    56     }
    57 }

    【6】修改MainActivity.java

     1 package com.example.reclerviewpractice;
     2 import android.support.v7.app.AppCompatActivity;
     3 import android.os.Bundle;
     4 import android.support.v7.widget.LinearLayoutManager;
     5 import android.support.v7.widget.RecyclerView;
     6 
     7 import java.util.ArrayList;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10     private RecyclerView mRecyclerView;
    11 
    12     private RecyclerView.Adapter mAdapter;
    13 
    14     private RecyclerView.LayoutManager mLayoutManager;
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20         initData();
    21         initView();
    22     }
    23 
    24     private void initData() {
    25         mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    26         mAdapter = new MyAdapter(getData());
    27     }
    28 
    29     private void initView() {
    30         mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    31         // 设置布局管理器
    32         mRecyclerView.setLayoutManager(mLayoutManager);
    33         // 设置adapter
    34         mRecyclerView.setAdapter(mAdapter);
    35     }
    36 
    37     private ArrayList<String> getData() {
    38         ArrayList<String> data = new ArrayList<>();
    39         String temp = " item";
    40         for(int i = 0; i < 20; i++) {
    41             data.add(i + temp);
    42         }
    43 
    44         return data;
    45     }
    46 }

  • 相关阅读:
    第二节.NET两种交互模式:c/s和b/s
    python自动登录代码
    关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠
    利用java内部静态类实现懒汉式单例
    Linux,Ubuntu,Mint 环境下 navicat 乱码问题
    如何在Linux中使用命令行卸载软件
    Linux安装Oracle JDK替换OpenJDK详解
    【踩坑】利用fastjson反序列化需要默认构造函数
    基于 Markdown 编写接口文档
    Gradle里面两个 依赖管理插件,可以不用关心 具体jar版本号
  • 原文地址:https://www.cnblogs.com/hbuwyg/p/6978592.html
Copyright © 2011-2022 走看看