首先需要导入的依赖
implementation ‘com.gavin.com.library:stickyDecoration:1.1.0’
MainActivity
package com.example.asus.week3application;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.example.asus.week3application.adapter.MyAdapter;
import com.example.asus.week3application.bean.MyBean;
import com.example.asus.week3application.contacts.Contacts;
import com.example.asus.week3application.presenter.MyPresenter;
import com.example.asus.week3application.view.MyView;
import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,MyView {
@BindView(R.id.recy)
RecyclerView recy;
private List Rdata = new ArrayList<>();
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MyPresenter myPresenter = new MyPresenter(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recy.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter(Rdata, this);
recy.setAdapter(myAdapter);
sticky();
Map map = new HashMap<>();
map.put("uid",71+"");
myPresenter.Request(Contacts.URL,map,MyBean.class);
}
private void sticky() {
//使用StickyDecoration
StickyDecoration decoration = StickyDecoration.Builder
.init(new GroupListener() {
@Override
public String getGroupName(int position) {
//组名回调
if (Rdata.size() > position) {
//获取城市对应的省份
return Rdata.get(position).getSellerName();
}
return null;
}
})
.setGroupBackground(Color.parseColor("#48BDFF")) //背景色
//高度
.setDivideColor(Color.parseColor("#CCCCCC")) //分割线颜色
//分割线高度 (默认没有分割线)
.setGroupTextColor(Color.WHITE) //字体颜色
// 边距 靠左时为左边距 靠右时为右边距
.isAlignLeft(false) //靠右显示 (默认靠左)
.build();
recy.addItemDecoration(decoration);
}
@Override
public void onClick(View v) {
}
@Override
public void Success(Object data) {
if(data instanceof MyBean){
MyBean bean = (MyBean) data;
List data1 = bean.getData();
//去除空格
Log.e("data",data1.toString());
data1.remove(0);
Log.e("data",data1.toString());
Rdata.addAll(data1);
//刷新适配器
myAdapter.setRdata(Rdata);
}
}
@Override
public void Error(Object error) {
Log.e("error",error.toString());
}
}
Adapter适配器
package com.example.asus.week3application.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import com.example.asus.week3application.R;
import com.example.asus.week3application.bean.MyBean;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter
private List
private Context context;
public MyAdapter(List rdata, Context context) {
Rdata = rdata;
this.context = context;
}
public void setRdata(List rdata) {
Rdata = rdata;
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(context, R.layout.item_list, null);
MyViewHolder myViewHolder = new MyViewHolder(inflate);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
myViewHolder.recy_ist.setLayoutManager(linearLayoutManager);
MyAdater2 myAdater2 = new MyAdater2(Rdata.get(i).getList(),context);
myViewHolder.recy_ist.setAdapter(myAdater2);
}
@Override
public int getItemCount() {
return Rdata.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private RecyclerView recy_ist;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
recy_ist = itemView.findViewById(R.id.recy_list);
}
}
}
//第二个适配器
package com.example.asus.week3application.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.asus.week3application.R;
import com.example.asus.week3application.bean.MyBean;
import java.util.List;
public class MyAdater2 extends RecyclerView.Adapter
private List
private Context context;
public MyAdater2(List rlist, Context context) {
Rlist = rlist;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(context, R.layout.recy_item, null);
MyViewHolder myViewHolder = new MyViewHolder(inflate);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.name.setText(Rlist.get(i).getTitle());
myViewHolder.price.setText(Rlist.get(i).getBargainPrice()+"");
String images = Rlist.get(i).getImages();
String[] split = images.split("\\|");
String s = split[0];
String s1 = s.replace("https", "http");
Log.d("zzz",split[0]);
Glide.with(context).load(s1).into(myViewHolder.img);
}
@Override
public int getItemCount() {
return Rlist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
private TextView price;
private TextView name;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
price = itemView.findViewById(R.id.price);
name= itemView.findViewById(R.id.name);
}
}
}