下面关于XRecyclerView 的简单使用
首先是XRecyclerView的布局文件
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity 主界面
private XRecyclerView xrv;
xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
list.clear();
num = 1;
initData();
xrv.refreshComplete();
}
@Override
public void onLoadMore() {
if (num < max) {
num++;
initData();
}
xrv.loadMoreComplete();
}
});
myAdapter.setOnItemClickListener(this);
}
public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {
private int num = 1;
private int max = 10;
private XRecyclerView xrv;
private List<JavaBean.InfoBean.GoodsBean> list;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initListener();
}
private void initListener() {
xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
list.clear();
num = 1;
initData();
xrv.refreshComplete();
}
@Override
public void onLoadMore() {
if (num < max) {
num++;
initData();
}
xrv.loadMoreComplete();
}
});
myAdapter.setOnItemClickListener(this);
}
private void initData() {
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=" + num + "&debug=true&client_id=null";
OkHttpUtils.get().url(url).build().execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
JavaBean javaBean = JSON.parseObject(response, JavaBean.class);
List<JavaBean.InfoBean.GoodsBean> goods = javaBean.getInfo().getGoods();
list.addAll(goods);
myAdapter.addList(list);
}
});
}
private void initView() {
list = new ArrayList<>();
xrv = (XRecyclerView) findViewById(R.id.xrv);
myAdapter = new MyAdapter(list, this);
xrv.setLayoutManager(new GridLayoutManager(this, 2));
xrv.setAdapter(myAdapter);
}
@Override
public void onItemClick(int i) {
JavaBean.InfoBean.GoodsBean goodsBean = list.get(i - 1);
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("goodsBean", goodsBean);
startActivity(intent);
}
}
public class Main2Activity extends AppCompatActivity {
private TextView EnglishName;
private TextView ChinaName;
private TextView money;
private ImageView thumb;
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
initData();
}
private void initData() {
Intent intent = getIntent();
JavaBean.InfoBean.GoodsBean goodsBean = (JavaBean.InfoBean.GoodsBean) intent.getSerializableExtra("goodsBean");
EnglishName.setText(goodsBean.getEnglish_name());
ChinaName.setText(goodsBean.getGoods_name());
money.setText(goodsBean.getShop_price());
message.setText(goodsBean.getDescription());
Glide.with(this).load(goodsBean.getThumb()).placeholder(R.mipmap.ic_launcher).into(thumb);
}
private void initView() {
EnglishName = (TextView) findViewById(R.id.EnglishName);
ChinaName = (TextView) findViewById(R.id.ChinaName);
money = (TextView) findViewById(R.id.money);
thumb = (ImageView) findViewById(R.id.thumb);
message = (TextView) findViewById(R.id.message);
}
public void returnClick(View view) {
finish();
}
}
package it.com.fuxi;
import java.io.Serializable;
import java.util.List;
/**
* Created by dell on 2017/9/21.
*/
public class JavaBean implements Serializable {
private String result;
private String msg;
private InfoBean info;
private String jf_tip;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public InfoBean getInfo() {
return info;
}
public void setInfo(InfoBean info) {
this.info = info;
}
public String getJf_tip() {
return jf_tip;
}
public void setJf_tip(String jf_tip) {
this.jf_tip = jf_tip;
}
public static class InfoBean implements Serializable {
private String cat_name;
private boolean url;
private int cat_id;
private int total_goods_num;
private int total_page_num;
private List<GoodsBean> goods;
public String getCat_name() {
return cat_name;
}
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public boolean isUrl() {
return url;
}
public void setUrl(boolean url) {
this.url = url;
}
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public int getTotal_goods_num() {
return total_goods_num;
}
public void setTotal_goods_num(int total_goods_num) {
this.total_goods_num = total_goods_num;
}
public int getTotal_page_num() {
return total_page_num;
}
public void setTotal_page_num(int total_page_num) {
this.total_page_num = total_page_num;
}
public List<GoodsBean> getGoods() {
return goods;
}
public void setGoods(List<GoodsBean> goods) {
this.goods = goods;
}
public static class GoodsBean implements Serializable {
private int i;
private String shop_price;
private String currency_price;
private String promote_price;
private String rank_price;
private String service_price;
private String shipping_price;
private int is_promote;
private int is_on_sale;
private int is_on_expired;
private int is_recommended;
private String goods_id;
private String goods_name;
private String english_name;
private String thumb;
private String goods_img;
private int add_time;
private String isNew;
private String weblink;
private int colorid;
private String colorname;
private String colorcode;
private String colorurl;
private String description;
private String goods_from;
private String is_favorited;
private List<?> albums;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getShop_price() {
return shop_price;
}
public void setShop_price(String shop_price) {
this.shop_price = shop_price;
}
public String getCurrency_price() {
return currency_price;
}
public void setCurrency_price(String currency_price) {
this.currency_price = currency_price;
}
public String getPromote_price() {
return promote_price;
}
public void setPromote_price(String promote_price) {
this.promote_price = promote_price;
}
public String getRank_price() {
return rank_price;
}
public void setRank_price(String rank_price) {
this.rank_price = rank_price;
}
public String getService_price() {
return service_price;
}
public void setService_price(String service_price) {
this.service_price = service_price;
}
public String getShipping_price() {
return shipping_price;
}
public void setShipping_price(String shipping_price) {
this.shipping_price = shipping_price;
}
public int getIs_promote() {
return is_promote;
}
public void setIs_promote(int is_promote) {
this.is_promote = is_promote;
}
public int getIs_on_sale() {
return is_on_sale;
}
public void setIs_on_sale(int is_on_sale) {
this.is_on_sale = is_on_sale;
}
public int getIs_on_expired() {
return is_on_expired;
}
public void setIs_on_expired(int is_on_expired) {
this.is_on_expired = is_on_expired;
}
public int getIs_recommended() {
return is_recommended;
}
public void setIs_recommended(int is_recommended) {
this.is_recommended = is_recommended;
}
public String getGoods_id() {
return goods_id;
}
public void setGoods_id(String goods_id) {
this.goods_id = goods_id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getEnglish_name() {
return english_name;
}
public void setEnglish_name(String english_name) {
this.english_name = english_name;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getGoods_img() {
return goods_img;
}
public void setGoods_img(String goods_img) {
this.goods_img = goods_img;
}
public int getAdd_time() {
return add_time;
}
public void setAdd_time(int add_time) {
this.add_time = add_time;
}
public String getIsNew() {
return isNew;
}
public void setIsNew(String isNew) {
this.isNew = isNew;
}
public String getWeblink() {
return weblink;
}
public void setWeblink(String weblink) {
this.weblink = weblink;
}
public int getColorid() {
return colorid;
}
public void setColorid(int colorid) {
this.colorid = colorid;
}
public String getColorname() {
return colorname;
}
public void setColorname(String colorname) {
this.colorname = colorname;
}
public String getColorcode() {
return colorcode;
}
public void setColorcode(String colorcode) {
this.colorcode = colorcode;
}
public String getColorurl() {
return colorurl;
}
public void setColorurl(String colorurl) {
this.colorurl = colorurl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGoods_from() {
return goods_from;
}
public void setGoods_from(String goods_from) {
this.goods_from = goods_from;
}
public String getIs_favorited() {
return is_favorited;
}
public void setIs_favorited(String is_favorited) {
this.is_favorited = is_favorited;
}
public List<?> getAlbums() {
return albums;
}
public void setAlbums(List<?> albums) {
this.albums = albums;
}
}
}
}
package it.com.fuxi;
import android.content.Context;
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.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.List;
/**
* Created by dell on 2017/9/21.
*/
public class MyAdapter extends XRecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<JavaBean.InfoBean.GoodsBean> list;
private Context context;
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(int i);
}
public MyAdapter(List<JavaBean.InfoBean.GoodsBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
JavaBean.InfoBean.GoodsBean goodsBean = list.get(position);
Glide.with(context).load(goodsBean.getThumb()).placeholder(R.mipmap.ic_launcher).into(holder.thumb);
holder.EnglishName.setText(goodsBean.getEnglish_name());
holder.ChinaName.setText(goodsBean.getGoods_name());
holder.money.setText(goodsBean.getShop_price() + "元");
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView thumb;
TextView EnglishName;
TextView money;
TextView ChinaName;
public MyViewHolder(View itemView) {
super(itemView);
thumb = (ImageView) itemView.findViewById(R.id.thumb);
EnglishName = (TextView) itemView.findViewById(R.id.EnglishName);
money = (TextView) itemView.findViewById(R.id.money);
ChinaName = (TextView) itemView.findViewById(R.id.ChinaName);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = MyViewHolder.this.getLayoutPosition();
onItemClickListener.onItemClick(position);
}
});
}
}
public void addList(List<JavaBean.InfoBean.GoodsBean> list) {
this.list = list;
notifyDataSetChanged();
}
}
activity_main
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
activity_main2
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efefef"
tools:context="it.com.fuxi.Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:onClick="returnClick"
android:text="return"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="商品详情"
android:textColor="#000"
android:textSize="22sp" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#999" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/EnglishName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="英文名字"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:id="@+id/ChinaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/money"
android:singleLine="true"
android:text="中文名字"
android:textColor="#000"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:text="$355"
android:textColor="#000"
android:textSize="22sp" />
</LinearLayout>
<ImageView
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="350dp"
android:scaleType="centerCrop"
android:src="@drawable/thumb" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="right"
android:paddingRight="30dp"
android:text="心" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/thumb" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#9000"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/EnglishName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:text="英文名字"
android:textColor="#fff"
android:textSize="22sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="$355.00元"
android:textColor="#fff"
android:textSize="22sp" />
<TextView
android:id="@+id/ChinaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/money"
android:singleLine="true"
android:text="$355.00元"
android:textColor="#fff"
android:textSize="22sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
glide-3.6.1
fastjson-1.1.40