码迷,mamicode.com
首页 > 移动开发 > 详细

[2017-7-26]Android Learning Day4

时间:2017-07-26 17:52:01      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:title   font   cal   内容   line   bind   print   i++   get   

RecycleView

恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在。(现在也还是一头雾水,不过照搬也会用了)

这是第一版的代码,都写在 MainActivity.java 里了。

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private RecyclerView rv;
 4     private ArrayList<String> datas = new ArrayList<String>();
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9 
10         rv = new RecyclerView(this);
11 
12         createData(datas);
13 
14         setContentView(rv);
15 
16         rv.setLayoutManager(new LinearLayoutManager(this));
17 
18         rv.setAdapter(new RecyclerView.Adapter() {
19 
20             //重写你想要的RecyclerView中的每一项所含有的内容
21             class myViewHolder extends RecyclerView.ViewHolder {
22                 private TextView tv;
23 
24                 public myViewHolder(TextView itemView) {
25                     super(itemView);
26                     tv = itemView;
27                     tv.setTextSize(28);
28                 }
29 
30                 public TextView getTv() {
31                     return tv;
32                 }
33             }
34 
35             //创建RecycleView中每一项的内容
36             @Override
37             public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
38                 return new myViewHolder(new TextView(parent.getContext()));
39             }
40 
41             //创建的的每一项与布局绑定并设置数据
42             @Override
43             public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
44                 ((myViewHolder)holder).getTv().setText(datas.get(position));
45             }
46 
47             //获取RecycleView的长度
48             @Override
49             public int getItemCount() {
50                 System.out.println("--------------------------------------->"+datas.size());
51                 return datas.size();
52             }
53         });
54     }
55 
56     public void createData(ArrayList<String> data) {
57 
58         for(char i=‘A‘;i<=‘Z‘;i++)
59         {
60             data.add(String.valueOf(i));
61         }
62     }
63 
64 }

 接下里是升级后的,把Adapter单独提出来写,也是最重要的部分!MyAdapter.java

 1 package com.liwenchi.learnrv;
 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 /**
10  * Created by VULCAN on 2017/7/26.
11  */
12 class MyAdapter extends RecyclerView.Adapter {
13 
14     private CellData[] data = new CellData[]{new CellData("为什么Django框架仍然统治着Python开发世界","因为它真的很厉害"),new CellData("用Python分析购物篮","一种有用的技术成为关联分析...")};
15 
16     //重写你想要的RecyclerView中的每一项所含有的内容
17     class myViewHolder extends RecyclerView.ViewHolder {
18         private View root;
19         private TextView tvTitle;
20         private TextView tvContent;
21 
22         public myViewHolder(View root) {
23             super(root);
24 
25             tvTitle = (TextView) root.findViewById(R.id.tvTitle);
26             tvContent = (TextView) root.findViewById(R.id.tvContent);
27         }
28 
29         public TextView getTvTitle() {
30             return tvTitle;
31         }
32 
33         public TextView getTvContent() {
34             return tvContent;
35         }
36     }
37 
38     //创建RecycleView中每一项的内容
39     @Override
40     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
41         //创建一个布局解释器
42         return new myViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell, null));
43     }
44 
45     //创建的的每一项与布局绑定并设置数据
46     @Override
47     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
48         ((myViewHolder) holder).getTvTitle().setText(data[position].title);
49         ((myViewHolder) holder).getTvContent().setText(data[position].content);
50     }
51 
52     //获取RecycleView的长度
53     @Override
54     public int getItemCount() {
55         return data.length;
56     }
57 }

MainActivity.java

 1 package com.liwenchi.learnrv;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.support.v7.widget.LinearLayoutManager;
 6 import android.support.v7.widget.RecyclerView;
 7 
 8 import java.util.ArrayList;
 9 public class MainActivity extends AppCompatActivity {
10 
11     private RecyclerView rv;
12 
13     private ArrayList<String> datas = new ArrayList<String>();
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18 
19         rv = new RecyclerView(this);
20 
21         createData(datas);
22 
23         setContentView(rv);
24 
25         rv.setLayoutManager(new LinearLayoutManager(this));
26 
27         rv.setAdapter(new MyAdapter());
28     }
29 
30     public void createData(ArrayList<String> data) {
31 
32         for(char i=‘A‘;i<=‘Z‘;i++)
33         {
34             data.add(String.valueOf(i));
35         }
36     }
37 }

CellData.java

 1 public class CellData {
 2 
 3     public CellData(String title, String content) {
 4         this.title = title;
 5         this.content = content;
 6     }
 7 
 8     String title = "title";
 9     String content = "Content";
10 
11 }

list_cell.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <TextView
 8         android:id="@+id/tvTitle"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:textAppearance="?android:attr/textAppearance"
12         android:text="标题" />
13 
14     <TextView
15         android:id="@+id/tvContent"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="内容" />
19 </LinearLayout>

 

[2017-7-26]Android Learning Day4

标签:title   font   cal   内容   line   bind   print   i++   get   

原文地址:http://www.cnblogs.com/liwenchi/p/7240189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!