RecycleView
恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在。(现在也还是一头雾水,不过照搬也会用了)
这是第一版的代码,都写在 MainActivity.java 里了。
public class MainActivity extends AppCompatActivity { private RecyclerView rv;
private ArrayList<String> datas = new ArrayList<String>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); rv = new RecyclerView(this); createData(datas); setContentView(rv); rv.setLayoutManager(new LinearLayoutManager(this)); rv.setAdapter(new RecyclerView.Adapter() { //重写你想要的RecyclerView中的每一项所含有的内容
class myViewHolder extends RecyclerView.ViewHolder {
private TextView tv; public myViewHolder(TextView itemView) {
super(itemView);
tv = itemView;
tv.setTextSize(28);
} public TextView getTv() {
return tv;
}
} //创建RecycleView中每一项的内容
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new myViewHolder(new TextView(parent.getContext()));
} //创建的的每一项与布局绑定并设置数据
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((myViewHolder)holder).getTv().setText(datas.get(position));
} //获取RecycleView的长度
@Override
public int getItemCount() {
System.out.println("--------------------------------------->"+datas.size());
return datas.size();
}
});
} public void createData(ArrayList<String> data) { for(char i='A';i<='Z';i++)
{
data.add(String.valueOf(i));
}
} }
接下里是升级后的,把Adapter单独提出来写,也是最重要的部分!MyAdapter.java
package com.liwenchi.learnrv; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; /**
* Created by VULCAN on 2017/7/26.
*/
class MyAdapter extends RecyclerView.Adapter { private CellData[] data = new CellData[]{new CellData("为什么Django框架仍然统治着Python开发世界","因为它真的很厉害"),new CellData("用Python分析购物篮","一种有用的技术成为关联分析...")}; //重写你想要的RecyclerView中的每一项所含有的内容
class myViewHolder extends RecyclerView.ViewHolder {
private View root;
private TextView tvTitle;
private TextView tvContent; public myViewHolder(View root) {
super(root); tvTitle = (TextView) root.findViewById(R.id.tvTitle);
tvContent = (TextView) root.findViewById(R.id.tvContent);
} public TextView getTvTitle() {
return tvTitle;
} public TextView getTvContent() {
return tvContent;
}
} //创建RecycleView中每一项的内容
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建一个布局解释器
return new myViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell, null));
} //创建的的每一项与布局绑定并设置数据
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((myViewHolder) holder).getTvTitle().setText(data[position].title);
((myViewHolder) holder).getTvContent().setText(data[position].content);
} //获取RecycleView的长度
@Override
public int getItemCount() {
return data.length;
}
}
MainActivity.java
package com.liwenchi.learnrv; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import java.util.ArrayList;
public class MainActivity extends AppCompatActivity { private RecyclerView rv; private ArrayList<String> datas = new ArrayList<String>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); rv = new RecyclerView(this); createData(datas); setContentView(rv); rv.setLayoutManager(new LinearLayoutManager(this)); rv.setAdapter(new MyAdapter());
} public void createData(ArrayList<String> data) { for(char i='A';i<='Z';i++)
{
data.add(String.valueOf(i));
}
}
}
CellData.java
public class CellData { public CellData(String title, String content) {
this.title = title;
this.content = content;
} String title = "title";
String content = "Content"; }
list_cell.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"> <TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="标题" /> <TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="内容" />
</LinearLayout>