高斋晓开卷,独共圣人语。这篇文章主要讲述[2017-7-26]Android Learning Day4相关的知识,希望能为你提供帮助。
RecycleView恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在。(现在也还是一头雾水,不过照搬也会用了)
这是第一版的代码,都写在 MainActivity.java 里了。
1 public class MainActivity extends AppCompatActivity { 2 3private RecyclerView rv; 4private ArrayList< String> datas = new ArrayList< String> (); 5 6@Override 7protected void onCreate(Bundle savedInstanceState) { 8super.onCreate(savedInstanceState); 9 10rv = new RecyclerView(this); 11 12createData(datas); 13 14setContentView(rv); 15 16rv.setLayoutManager(new LinearLayoutManager(this)); 17 18rv.setAdapter(new RecyclerView.Adapter() { 19 20//重写你想要的RecyclerView中的每一项所含有的内容 21class myViewHolder extends RecyclerView.ViewHolder { 22private TextView tv; 23 24public myViewHolder(TextView itemView) { 25super(itemView); 26tv = itemView; 27tv.setTextSize(28); 28} 29 30public TextView getTv() { 31return tv; 32} 33} 34 35//创建RecycleView中每一项的内容 36@Override 37public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 38return new myViewHolder(new TextView(parent.getContext())); 39} 40 41//创建的的每一项与布局绑定并设置数据 42@Override 43public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 44((myViewHolder)holder).getTv().setText(datas.get(position)); 45} 46 47//获取RecycleView的长度 48@Override 49public int getItemCount() { 50System.out.println("---------------------------------------> "+datas.size()); 51return datas.size(); 52} 53}); 54} 55 56public void createData(ArrayList< String> data) { 57 58for(char i=‘A‘; i< =‘Z‘; i++) 59{ 60data.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 14private CellData[] data = https://www.songbingjia.com/android/new CellData[]{new CellData("为什么Django框架仍然统治着python开发世界","因为它真的很厉害"),new CellData("用Python分析购物篮","一种有用的技术成为关联分析...")}; 15 16//重写你想要的RecyclerView中的每一项所含有的内容 17class myViewHolder extends RecyclerView.ViewHolder { 18private View root; 19private TextView tvTitle; 20private TextView tvContent; 21 22public myViewHolder(View root) { 23super(root); 24 25tvTitle = (TextView) root.findViewById(R.id.tvTitle); 26tvContent = (TextView) root.findViewById(R.id.tvContent); 27} 28 29public TextView getTvTitle() { 30return tvTitle; 31} 32 33public TextView getTvContent() { 34return tvContent; 35} 36} 37 38//创建RecycleView中每一项的内容 39@Override 40public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 41//创建一个布局解释器 42return new myViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell, null)); 43} 44 45//创建的的每一项与布局绑定并设置数据 46@Override 47public 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 54public int getItemCount() { 55return 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 11private RecyclerView rv; 12 13private ArrayList< String> datas = new ArrayList< String> (); 14 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17super.onCreate(savedInstanceState); 18 19rv = new RecyclerView(this); 20 21createData(datas); 22 23setContentView(rv); 24 25rv.setLayoutManager(new LinearLayoutManager(this)); 26 27rv.setAdapter(new MyAdapter()); 28} 29 30public void createData(ArrayList< String> data) { 31 32for(char i=‘A‘; i< =‘Z‘; i++) 33{ 34data.add(String.valueOf(i)); 35} 36} 37 }
CellData.java
1 public class CellData { 2 3public CellData(String title, String content) { 4this.title = title; 5this.content = content; 6} 7 8String title = "title"; 9String 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" 3android:orientation="vertical" 4android:layout_width="match_parent" 5android:layout_height="match_parent"> 6 7< TextView 8android:id="@+id/tvTitle" 9android:layout_width="match_parent" 10android:layout_height="wrap_content" 11android:textAppearance="?android:attr/textAppearance" 12android:text="标题" /> 13 14< TextView 15android:id="@+id/tvContent" 16android:layout_width="match_parent" 17android:layout_height="wrap_content" 18android:text="内容" /> 19 < /LinearLayout>
【[2017-7-26]Android Learning Day4】
推荐阅读
- Android获取wifi MAC,关闭的wifi不能获取
- 研读(Shielding applications from an untrusted cloud with Haven)
- iisapp -a命令出现 :此脚本不能与WScript工作
- Android 打开本地文件
- 安卓手机误删除照片恢复方法最强版(适应小米华为等)
- Android实现点击通知栏后,先启动应用再打开目标Activity ,极光推送等推送的也可以参考一下(转)
- 货拉拉app开发
- Android自定义View实现仿QQ实现运动步数效果
- android中实现毛笔效果(View 中画图)