在这里插入代码片package com.bawei.weeklx.fragmen;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout;
import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayoutDirection;
import com.bawei.weeklx.R;
import com.bawei.weeklx.SearchView;
import com.bawei.weeklx.adapter.MyAdapter;
import com.bawei.weeklx.base.BaseFragment;
import com.bawei.weeklx.presenter.ShowPresenter;
import com.bawei.weeklx.view.ShowView;
import org.json.JSONArray;
/**
*/
public class Frag01 extends BaseFragment implements ShowView {
private Handler handler = new Handler();
private RecyclerView recyclerView;
private ShowPresenter showPresenter;
private com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout swipyRefreshLayout;
int page = 5;
String sou="板鞋";
@Override
protected int layoutID() {
return R.layout.frag01;
}
@Override
protected void initView(View view) {
final com.bawei.weeklx.SearchView searchView = view.findViewById(R.id.sv);
showPresenter = new ShowPresenter(this);
recyclerView = view.findViewById(R.id.rv);
swipyRefreshLayout = view.findViewById(R.id.srl);
showPresenter.attachView(this);
//获取布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
searchView.setSearchLisenter(new SearchView.OnSearchLisenter() {
@Override
public void onResult(String text) {
sou=text;
showPresenter.relaed(sou,page);
}
});
}
@Override
protected void initData() {
swipyRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
swipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(int index) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
swipyRefreshLayout.setRefreshing(false);
page=5;
showPresenter.relaed(sou,page);
}
},2000);
}
@Override
public void onLoad(int index) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
page++;
showPresenter.relaed(sou,page+4);
swipyRefreshLayout.setRefreshing(false);
}
},2000);
}
});
}
@Override
public void showw(JSONArray result) {
final MyAdapter myAdapter = new MyAdapter(getActivity(), result);
recyclerView.setAdapter(myAdapter);
myAdapter.setLongitemLisenter(new MyAdapter.OnLongitemLisenter() {
@Override
public void OnDel(int i) {
myAdapter.onDelete(i);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
showPresenter.detachView();
Log.d("xxx","销毁了");
}
}
package com.bawei.weeklx.presenter;
import com.bawei.weeklx.model.ShowModel;
import com.bawei.weeklx.view.ShowView;
import org.json.JSONArray;
/**
*/
public class ShowPresenter {
private final ShowModel showModel;
private final ShowView showView;
public ShowPresenter(ShowView view) {
showModel = new ShowModel();
showView = view;
}
public void relaed(String text,int page) {
showModel.show(text,page);
showModel.setShowLisenter(new ShowModel.OnShowLisenter() {
@Override
public void show(JSONArray result) {
showView.showw(result);
}
});
//解决内存泄漏
public void attachView(T t){
reference = new WeakReference<>(t);
}
public void detachView(){
if (reference.get()!=null){
reference.clear();
reference=null;
}
}
}
}
package com.bawei.weeklx.model;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.bawei.weeklx.utils.OkHttpUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
*/
public class ShowModel {
public interface OnShowLisenter {
void show(JSONArray result);
}
private OnShowLisenter showLisenter;
public void setShowLisenter(OnShowLisenter showLisenter) {
this.showLisenter = showLisenter;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
String json = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray("result");
if (showLisenter!=null){
showLisenter.show(result);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
};
public void show(String text, int page) {
String url="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword="+text+"&page=1&count="+page;
OkHttpUtils.getInstance().doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.what = 0;
message.obj = json;
handler.sendMessage(message);
}
});
}
}
package com.bawei.weeklx.view;
import org.json.JSONArray;
/**
*/
public interface ShowView {
void showw(JSONArray result);
}
package com.bawei.weeklx.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.TextView;
import android.widget.Toast;
import com.bawei.weeklx.R;
import com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private JSONArray result;
public MyAdapter(Context context, JSONArray result) {
this.context = context;
this.result = result;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, null, false);
final MyViewHolder myViewHolder = new MyViewHolder(view);
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int ii = myViewHolder.getLayoutPosition();
if (longitemLisenter!=null){
longitemLisenter.OnDel(ii);
Toast.makeText(context,""+ii,Toast.LENGTH_SHORT).show();
}
return true;
}
});
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
try {
JSONObject jsonObject = result.getJSONObject(i);
String commodityName = jsonObject.getString("commodityName");
String masterPic = jsonObject.getString("masterPic");
myViewHolder.textView.setText(commodityName);
Glide.with(context).load(masterPic).into(myViewHolder.imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return result.length();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView textView;
private final ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.item_img);
textView = itemView.findViewById(R.id.item_tv);
}
}
public interface OnLongitemLisenter{
void OnDel(int i);
}
private OnLongitemLisenter longitemLisenter;
public void setLongitemLisenter(OnLongitemLisenter longitemLisenter) {
this.longitemLisenter = longitemLisenter;
}
public void onDelete(int i){
result.remove(i);
notifyDataSetChanged();
}
}
上下拉刷新包含recycleview展示数据
最新推荐文章于 2019-07-05 17:32:19 发布