public class Contacts {
public static final String BASEURL = “http://www.zhaoapi.cn/product/”;
public static final String URL = “searchProducts?keywords=%E7%AC%94%E8%AE%B0%E6%9C%AC&page=1”;
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<News.DataBean> list;
private Context context;
private int layout;
public MyAdapter(List<News.DataBean> list, Context context,int layout) {
this.list = list;
this.context = context;
this.layout=layout;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
View view = LayoutInflater.from(context).inflate(layout, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
String images = list.get(i).getImages();
String[] splits = images.split("\\|");
Uri parse = Uri.parse(splits[0]);
viewHolder.img.setImageURI(parse);
viewHolder.title.setText(list.get(i).getTitle());
viewHolder.price.setText(list.get(i).getPrice()+"");
//点击
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback!=null){
callback.setClick(v,i);
}
}
});
//长按
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
/*
//
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
News.DataBean dataBean = list.get(i);
EventBus.getDefault().postSticky(dataBean);
context.startActivity(new Intent(context,Main2Activity.class));
}
});
*/
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView img;
private TextView title;
private TextView price;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
title = itemView.findViewById(R.id.title);
price = itemView.findViewById(R.id.price);
}
}
public interface Callback{
void setClick(View view,int position);
void setLongClick(View view,int position);
}
private Callback callback;
public void setCallback(Callback callback){
this.callback = callback;
}
public void onClick(View v) {
if(callback!=null){
callback.setClick(v, (int) v.getTag());
callback.setLongClick(v, (int) v.getTag());
}
}
}
public interface MyCallBack {
void success(T successData);
void error(T errorData);
}
public interface IModel {
void getData(String url, Map<String,String> map, Class cla, MyCallBack myCallBack);
void postData(String url, Map<String,String> map, Class cla, MyCallBack myCallBack);
}
//获取数据
public class MyModel implements IModel {
@Override
public void getData(String url, Map<String, String> map, final Class cla, final MyCallBack myCallBack) {
RetrofitUtils.getInstance().get(url,map).setHttpClient(new RetrofitUtils.CallBacks() {
@Override
public void onSuccess(String jsonStr) {
Gson gson = new Gson();
Object o = gson.fromJson(jsonStr, cla);
myCallBack.success(o);
}
@Override
public void onError(String error) {
}
});
}
@Override
public void postData(String url, Map<String, String> map, final Class cla, final MyCallBack myCallBack) {
RetrofitUtils.getInstance().post(url,map).setHttpClient(new RetrofitUtils.CallBacks() {
@Override
public void onSuccess(String jsonStr) {
Gson gson = new Gson();
Object o = gson.fromJson(jsonStr, cla);
myCallBack.success(o);
Log.e("zzzMod",o+"");
}
@Override
public void onError(String error) {
}
});
}
public interface IPresenter {
void getRequest(String url, Map<String,String> map,Class cla);
void postRequest(String url, Map<String,String> map,Class cla);
}
private MyModel myModel;
private IView iView;
public MyPresenter(IView iView) {
this.iView = iView;
myModel = new MyModel();
}
@Override
public void getRequest(String url, Map<String, String> map, Class cla) {
myModel.postData(url, map, cla, new MyCallBack() {
@Override
public void success(Object successData) {
iView.success(successData);
}
@Override
public void error(Object errorData) {
}
});
}
@Override
public void postRequest(String url, Map<String, String> map, Class cla) {
myModel.postData(url, map, cla, new MyCallBack() {
@Override
public void success(Object successData) {
iView.success(successData);
Log.e("zzzPre",successData+"");
}
@Override
public void error(Object errorData) {
}
});
}
public void onDetch(){
if(myModel != null){
myModel = null;
}
if(iView != null){
iView = null;
}
}
public interface IView<T> {
void success(T success);
void error(T error);
}
private ImageView change;
public HeaderView(Context context,AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.headerview,this);
change = findViewById(R.id.change);
}
public ImageView getImageView(){
return change;
}
public class MainActivity extends AppCompatActivity implements IView {
@BindView(R.id.headerview)
HeaderView headerview;
@BindView(R.id.xrecy)
XRecyclerView xrecy;
private MyPresenter myPresenter;
private List<News.DataBean> list = new ArrayList<>();
private MyAdapter adapter;
boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Fresco.initialize(this); //Fresco注册
LinearLayoutManager manager = new LinearLayoutManager(this);
xrecy.setLayoutManager(manager);
myPresenter = new MyPresenter(this);
adapter = new MyAdapter(list,this,R.layout.item);
Log.e("zzzMain",list.toString());
xrecy.setAdapter(adapter);
Map<String,String> map = new HashMap<>();
myPresenter.getRequest(Contacts.URL,map,News.class);
adapter.setCallback(new MyAdapter.Callback() {
@Override
public void setClick(View view, int position) {
News.DataBean dataBean = list.get(position);
EventBus.getDefault().postSticky(dataBean);
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
@Override
public void setLongClick(View view, int position) {
}
});
headerview.getImageView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag){
flag=false;
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
xrecy.setLayoutManager(manager);
adapter=new MyAdapter(list,MainActivity.this,R.layout.item);
adapter.notifyDataSetChanged();
}else {
flag=true;
GridLayoutManager manager = new GridLayoutManager(MainActivity.this,2);
xrecy.setLayoutManager(manager);
adapter=new MyAdapter(list,MainActivity.this,R.layout.item2);
adapter.notifyDataSetChanged();
}
}
});
}
//view
@Override
public void success(Object success) {
News news = (News) success;
list.addAll(news.getData());
Toast.makeText(this, list.size(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
@Override
public void error(Object error) {
}
@Override
protected void onDestroy() {
super.onDestroy();
if(myPresenter != null){
myPresenter = null;
}
}
@BindView(R.id.images)
ImageView images;
@BindView(R.id.titles)
TextView titles;
@BindView(R.id.prices)
TextView prices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
Fresco.initialize(this);
EventBus.getDefault().register(this);
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void messageEventBus(News.DataBean bean) {
String image = bean.getImages();
String[] splits = image.split("\\|");
Uri parse = Uri.parse(splits[0]);
Toast.makeText(this, parse+"", Toast.LENGTH_SHORT).show();
images.setImageURI(parse);
String title = bean.getTitle();
titles.setText(title);
int price = bean.getPrice();
prices.setText("¥"+price);
}
//防止内存
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}