加依赖:
implementation 'com.youth.banner:banner:1.4.9'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.jcodecraeer:xrecyclerview:1.2.0'
记住写权限:
布局:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".v.MainActivity">
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:id="@+id/my_banner"
></com.youth.banner.Banner>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:id="@+id/recy_id"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
listview01:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:text="哈哈"
android:textSize="20sp"
android:padding="10dp"
android:id="@+id/text_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:padding="10dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/image_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
listview02:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:padding="10dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/image_02"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:textSize="20sp"
android:text="哈哈"
android:padding="10dp"
android:id="@+id/text_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
主方法:
工具类:
public class OkhttpUtil {
static OkhttpUtil util;
OkHttpClient okHttpClient;
//构造方法私有化
private OkhttpUtil(){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyIntercept()).build();
}
public static synchronized OkhttpUtil getInstance(){
if (util==null){
util=new OkhttpUtil();
}
return util;
}
public void doGet(Callback callback){
Request request=new Request.Builder()
.url("http://172.17.8.100/small/commodity/v1/bannerShow")
.get()
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(callback);
}
public void getGet(String url, Map<String,String>map,Callback callback){
StringBuilder builder=new StringBuilder();
if (map!=null&&map.size()>0){
for (String key:map.keySet()){
String value=map.get(key);
builder.append(key)
.append("=")
.append(value)
.append("&");
}
String str=builder.toString();
int index = str.lastIndexOf("&");
str=str.substring(0,index);
url=url+"?"+str;
Request request=new Request.Builder()
.url(url)
.get()
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(callback);
}
}
public class MyIntercept implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException{
Request request=chain.request();
Response response=chain.proceed(request);
return response;
}
}
}
contarct类:
public interface ContarctIntface {
//view层接口
public interface ProductViewIntface{
public void ShowImage(Object obj);
public void ShowDisplay(Object obj);
}
//p层接口
public interface PresenterIntface{
public void toImage();
public void toProduct(int page);
}
}
m层:
public class MyModel {
MyCallBack myCallBack;
MyCallBacks myCallBacks;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int type = msg.arg1;
String json=(String)msg.obj;
if (type==1){
Log.e("ddd",""+json);
Gson gson=new Gson();
PickBean pickBean=gson.fromJson(json,PickBean.class);
List<Pick> result = pickBean.getResult();
Log.e("bbb",""+result);
myCallBack.success(result);
}
if (type==2){
Log.e("aaaa",""+json);
Gson gson=new Gson();
ProductBean productBean = gson.fromJson(json,ProductBean.class);
List<Product> s = productBean.getResult();
Log.e("bbbb",""+s);
myCallBacks.successes(s);
}
}
};
public void getRequest(){
OkhttpUtil util = OkhttpUtil.getInstance();
util.doGet(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.obj=json;
message.arg1=1;
handler.sendMessage(message);
}
});
}
public void getResponse(String url, Map<String,String>map){
OkhttpUtil util=OkhttpUtil.getInstance();
util.getGet(url, map, 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.arg1=2;
message.obj=json;
handler.sendMessage(message);
}
});
}
public void setMyCallBack(MyCallBack myCallBack){
this.myCallBack=myCallBack;
}
public void setMyCallBacks(MyCallBacks myCallBacks){
this.myCallBacks=myCallBacks;
}
public interface MyCallBack{
public void success(Object obj);
}
public interface MyCallBacks{
public void successes(Object obj);
}
}
p层:
public class MyPresenter<T> implements ContarctIntface.PresenterIntface {
MyModel myModel;
T tt;
public MyPresenter(T t){
this.tt=t;
myModel=new MyModel();
}
@Override
public void toImage() {
myModel.setMyCallBack(new MyModel.MyCallBack() {
@Override
public void success(Object obj) {
List<Pick>list= (List<Pick>) obj;
Log.e("ppp",""+list);
ContarctIntface.ProductViewIntface productViewIntface=(ContarctIntface.ProductViewIntface) tt;
productViewIntface.ShowImage(list);
}
});
myModel.getRequest();
}
@Override
public void toProduct(int page) {
String url="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
myModel.setMyCallBacks(new MyModel.MyCallBacks() {
@Override
public void successes(Object obj) {
List<Product>list=(List<Product>) obj;
Log.e("uuu",""+list);
ContarctIntface.ProductViewIntface productViewIntface=(ContarctIntface.ProductViewIntface) tt;
productViewIntface.ShowDisplay(list);
}
});
Map<String,String>map=new HashMap<>();
map.put("keyword","手机");
map.put("page",page+"");
map.put("count","20");
myModel.getResponse(url,map);
}
}
v层:
public class MainActivity extends AppCompatActivity implements ContarctIntface.ProductViewIntface{
Banner banner;
XRecyclerView xRecyclerView;
ContarctIntface.PresenterIntface presenterIntface;
List<Pick>mlist=new ArrayList<>();
List<Product>slist=new ArrayList<>();
List<String>plist=new ArrayList<>();
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner=findViewById(R.id.my_banner);
xRecyclerView=findViewById(R.id.recy_id);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(layoutManager);
xRecyclerView=findViewById(R.id.recy_id);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setPullRefreshEnabled(true);
presenterIntface=new MyPresenter(this);
myAdapter=new MyAdapter(slist,this);
xRecyclerView.setAdapter(myAdapter);
presenterIntface=new MyPresenter(this);
presenterIntface.toImage();
presenterIntface.toProduct(1);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
slist.clear();
presenterIntface.toProduct(1);
}
int page=1;
@Override
public void onLoadMore() {
page++;
presenterIntface.toProduct(page);
}
});
}
@Override
public void ShowImage(Object obj) {
List<Pick>list=(List<Pick>) obj;
mlist.addAll(list);
Log.e("aaa",""+mlist);
for (int i=0;i<mlist.size();i++){
plist.add(mlist.get(i).getImageUrl());
}
Log.e("ggg",""+plist);
banner.isAutoPlay(true);
banner.setDelayTime(2000);
banner.setImages(plist);
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
});
banner.start();
}
@Override
public void ShowDisplay(Object obj) {
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
List<Product>list=(List<Product>) obj;
slist.addAll(list);
myAdapter.notifyDataSetChanged();
}
}
适配器MyAdapter类:
public class MyAdapter extends XRecyclerView.Adapter<MyAdapter.Holder>{
List<Product> list;
Context context;
int type=1;
public MyAdapter(List<Product> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (type==1){
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listview01,null);
return new Holder(view);
}else {
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listview02,null);
return new Holder(view);
}
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
if (type==1){
String title=list.get(i).getCommodityName();
String imgUrl=list.get(i).getMasterPic();
holder.textView1.setText(title);
Glide.with(context).load(imgUrl).into(holder.imageView1);
}else {
String title=list.get(i).getCommodityName();
String imgUrl=list.get(i).getMasterPic();
holder.textView2.setText(title);
Glide.with(context).load(imgUrl).into(holder.imageView2);
}
}
@Override
public int getItemViewType(int position) {
type=position%2;
if (type==0){
type=2;
return type;
}else {
type=1;
return type;
}
}
@Override
public int getItemCount() {
if (list!=null){
return list.size();
}
return 0;
}
public class Holder extends XRecyclerView.ViewHolder{
public TextView textView1,textView2;
public ImageView imageView1,imageView2;
public Holder(@NonNull View itemView) {
super(itemView);
if (type==1){
textView1=itemView.findViewById(R.id.text_01);
imageView1=itemView.findViewById(R.id.image_01);
}else {
textView2=itemView.findViewById(R.id.text_02);
imageView2=itemView.findViewById(R.id.image_02);
}
}
}
}
Bean类:
Pick
public class Pick {
public String imageUrl;
public String jumpUrl;
public String rank;
public String title;
public Pick(String imageUrl, String jumpUrl, String rank, String title) {
this.imageUrl = imageUrl;
this.jumpUrl = jumpUrl;
this.rank = rank;
this.title = title;
}
@Override
public String toString() {
return "Pick{" +
"imageUrl='" + imageUrl + '\'' +
", jumpUrl='" + jumpUrl + '\'' +
", rank='" + rank + '\'' +
", title='" + title + '\'' +
'}';
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getJumpUrl() {
return jumpUrl;
}
public void setJumpUrl(String jumpUrl) {
this.jumpUrl = jumpUrl;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
PickBean:
public class PickBean {
public List<Pick> result;
public PickBean(List<Pick> result) {
this.result = result;
}
@Override
public String toString() {
return "PickBean{" +
"result=" + result +
'}';
}
public List<Pick> getResult() {
return result;
}
public void setResult(List<Pick> result) {
this.result = result;
}
}
product:
public class Product {
public String commodityId;
public String commodityName;
public String masterPic;
public String price;
public String saleNum;
public Product(String commodityId, String commodityName, String masterPic, String price, String saleNum) {
this.commodityId = commodityId;
this.commodityName = commodityName;
this.masterPic = masterPic;
this.price = price;
this.saleNum = saleNum;
}
public String getCommodityId() {
return commodityId;
}
public void setCommodityId(String commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getSaleNum() {
return saleNum;
}
public void setSaleNum(String saleNum) {
this.saleNum = saleNum;
}
@Override
public String toString() {
return "Product{" +
"commodityId='" + commodityId + '\'' +
", commodityName='" + commodityName + '\'' +
", masterPic='" + masterPic + '\'' +
", price='" + price + '\'' +
", saleNum='" + saleNum + '\'' +
'}';
}
}
productBean:
public class ProductBean {
public List<Product> result;
public ProductBean(List<Product> result) {
this.result = result;
}
@Override
public String toString() {
return "ProductBean{" +
"result=" + result +
'}';
}
public List<Product> getResult() {
return result;
}
public void setResult(List<Product> result) {
this.result = result;
}
}