package zhoukao1.bwie.com.mvpapplication.activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import zhoukao1.bwie.com.mvpapplication.Bean.Lishi;
import zhoukao1.bwie.com.mvpapplication.Bean.ListBean;
import zhoukao1.bwie.com.mvpapplication.GoodView;
import zhoukao1.bwie.com.mvpapplication.R;
import zhoukao1.bwie.com.mvpapplication.mvp.view.AbnerMing;
import zhoukao1.bwie.com.mvpapplication.net.Hepler;
public class GoodsActivityPresenter extends AbnerMing implements View.OnClickListener {
private GoodView mGoodsView;
private GoodView mGoodsViewB;
private EditText mEditText;
@Override
public int getLayoutId() {
return R.layout.goog;
}
@Override
public void initData() {
super.initData();
mGoodsView = (GoodView) get(R.id.goods_view_top);
mGoodsViewB = (GoodView) get(R.id.goods_view_bottom);
mEditText = (EditText) get(R.id.goods_txt);
doHttpHot();
setClick(this, R.id.seach_msg);
}
List<Lishi.DatasBean> list = new ArrayList<>();
private void doHttpHot() {
String url="http://ftp6252741.host709.zhujiwu.me/goods/goods_hot.txt";
new Hepler().get(url).result(new Hepler.HttpListener() {
@Override
public void success(String data) {
Lishi lishi = new Gson().fromJson(data, Lishi.class);
List<Lishi.DatasBean> datas = lishi.getDatas();
mGoodsView.setList(datas);
}
});
}
public Context context;
@Override
public void getContext(Context context) {
this.context = context;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.seach_msg://搜索
String messs = mEditText.getText().toString().trim();
if (TextUtils.isEmpty(messs)) {
Toast.makeText(context, "请输入要搜索的商品", Toast.LENGTH_SHORT).show();
return;
}
Lishi.DatasBean bean = new Lishi.DatasBean();
bean.setName(messs);
list.add(bean);
mGoodsViewB.setList(list);
break;
}
}
}
=============================
package zhoukao1.bwie.com.mvpapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import zhoukao1.bwie.com.mvpapplication.Bean.Lishi;
public class GoodView extends RelativeLayout {
private LinearLayout mLyoutV;
private View view;
public GoodView(Context context) {
super(context);
init(context);
}
public GoodView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GoodView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private Context context;
private void init(Context context) {
this.context = context;
//创建垂直的LinearLayout
view = View.inflate(context, R.layout.good_v, null);
mLyoutV = (LinearLayout) view.findViewById(R.id.lyout_v);
//创建水平的
addView(view);
}
public void setList(List<Lishi.DatasBean> list) {
mLyoutV.removeAllViews();
//创建水平layout
LinearLayout view_h = (LinearLayout) View.inflate(context, R.layout.good_h, null);
mLyoutV.addView(view_h);
int len = 0;
view_h.removeAllViews();
for (int a = 0; a < list.size(); a++) {
String msg = list.get(a).getName();
len += msg.length();
if (len > 22) {
view_h = (LinearLayout) View.inflate(context, R.layout.good_h, null);
mLyoutV.addView(view_h);
len = 0;
}
//创建展示内容的layout
View viewContent = View.inflate(context, R.layout.good_center, null);
TextView textView = (TextView) viewContent.findViewById(R.id.tv_content);
textView.setText(msg);
view_h.addView(viewContent);
//设置parmars
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewContent.getLayoutParams();
params.weight = 1;
params.leftMargin = 5;
params.topMargin = 5;
viewContent.setLayoutParams(params);
}
}
}
=====================布局
<?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:background="#e8e8e8"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="1"
>
<EditText
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.8"
android:layout_margin="5dp"
android:hint="请输入商品名称"
android:background="@drawable/bian"
android:paddingLeft="10dp"
android:gravity="center_vertical"
android:id="@+id/goods_txt"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="搜索"
android:background="@drawable/cen"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center_horizontal"
android:layout_marginRight="5dp"
android:id="@+id/seach_msg"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
android:textColor="#d43c3c"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
/>
<zhoukao1.bwie.com.mvpapplication.GoodView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/goods_view_top"
android:layout_marginTop="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史搜索"
android:textColor="#d43c3c"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
/>
<zhoukao1.bwie.com.mvpapplication.GoodView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/goods_view_bottom"
android:layout_marginTop="8dp"
/>
</LinearLayout>
===========================
<?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="wrap_content"
tools:context=".activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/lyout_v"
/>
</RelativeLayout>
=======================
<?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="wrap_content"
android:orientation="horizontal"
tools:context=".activity.MainActivity">
</LinearLayout>
==========================
<?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="wrap_content"
android:background="@drawable/cen"
android:paddingTop="10dp"
android:paddingBottom="10dp"
tools:context=".activity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>