提醒一下 RecyclerView 安卓自带的当中是没有的,所以需要导入包的。
android.support.v4.widget.SwipeRefreshLayout 中的v7,v4都已经变成 androidx了。希望大家注意
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
如果版本较低的话,添加
implementation 'com.android.support:recyclerview-v7:28.0.0'
项目结构 ,写的比较复杂,没有嵌套 需求,本文章就不要看了。
activity_buttom.xml
activity_grid.xml
activity_griditem.xml
activity_main.xml
activity_topitem.xml
package com.example.myapplication.pojo;
public class Taobao {
private String name;
private String information;
@Override
public String toString() {
return "Taobao{" +
"name='" + name + '\'' +
", information='" + information + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
}
package com.example.myapplication.MyAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.myapplication.R;
import com.example.myapplication.pojo.Taobao;
import java.util.List;
//自定义带有GridLayOut布局的
public class GridViewAdapter extends BaseAdapter {
private List taobaoList;
private LayoutInflater inflater;
public GridViewAdapter() {
}
public GridViewAdapter(List stuList, Context context) {
this.taobaoList = stuList;
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return taobaoList ==null?0: taobaoList.size();
}
@Override
public Taobao getItem(int position) {
return taobaoList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//加载布局为一个视图
final View view=inflater.inflate(R.layout.activity_griditem,null);
ViewHolder viewHolder=new ViewHolder();
Taobao taobao=getItem(position);
viewHolder.t1=view.findViewById(R.id.t1);
viewHolder.t1.setText(taobao.getName());
viewHolder.t2=view.findViewById(R.id.t2);
viewHolder.t2.setText(taobao.getInformation());
return view;
}
class ViewHolder{
TextView t1;
TextView t2;
}
}
package com.example.myapplication.MyAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.pojo.Taobao;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
List list; //数据集合
Context context;
String path1;
String path2;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(List list, Context context,String path1,String path2){
this.list=list ;
this.path1=path1;
this.path2=path2;
this.context = context;
}
// 这个方法是用来处理onCreateViewHolder方法中的viewtype 的
@Override
public int getItemViewType(int position) {
System.out.println("getItemViewType "+position);
return position;
}
// 初始化item布局
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder=null;
System.out.println("这里的 viewtype "+viewType);
if(viewType==0){
View view= LayoutInflater.from(context).inflate(R.layout.activity_topitem,parent,false);
view.getLayoutParams().height=170;
viewHolder=new TopHolder(view);
}
if(viewType==1){
View view= LayoutInflater.from(context).inflate(R.layout.activity_grid,parent,false);
viewHolder=new GridViewHolder(view);
}
if(viewType==2){
View view= LayoutInflater.from(context).inflate(R.layout.activity_buttom,parent,false);
view.getLayoutParams().height=100;
viewHolder=new ButtomHolder(view);
}
return viewHolder;
}
// 处理每一个item 的地方,绑定数据
@Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
System.out.println("onBindViewHolder" +position);
if(position==0){
System.out.println("yongl ");
TopHolder topHolder=(TopHolder)holder;
}
if( position==1){
System.out.println("yong2 ");
GridViewHolder gridViewHolder=(GridViewHolder)holder;
gridViewHolder.gridView.setAdapter(gridViewHolder.gridViewAdapter);
}
if( position==2){
System.out.println("yong3 ");
ButtomHolder buttomHolder=(ButtomHolder)holder;
buttomHolder.t1.setText(path1);
buttomHolder.t2.setText(path2);
}
}
// 显示数量,因为我的布局文件就是三个,所以我返回san
@Override
public int getItemCount() {
return 3;
}
public class GridViewHolder extends RecyclerView.ViewHolder{
public GridView gridView;
public GridViewAdapter gridViewAdapter;
public GridViewHolder(@NonNull View itemView) {
super(itemView);
gridView=itemView.findViewById(R.id.g1);
gridViewAdapter=new GridViewAdapter(list,context);
}
}
public class ButtomHolder extends RecyclerView.ViewHolder {
public TextView t1;
public TextView t2;
public ButtomHolder(@NonNull View itemView) {
super(itemView);
t1=itemView.findViewById(R.id.t1);
t2=itemView.findViewById(R.id.t2);
}
}
public class TopHolder extends RecyclerView.ViewHolder {
public TopHolder(@NonNull View itemView) {
super(itemView);
}
}
}
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.myapplication.MyAdapter.RecyclerViewAdapter;
import com.example.myapplication.pojo.Taobao;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// 上面是 gridview 适配器
private List taobaoList;
private RecyclerViewAdapter recyclerViewAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initdata();
recyclerView = findViewById(R.id.news_list);
recyclerViewAdapter = new RecyclerViewAdapter(taobaoList, MainActivity.this, "num1", "num2");
recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//// 下面配置RecyclerView 布局
// recyclerViewAdapter = new RecyclerViewAdapter(taobaoList, MainActivity.this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Toast.makeText(MainActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Toast.makeText(MainActivity.this, "下拉刷新OnScrolled", Toast.LENGTH_SHORT).show();
}
});
}
public void initdata() {
taobaoList = new ArrayList<>();
String[] taobao_name = new String[]{
"蓝月亮洗衣液组合", "南方黑芝麻糊360*3袋", "鞋刷软毛刷鞋刷子不伤血", "ANN骑士靴女高筒2019秋冬平底鞋",
"凌平铺子-坚果大礼包", "黑麦片高纤维低脂", "君乐宝酸奶健存", "新品【三只松鼠】750g"
};
String[] taobao_price = new String[]{
"¥22.2", "¥332", "¥213", "¥99.9", "¥19.9", "¥234", "¥222", "¥999", "¥888"
};
for (int i = 0; i < 8; i++) {
Taobao stu = new Taobao();
stu.setName(taobao_name[i]);
stu.setInformation(taobao_price[i]);
taobaoList.add(stu);
}
}
}