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);
}
}
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);
}
}
相关文章推荐
- RecyclerView点击切换布局
- Viewpager+Fragment+RecyclerView 在隔页点击切换数数据为空白的解决方法
- Android使用OKHTTP网络框架请求数据,RecyclerView结合Glide展示图片,瀑布流布局样式
- RecyclerView-点击切换多种布局
- 在RecyclerView的子布局中使用EditText,在第一项输入数据滚动出可视界面后,再次回到第一项后,输入的数据没有了
- XrecyclerView加载数据+切换不同布局
- RecyclerView点击切换布局
- RecyclerView中EditText数据混乱,FoldingCell折叠布局第一次点击有问题
- RecyclerView展示固定数据、上拉加载更多、下拉刷新、点击事件、长按点击事件、删除条目、刷新条目、添加条目、多条目加载
- MVP+retrofit和Rxjava请求数据+recyclerview展示
- RecyclerView点击事件、加载不同布局
- RecyclerView的多条目展示,使用OkHttp解析数据
- MVP架构分包+OkHttp网络请求数据并展示到xrecyclerview上
- Android RecyclerView一键快速切换布局样式
- Android商城开发系列(七)—— 使用RecyclerView展示首页数据
- 关于recyclerview的横向布局的宽度以及item的点击事件的添加
- 优雅的使用RecyclerView(在一个recyclerView里显示有不同子布局的界面,就是混合不同布局)
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- ViewPager实现页面切换 (包含App第一次登录时候的欢迎界面的展示)
- TabLayout +ViewPager + Fragment : RecyclerView展示网络数据 + webView 显示网页