xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="商品列表"
android:gravity="center"
android:textSize="30sp"
/>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:id="@+id/pull_torefresh"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:id="@+id/main_zonghe"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/zonghe"
android:textSize="25sp"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/main_xiaoliang"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/gao"
android:textSize="25sp"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/main_zuijin"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/juli"
android:textSize="25sp"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/main_shuaixuan"
android:layout_height="match_parent"
android:layout_width="0dp"
android:textSize="25sp"
android:text="@string/shaixuan"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:id="@+id/recycler"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
mainactivity:
package com.example.monizhoukao2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ScrollView;
import android.widget.Toast;
import com.example.monizhoukao2.adapter.ShowAdapter;
import com.example.monizhoukao2.bean.ShowBean;
import com.example.monizhoukao2.presenter.ShowPresenterimpl;
import com.example.monizhoukao2.view.MainView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MainView{
private RecyclerView recycler;
private PullToRefreshScrollView pull_torefresh;
private ShowPresenterimpl showPresenterimpl;
private int page=0;
private List<ShowBean.DataBean> data;
private ShowAdapter adapter;
private boolean load=true;
private boolean push=false;
private boolean pull=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
recycler = findViewById(R.id.recycler);
pull_torefresh = findViewById(R.id.pull_torefresh);
showPresenterimpl = new ShowPresenterimpl(this);
showPresenterimpl.getData(page);
recycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
pull_torefresh.setMode(PullToRefreshBase.Mode.BOTH);
pull_torefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
change(false,true,false);
if(page>1){
page--;
showPresenterimpl.getData(page);
}else{
pull_torefresh.onRefreshComplete();
}
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
change(false,false,true);
page++;
showPresenterimpl.getData(page);
}
});
}
public void change(boolean b1,boolean b2,boolean b3) {
push = b1;
push = b2;
pull = b3;
}
@Override
public void onSuccess(final ShowBean showBean) {
data = showBean.getData();
if(data!=null){
runOnUiThread(new Runnable() {
@Override
public void run() {
if (load) {
adapter = new ShowAdapter(MainActivity.this,data);
recycler.setAdapter(adapter);
}
if(push){
adapter.add(data);
}else if(pull){
adapter.load(data);
}else
Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show();
pull_torefresh.onRefreshComplete();
adapter.setCallBack(new ShowAdapter.ClickListener() {
@Override
public void onItemClick(int position) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("title",showBean.getData().get(position).getName());
startActivity(intent);
}
});
}
});
}
}
@Override
public void onError(int code) {
}
}
adapter:
package com.example.monizhoukao2.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.monizhoukao2.MainActivity;
import com.example.monizhoukao2.R;
import com.example.monizhoukao2.bean.ShowBean;
import java.util.List;
import javax.security.auth.callback.Callback;
/**
* Created by 小薇 on 2018/7/22.
*/
public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.ViewHolder>{
private Context context;
private List<ShowBean.DataBean> dataBeans;
public ShowAdapter(Context context, List<ShowBean.DataBean> dataBeans) {
this.context = context;
this.dataBeans = dataBeans;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.showlist, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.shop.setText(dataBeans.get(position).getMonth_sales_tip());
holder.title.setText(dataBeans.get(position).getName());
String pic_url = dataBeans.get(position).getPic_url();
Glide.with(context).load(pic_url).into(holder.ion);
if(callBack!=null){
holder.relate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callBack.onItemClick(position);
}
});
}
}
@Override
public int getItemCount() {
return dataBeans.size();
}
public void load(List<ShowBean.DataBean> data) {
this.dataBeans.addAll(data);
notifyDataSetChanged();
}
public void add(List<ShowBean.DataBean> data) {
this.dataBeans.addAll(0,data);
}
class ViewHolder extends RecyclerView.ViewHolder{
private ImageView ion;
private TextView title;
private TextView shop;
private RelativeLayout relate;
public ViewHolder(View itemView) {
super(itemView);
ion = itemView.findViewById(R.id.ion);
title = itemView.findViewById(R.id.show_title);
shop = itemView.findViewById(R.id.shop);
relate = itemView.findViewById(R.id.relate);
}
}
public interface ClickListener{
void onItemClick(int position);
}
public ClickListener callBack;
public void setCallBack(ClickListener callBack) {
this.callBack = callBack;
}
}