-这是自定义View
public class MyView extends ViewGroup {
int zuo=20;
int shang=20;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int zuoyou=zuo;
int shangxia=shang;
for (int i = 0; i < getChildCount(); i++) {
int measuredWidth = getChildAt(i).getMeasuredWidth();
int measuredHeight = getChildAt(i).getMeasuredHeight();
if (measuredWidth+zuo+zuoyou>getWidth()){
zuoyou=20;
shangxia+=measuredHeight+shang;
getChildAt(i).layout(zuoyou,shangxia,measuredWidth+zuoyou,measuredHeight+shangxia);
}else {
getChildAt(i).layout(zuoyou,shangxia,zuoyou+measuredWidth,shangxia+measuredHeight);
}
zuoyou+=measuredWidth+zuo;
}
}
}
- 主页面代码
public class Main2Activity extends AppCompatActivity implements View.OnClickListener, IContract.IView {
private EditText et;
private Button bnt_sousuo;
private TextView tv_delete;
private MyView my_view;
private RecyclerView my_RecyA;
private PresenterImpl presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// ButterKnife.bind(this);
initView();
presenter = new PresenterImpl();
presenter.onAttch(this);
tv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
my_view.removeAllViews();
}
});
}
public void initView() {
et = (EditText) findViewById(R.id.et);
et.setOnClickListener(this);
bnt_sousuo = (Button) findViewById(R.id.bnt_sousuo);
bnt_sousuo.setOnClickListener(this);
tv_delete = (TextView) findViewById(R.id.tv_delete);
tv_delete.setOnClickListener(this);
my_view = (MyView) findViewById(R.id.my_view);
my_view.setOnClickListener(this);
my_RecyA = (RecyclerView) findViewById(R.id.my_RecyA);
my_RecyA.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bnt_sousuo:
final String s = et.getText().toString();
if (TextUtils.isEmpty(s)) {
Toast.makeText(this, "请输入搜索为空", Toast.LENGTH_SHORT).show();
} else {
//1.设置textView文本
TextView textView = new TextView(this);
//2.把et输入数值添加到textView
textView.setText(s);
//3.添加它的类型
textView.setBackgroundResource(R.drawable.my_liusi);
//4.把自定义流失布局添加到textview
my_view.addView(textView);
//5.搜索框为空
et.setText(null);
//6.请求网络
HashMap<String, Object> map = new HashMap<>();
map.put("page", "1");
map.put("count", "10");
map.put("keyword", s);
presenter.getMethodB(MyUrl.SHOW_SEEK,map,ShowSeekBean.class);
//7.点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main2Activity.this, s, Toast.LENGTH_SHORT).show();
HashMap<String, Object> map = new HashMap<>();
map.put("page", "1");
map.put("count", "10");
map.put("keyword", s);
presenter.getMethodB(MyUrl.SHOW_SEEK,map,ShowSeekBean.class);
}
});
//.startGetInfo(MyUrl.SHOW_URL, map, ShowBean.class);
}
break;
}
}
private void submit() {
// validate
String etString = et.getText().toString().trim();
if (TextUtils.isEmpty(etString)) {
Toast.makeText(this, "etString不能为空", Toast.LENGTH_SHORT).show();
return;
}
// TODO validate success, do something
}
@Override
public void setData(Object data) {
if (data instanceof ShowSeekBean) {
List<ShowSeekBean.ResultBean> result = ((ShowSeekBean) data).getResult();
List<ShowSeekBean.ResultBean> list = new ArrayList<>();
list.addAll(result);
//添加数据库
//dao.insert(result);
//ShowAdapter showAdapter = new ShowAdapter(list, this);
//my_recyA.setAdapter(showAdapter);
my_RecyA.setLayoutManager(new GridLayoutManager(this, 2));
ShowAdapter showAdapter = new ShowAdapter(list, this);
my_RecyA.setAdapter(showAdapter);
}
}
}
- 布局页面
<?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=".ui.activity.Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_marginTop="5dp"
android:background="@drawable/common_btn_menu"
/>
<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:ems="13"
android:hint="请输入内容"
android:background="@drawable/my_shape1"
/>
<Button
android:id="@+id/bnt_sousuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_delete"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="right"
android:text="清空历史记录"
/>
<com.bawei.moth.di.view.MyView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="50dp"></com.bawei.moth.di.view.MyView>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_RecyA"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>