关于okhttputils的简单使用(含 XRecyclerView的加载,刷新)




import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.example.lenovo.shishnagyuan.R;
import com.example.lenovo.shishnagyuan.adapter.MyNewon;
import com.example.lenovo.shishnagyuan.url.Newon;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;



public class Fragment_newon extends Fragment {
private XRecyclerView xRecyclerView;
private static int i=20;
private RequestQueue mQueue;
private MyNewon adapter;

public String url="http://atp.fulishe.com/ClientApi/category.php?api_version=1.0&" +
"act=search_category_goods_list&c_id=35&order_price=0&page_num=20" +
"&page=1&debug=true&client_id=null";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.newon, null);
xRecyclerView = view.findViewById(R.id.xrecyclerview1);
mQueue = Volley.newRequestQueue(getActivity());
initUrils(url);
return view;
}
private void initUrils(final String url) {
OkHttpUtils
.get()
.url(url)
.build()
.execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
Toast.makeText(getActivity(), "网络连接失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(String response) {
Newon javaBren= JSONObject.parseObject(response,Newon.class);
adapter = new MyNewon(javaBren,getActivity());
xRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
xRecyclerView.setAdapter(adapter);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
// new_rv.refreshComplete(); //下拉刷新完成
}
@Override
public void onLoadMore() {
i+=10;
initUrils(url);
xRecyclerView.loadMoreComplete();//加载更多完成
}
});
}
});
}
}

============================================================================================================

MySP
import android.content.Context;
import android.content.Intent;
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 com.bumptech.glide.Glide;
import com.example.lenovo.shishnagyuan.R;
import com.example.lenovo.shishnagyuan.details.ShangPinX;
import com.example.lenovo.shishnagyuan.url.Newon;


/**
* Created by lenovo-ai on 2017-08-04.
*/

public class MyNewon extends RecyclerView.Adapter<MyNewon.ViewHodler> {
private Newon datas;
private Context context;

public MyNewon(Newon datas, Context context) {
this.datas = datas;
this.context = context;
}


@Override
public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
MyNewon.ViewHodler hodler= new MyNewon.ViewHodler(LayoutInflater.from(context).inflate(R.layout.newon_two,parent,false));
return hodler;
}

@Override
public void onBindViewHolder(ViewHodler holder, final int position) {
Glide.with(context).load(datas.getInfo().getGoods().get(position).getThumb()).into(holder.newon_iv);
holder.newon_tv1.setText(datas.getInfo().getGoods().get(position).getEnglish_name());
holder.newon_tv2.setText(datas.getInfo().getGoods().get(position).getGoods_name());
holder.newon_tv3.setText(datas.getInfo().getGoods().get(position).getShop_price());
holder.newon_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context, ShangPinX.class);
intent.putExtra("id1",position+"");
context.startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return datas.getInfo().getGoods().size();
}

public class ViewHodler extends RecyclerView.ViewHolder {
private ImageView newon_iv;
private TextView newon_tv1,newon_tv2,newon_tv3;
public ViewHodler(View itemView) {
super(itemView);
newon_iv = (ImageView) itemView.findViewById(R.id.newon_iv);
newon_tv1 = (TextView) itemView.findViewById(R.id.newon_tv1);
newon_tv2 = (TextView) itemView.findViewById(R.id.newon_tv2);
newon_tv3 = (TextView) itemView.findViewById(R.id.newon_tv3);
}
}
}


//布局

<TextView
android:layout_marginTop="10dp"
android:id="@+id/dl"
android:text="新品"
android:textSize="35dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/hx"
android:background="#000000"
android:layout_below="@+id/dl"
android:layout_width="match_parent"
android:layout_height="1dp" />

<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrecyclerview1"
android:layout_below="@+id/hx"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

</com.jcodecraeer.xrecyclerview.XRecyclerView>
implements Serializable









posted @ 2017-09-21 20:52  于大大  阅读(434)  评论(0编辑  收藏  举报