您的位置:首页 > 其它

RecyclerView展示数据 点击图标切换界面布局

2018-02-17 19:04 489 查看
导入依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'添加权限
<uses-permission android:name="android.permission.INTERNET"/>一、MainActivity的布局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.bw.lenovo.lianxi_showrecylcrview_two.MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f89"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:layout_marginTop="8dp"
android:text="商品展示列表"/>

<ImageView
android:id="@+id/icon_change"
android:layout_gravity="right"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/kind_grid"/>
</FrameLayout>

<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="#000000"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

</LinearLayout>
二、页面展示的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:text="商品名称"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"/>
<TextView
android:text="价格:"
android:textColor="#f00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/price"/>
</LinearLayout>
</LinearLayout>
三、OkhttpUtils网络请求
package com.bw.lenovo.lianxi_showrecylcrview_two.Utils;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtils {
// private static String goods_url="http://120.27.23.105/product/getProducts?pscid=39&page=1";
private MyHandler myhandelr = new MyHandler();
private static OkHttpUtils okHttpUtils = null;
private OnLoadListener onLoadListener;

public static OkHttpUtils getInstance() {
if (okHttpUtils == null) {
okHttpUtils = new OkHttpUtils();
}
return okHttpUtils;
}

public void OKGet(String url, String pscid, String page) {
//创建oK对象
OkHttpClient okHttpClient = new OkHttpClient();
//创建请求对象
String url1 = url + "?pscid=" + pscid + "&page=" + page;
Request request = new Request.Builder().url(url1).build();
//创建请求队列
Call call = okHttpClient.newCall(request);
//执行 异步请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message message = myhandelr.obtainMessage();
message.what = 0;
message.obj = e.getMessage();
myhandelr.sendMessage(message);

}

@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = myhandelr.obtainMessage();
message.what = 1;
message.obj = response.body().string();
myhandelr.sendMessage(message);
}
});
}

//处理线程
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
String error = (String) msg.obj;
onLoadListener.LoadError(error);

break;
case 1:
String json = (String) msg.obj;
onLoadListener.LoadSuccess(json);
break;

}
}
}

//定义接口回调的方法
//定义接口
public interface OnLoadListener {
//定义方法
void LoadSuccess(String json);

void LoadError(String Error);
}

//定义一个方法,供外部调用
public void SetOnLoadListener(OnLoadListener onLoadListener) {
this.onLoadListener = onLoadListener;

}
}
四、解析数据(创建一个类解析接口数据)
五、适配器
package com.bw.lenovo.lianxi_showrecylcrview_two.Adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bw.lenovo.lianxi_showrecylcrview_two.Bean.GoodListBean;
import com.bw.lenovo.lianxi_showrecylcrview_two.R;

import java.util.List;

/**
* Created by lenovo on 2018/2/17.
*/

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myviewholder> {
//把context 和list数据传过来
private Context context;
private List<GoodListBean.DataBean> list;

public MyAdapter(Context context, List<GoodListBean.DataBean> list) {
this.context = context;
this.list = list;
}

//创建Viewholder时调用
@Override
public Myviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载布局
View view = View.inflate(context, R.layout.item_horizontal, null);
//实例化viewholder
Myviewholder myviewholder = new Myviewholder(view);
return myviewholder;
}

//关联viewholder时调用
@Override
public void onBindViewHolder(Myviewholder holder, int position) {
//获取图片
String images = list.get(position).getImages();
//将图片按照|竖杠拆分
String icon_url = images.split("\\|")[0];
Glide.with(context).load(icon_url).into(holder.getIcon());
//赋值
holder.getTitle().setText(list.get(position).getTitle());
holder.getPrice().setText(list.get(position).getPrice() + "");
}

@Override
public int getItemCount() {
return list.size();
}

//viewhlder
class Myviewholder extends RecyclerView.ViewHolder {

private final ImageView icon;
private final TextView title;
private final TextView price;

public Myviewholder(View itemView) {
// itemView就是条目的布局文件
super(itemView);
//找到控件,条目里面的控件
icon = itemView.findViewById(R.id.icon);
title = itemView.findViewById(R.id.title);
price = itemView.findViewById(R.id.price);

}

public ImageView getIcon() {
return icon;
}

public TextView getTitle() {
return title;
}

public TextView getPrice() {
return price;
}
}
}
六、MainActivity主页面代码
package com.bw.lenovo.lianxi_showrecylcrview_two;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bw.lenovo.lianxi_showrecylcrview_two.Adapter.MyAdapter;
import com.bw.lenovo.lianxi_showrecylcrview_two.Bean.GoodListBean;
import com.bw.lenovo.lianxi_showrecylcrview_two.Utils.OkHttpUtils;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OkHttpUtils.OnLoadListener {

private static final String TAG = "MainActivity==============";
private static String goods_url = "http://120.27.23.105/product/getProducts";
private RecyclerView recyclerview;
private ImageView icon_change;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化界面
initviews();
//初始化数据
OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
okHttpUtils.OKGet(goods_url, "" + 39, "" + 1);
okHttpUtils.SetOnLoadListener(this);

}

private void initviews() {
//根据id找到控件
icon_change = (ImageView) findViewById(R.id.icon_change);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
icon_change.setOnClickListener(this);
//设置布局管理者
recyclerview.setLayoutManager(new LinearLayoutManager(this));
}

/**
* 开关
* 设置点击按钮布局改变得变量
*/
private boolean flag = true;

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.icon_change:
if (flag) {
//GridView布局
recyclerview.setLayoutManager(new GridLayoutManager(this, 2));
} else {
//listView布局
recyclerview.setLayoutManager(new LinearLayoutManager(this));
}
flag = !flag;
break;
}
}

//接口回调的方法
@Override
public void LoadSuccess(String json) {
Log.d(TAG, "LoadSuccess() retured:成功================" + json);
//gson解析数据
Gson gson = new Gson();
GoodListBean goodListBean = gson.fromJson(json, GoodListBean.class);
List<GoodListBean.DataBean> list = goodListBean.getData();

//设置设配器
MyAdapter adapter = new MyAdapter(MainActivity.this, list);
recyclerview.setAdapter(adapter);

}

@Override
public void LoadError(String Error) {
Log.d(TAG, "LoadError() retured:失败================" + Error);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐