您的位置:首页 > 其它

MVP登录注册+流式布局+搜索商品切换

2018-01-07 20:30 399 查看

——首先要做的需求 是登录注册 + 流式布局 + 搜索切换页面 > 所以 登录是Main开头的 注册是ZhuCe开头的 流式是 Sousuo 搜索切换是Shop

layout 布局

> activity_main.xml 登录的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="weilin.bwei.com.week2_dome.view.activity.MainActivity">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="登录"
/>
<View
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"></View>
<LinearLayout
android:id="@+id/ln"
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/et_name"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="请输入手机号"/>
<EditText
android:id="@+id/et_pass"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="请输入密码"/>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/ln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp"
android:gravity="center">
<Button
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
<Button
android:onClick="zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>

</LinearLayout>
</RelativeLayout>


> activity_main2.xml 立即注册的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="weilin.bwei.com.week2_dome.view.activity.Main2Activity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="登录"
/>
<View
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"></View>
<LinearLayout
android:id="@+id/ln"
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="请输入手机号"/>
<EditText
android:id="@+id/pass"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="请输入密码"/>
</LinearLayout>
<Button
android:onClick="liji"
android:layout_below="@id/ln"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="立即注册"/>
</RelativeLayout>


> activity_sousuo.xml 流式布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="weilin.bwei.com.week2_dome.view.activity.SousuoActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
tools:ignore="TextFields" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:onClick="sousuo"
android:text="搜索"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="29dp">
<TextView
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="热搜"
/>
<weilin.bwei.com.week2_dome.view.activity.ZDY
android:layout_width="wrap_content"
android:layout_height="120dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="     张国荣"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="张国荣"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="张国荣"
/>
</weilin.bwei.com.week2_dome.view.activity.ZDY>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="center"
android:text="历史记录"
/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.87"
></ListView>
<Button
android:onClick="dele"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空历史记录"
/>
</LinearLayout>
</LinearLayout>


> item_sousuo.xml

<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
/>
</LinearLayout>


> activity_shop.xml 搜索商品 切换的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="weilin.bwei.com.week2_dome.view.activity.ShopActivity">
<LinearLayout
android:id="@+id/ln"
android:layout_width="368dp"
android:layout_height="50dp"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"
android:orientation="horizontal">
<EditText
android:id="@+id/et_sousuo"
android:layout_weight="9"
android:layout_width="0dp"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:hint="输入搜索关键字"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sousuo"
android:text="搜索"
/>
<ImageView
android:onClick="huan"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
tools:ignore="ContentDescription" />
</LinearLayout>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:layout_below="@+id/ln"
android:id="@+id/xrecy"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>


> item_shop.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/item1_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item1_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="标题 标题 标题 标题 ........"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/item1_img"
android:layout_toEndOf="@+id/item1_img" />
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="¥22.90"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_centerHorizontal="true" />
</LinearLayout>

</LinearLayout>


util 工具类

> Bean 有两个 一个是图片切换需要的bean ShopBean 。还有一个是注册需要的ZhuceBean

> OkHttp3Util

public class OkHttp3Util {

/**
* 懒汉 安全 加同步
* 1.私有的静态成员变量 只声明不创建
* 2.私有的构造方法
* 3.提供返回实例的静态方法
*/
private static OkHttpClient okHttpClient = null;

private OkHttp3Util() {
}

public static OkHttpClient getInstance() {

if (okHttpClient == null) {
//加同步安全
synchronized (OkHttp3Util.class) {
if (okHttpClient == null) {
//okhttp可以缓存数据....指定缓存路径
File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
//指定缓存大小
int cacheSize = 10 * 1024 * 1024;

okHttpClient = new OkHttpClient.Builder()//构建器
.connectTimeout(15, TimeUnit.SECONDS)//连接超时
.writeTimeout(20, TimeUnit.SECONDS)//写入超时
.readTimeout(20, TimeUnit.SECONDS)//读取超时

.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))//设置缓存
.build();
}
}

}

return okHttpClient;
}

/**
* get请求
* 参数1 url
* 参数2 回调Callback
*/

public static void doGet(String oldUrl, Callback callback) {

//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建Request
Request request = new Request.Builder().url(oldUrl).build();
//得到Call对象
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);

}

/**
* post请求
* 参数1 url
* 参数2 Map<String, String> params post请求的时候给服务器传的数据
*      add..("","")
*      add()
*/

public static void doPost(String url, Map<String, String> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//3.x版本post请求换成FormBody 封装键值对参数

FormBody.Builder builder = new FormBody.Builder();
//遍历集合,,,map集合遍历方式
for (String key : params.keySet()) {
builder.add(key, params.get(key));

}

//创建Request....formBody...new formBody.Builder()...add()....build()
Request request = new Request.Builder().url(url).post(builder.build()).build();

Call call = okHttpClient.newCall(request);
call.enqueue(callback);

}

/**
* post请求上传文件....包括图片....流的形式传任意文件...
* 参数1 url
* file表示上传的文件
* fileName....文件的名字,,例如aaa.jpg
* params ....传递除了file文件 其他的参数放到map集合
*
*/
public static void uploadFile(String url, File file, String fileName,Map<String,String> params,Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();

//MultipartBody多功能的请求实体对象,,,formBody只能传表单形式的数据
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);

//参数
if (params != null){
for (String key : params.keySet()){
builder.addFormDataPart(key,params.get(key));
}
}
//文件...参数name指的是请求路径中所接受的参数...如果路径接收参数键值是fileeeee,此处应该改变
builder.addFormDataPart("file",fileName,RequestBody.create(MediaType.parse("application/octet-stream"),file));

//构建
MultipartBody multipartBody = builder.build();

//创建Request
Request request = new Request.Builder().url(url).post(multipartBody).build();

//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(callback);

}

/**
* Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"}
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getInstance().newCall(request);
call.enqueue(callback);

}

/**
* 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装
* 参数er:请求Url
* 参数san:保存文件的文件夹....download
*/
public static void download(final Activity context, final String url, final String saveDir) {
Request request = new Request.Builder().url(url).build();
Call call = getInstance().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//com.orhanobut.logger.Logger.e(e.getLocalizedMessage());
}

@Override
public void onResponse(Call call, final Response response) throws IOException {

InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();//以字节流的形式拿回响应实体内容
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));

fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}

fos.flush();

context.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show();
}
});

//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);

} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();

}
}
});

}

/**
* 判断下载目录是否存在......并返回绝对路径
*
* @param saveDir
* @return
* @throws IOException
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}

/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}

}


Adapter

> Adapter 流式布局的适配器

public class Adapter extends BaseAdapter{

List<String> list;
Context context;

public Adapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}

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

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
viewholder vh;
if (view == null){
vh = new viewholder();
view = View.inflate(context, R.layout.item_sousuo,null);
vh.tv = view.findViewById(R.id.tv);
view.setTag(vh);
}else{
vh = (viewholder) view.getTag();
}
//赋值
vh.tv.setText(list.get(i).toString());
return view;
}
class viewholder{
TextView tv;
}
}


> ShopAdapter 搜索商品切换的适配器

public class ShopAdapter extends XRecyclerView.Adapter<ShopAdapter.vieholder>{

Context context;
List<ShopBean.DataBean> data;

public ShopAdapter(Context context, List<ShopBean.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public ShopAdapter.vieholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item_shop,parent,false);

vieholder vieholder=new vieholder(view);
return vieholder;
}

@Override
public void onBindViewHolder(ShopAdapter.vieholder holder, int position) {
holder.tv1.setText(data.get(position).getTitle());
holder.tv2.setText("$"+data.get(position).getBargainPrice());
String[] split = data.get(position).getImages().split("\\|");
Glide.with(context).load(split[0]).into(holder.item_img);

}

@Override
public int getItemCount() {
return data.size();
}
class vieholder extends RecyclerView.ViewHolder {
ImageView item_img;
TextView tv1,tv2;
public vieholder(View itemView) {
super(itemView);
item_img=itemView.findViewById(R.id.item1_img);
tv1=itemView.findViewById(R.id.item1_title);
tv2=itemView.findViewById(R.id.item_price);

}
}
}


Model层

> Maindoel 登录的M层

public class MainModel {
public void Netinfo(String number, String pass, final Onfinish onfinish){

OkHttp3Util.doGet("http://120.27.23.105/user/login?mobile="+number+"&password="+pass, new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String string = response.body().string();
onfinish.onSuccess(string);
}
}
});
}
}


> ShopModel 搜索商品切换的M层

public class ShopModel {

public void getSou(String keyname, int page, final ShopIpterface shopIpterface){
OkHttp3Util.doGet("https://www.zhaoapi.cn/product/searchProducts?keywords="+keyname+"&page="+page+"&source=android", new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){

//得到请求下来的数据
String string = response.body().string();
Log.d("TAG", "M层: "+string);
Gson gson = new Gson();
//解析
ShopBean shopBean = gson.fromJson(string, ShopBean.class);
shopIpterface.onSuccess(shopBean);

}
}
});
}
}


> ZhuceModel 注册的M层

public class ZhuceMondle {

ZhuIpterface zhuIpterface;
public ZhuceMondle(ZhuIpterface zhuIpterface) {
this.zhuIpterface=zhuIpterface;
}

public void getCart(String s, String name, String pass) {
Map<String, String> z=new HashMap<>();

z.put("mobile",name);
z.put("password",pass);
OkHttp3Util.doPost(s, z, new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}
@Override
public void onResponse(Call call, Response response) throws IOException {

if (response.isSuccessful())
{
String json = response.body().string();
ZhuceBean zhuceBean = new Gson().fromJson(json, ZhuceBean.class);
zhuIpterface.chenggong(zhuceBean);
}
}
});
}
}


Persenter

> MainPersenter 登录的P层

public class MainPersenter implements Onfinish {

LoginView loginView;
private MainModel mainModel;

public MainPersenter(LoginView loginView) {
this.loginView = loginView;
mainModel = new MainModel();

}

public void Net(String number, String pass){
mainModel.Netinfo(number,pass,this);
}

@Override
public void onSuccess(String string) {
loginView.onSuccess(string);
}
}


登陆的接口

public interface Onfinish {
void onSuccess(String string);
}


> ShopPersenter 搜索切换的P层

public class ShopPersenter implements ShopIpterface{
ShopSterface shopSterface;
ShopModel shopModel;

public ShopPersenter(ShopSterface shopSterface) {
this.shopSterface=shopSterface;
shopModel = new ShopModel();

}

public void getSou(String keyname, int page){
shopModel.getSou(keyname,page,this);
}

@Override
public void onSuccess(ShopBean shopBean) {
shopSterface.onSuccess(shopBean);
}
}


搜索切换展示的接口

public interface ShopIpterface {
void onSuccess(ShopBean shopBean);
}


> ZhuCepersenter 注册的P层

public class ZhuCeperester implements ZhuIpterface {

private  ZhuceMondle zhuceMondle;
ZhuceMterface zhuceMterface;
public ZhuCeperester(ZhuceMterface zhuceMterface) {
this.zhuceMterface=zhuceMterface;

zhuceMondle = new ZhuceMondle(this);
}

public void getCart(String s, String name, String pass) {
zhuceMondle.getCart(s,name,pass);
}

@Override
public void chenggong(ZhuceBean zhuceBean) {
zhuceMterface.cheng(zhuceBean);
}
}


注册的接口

public interface ZhuIpterface {
void chenggong(ZhuceBean zhuceBean);
}


View

> activity

MainActivity 登录的 V层

public class MainActivity extends AppCompatActivity implements LoginView {

private EditText name;
private EditText pass;
private MainPersenter mainPersenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.et_name);
pass = (EditText) findViewById(R.id.et_pass);
mainPersenter = new MainPersenter(this);
}
public void login(View view){
String text = name.getText().toString();
String text2 = pass.getText().toString();
mainPersenter.Net(text,text2);

}
public void zhuce(View view){
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
@Override
public void onSuccess(final String string) {
if (string!=null){
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(string);
String msg = jsonObject.getString("msg");
if ("登录成功".equals(msg)){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,SousuoActivity.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}

}
});
}
}
}


登录的接口

public interface LoginView {
void onSuccess(String string);
}


> Main2Activity 立即注册的 V层

public class Main2Activity extends AppCompatActivity implements ZhuceMterface{

private EditText name;
private EditText pass;
private ZhuCeperester zhuCeperester;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
name = (EditText) findViewById(R.id.name);
pass = (EditText) findViewById(R.id.pass);
zhuCeperester = new ZhuCeperester(this);
}
public void liji(View view)
{
String sjh = name.getText().toString();
String mm = pass.getText().toString();
zhuCeperester.getCart("http://120.27.23.105/user/reg",sjh,mm);
}

@Override
public void cheng(final ZhuceBean zhuceBean) {

runOnUiThread(new Runnable() {
@Override
public void run() {
if (zhuceBean.getCode().equals("0")) {
Toast.makeText(Main2Activity.this,zhuceBean.getMsg(),Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(Main2Activity.this,zhuceBean.getMsg(),Toast.LENGTH_SHORT).show();
finish();
}
}
});

}
}


注册的接口

public interface ZhuceMterface {
void cheng(ZhuceBean zhuceBean);
}


> ShopActivity 搜索切换视图的V层

public class ShopActivity extends AppCompatActivity implements ShopSterface{

private XRecyclerView xrecy;
private ShopPersenter shopPersenter;
private EditText et_sousuo;
private int page = 1;

private ShopAdapter shopAdapter;
private List<ShopBean.DataBean> data;
private List<ShopBean.DataBean> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
xrecy = (XRecyclerView) findViewById(R.id.xrecy);
et_sousuo = (EditText) findViewById(R.id.et_sousuo);

shopPersenter = new ShopPersenter(this);
xrecy.setLoadingListener(new XRecyclerView.LoadingListener() {
/**
* 刷新
*/
@Override
public void onRefresh() {
shopAdapter.notifyDataSetChanged();
xrecy.refreshComplete();
}
/**
* 加载
*/
@Override
public void onLoadMore() {

shopPersenter.getSou(et_sousuo.getText().toString(), page++);
shopAdapter.notifyDataSetChanged();
xrecy.refreshComplete();
}
});

}
boolean flag = true;
public void huan(View view){
if (flag){
flag = false;
xrecy.setLayoutManager(new LinearLayoutManager(ShopActivity.this,LinearLayoutManager.VERTICAL,false ));
//刷新
shopAdapter.notifyDataSetChanged();
}else {
xrecy.setLayoutManager(new GridLayoutManager(ShopActivity.this, 2, LinearLayoutManager.VERTICAL, false));
shopAdapter.notifyDataSetChanged();
flag = true;
}
}
public void sousuo(View view){
shopPersenter.getSou(et_sousuo.getText().toString(),page);
}

@Override
public void onSuccess(final ShopBean shopBean) {
Log.i("TAG", "v层接口数据: "+shopBean.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
//获得的网络数据的集合
list = new ArrayList<ShopBean.DataBean>();
data = shopBean.getData();

list.addAll(data);
//自己作业做一个集合来包裹网络请求的集合
shopAdapter = new ShopAdapter(ShopActivity.this, list);

//页面管理者 用来控制recycleview怎么来展示
xrecy.setLayoutManager(new GridLayoutManager(ShopActivity.this, 2, LinearLayoutManager.VERTICAL, false));
//分割线
xrecy.setAdapter(shopAdapter);

}
});
}
}


ShopSterface 搜索商品的接口

public interface ShopSterface {
void onSuccess(ShopBean shopBean);
}


> SousuoActivity 这个是流式布局的V层

public class SousuoActivity extends AppCompatActivity {

private EditText et;
private ListView lv;
private List<String> list;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sousuo);
et = (EditText) findViewById(R.id.et);
lv = (ListView) findViewById(R.id.lv);
list = new ArrayList<>();

}
public void sousuo(View view){
String string = et.getText().toString();
adapter = new Adapter(list, this);
list.add(string);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(SousuoActivity.this);
builder.setMessage("确定删除这条记录吗");
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int a) {
list.remove(list.get(i));
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
builder.show();
return true;
}
});
adapter.notifyDataSetChanged();
Intent intent = new Intent(SousuoActivity.this,ShopActivity.class);
startActivity(intent);
}
public void dele(View view){
list.clear();
adapter.notifyDataSetChanged();
}
}


> 自定义 ZDY

public class ZDY extends ViewGroup {
public ZDY(Context context) {
super(context,null);
}

public ZDY(Context context, AttributeSet attrs) {
super(context, attrs,0);
}

public ZDY(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int wid = 0;
int hei = 0;
int childCount = getChildCount();
for (int f = 0;f<childCount;f++){
View childAt = getChildAt(f);

int measuredHeight = childAt.getMeasuredHeight();
int measuredWidth = childAt.getMeasuredWidth();

childAt.layout(wid,hei,wid+measuredWidth,hei+measuredHeight);

wid=wid+measuredWidth+20;
hei=hei+measuredHeight-measuredHeight;
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: