自定义view
package com.example.administrator.yizhoukao; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; /** * Created by Administrator on 2018/4/16 0016. */ public class MyView extends LinearLayout { private EditText et_shop; private int color; public MyView(Context context) { this(context,null); } public MyView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context,attrs); } public String getEt_shop() { return et_shop.getText().toString().trim(); } public void setEt_shop(String et) { et_shop.setText(et); et_shop.setTextColor(color); } //自定义接口 public interface OnMyViewClickListener{ void onClickListener(View v); } private OnMyViewClickListener onMyViewClickListener; public void setOnMyViewClickListener(OnMyViewClickListener onMyViewClickListener){ this.onMyViewClickListener=onMyViewClickListener; } private void initView(Context context,AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); color = a.getColor(R.styleable.MyView_tv_color, Color.BLACK); View view = View.inflate(context, R.layout.myview, this); et_shop = view.findViewById(R.id.et_shop); TextView sou = view.findViewById(R.id.sou); sou.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if(onMyViewClickListener!=null){ onMyViewClickListener.onClickListener(view); } } }); } }
自定义MyViewGroup
package com.example.administrator.yizhoukao; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2018/4/16 0016. */ public class MyViewGroup extends ViewGroup { //存储所有子View private List<List<View>> mAllChildViews = new ArrayList<>(); //每一行的高度 private List<Integer> mLineHeight = new ArrayList<>(); public MyViewGroup(Context context) { this(context, null); // TODO Auto-generated constructor stub } public MyViewGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public MyViewGroup(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub //父控件传进来的宽度和高度以及对应的测量模式 int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); //如果当前ViewGroup的宽高为wrap_content的情况 int width = 0;//自己测量的 宽度 int height = 0;//自己测量的高度 //记录每一行的宽度和高度 int lineWidth = 0; int lineHeight = 0; //获取子view的个数 int childCount = getChildCount(); for(int i = 0;i < childCount; i ++){ View child = getChildAt(i); //测量子View的宽和高 measureChild(child, widthMeasureSpec, heightMeasureSpec); //得到LayoutParams MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams(); //子View占据的宽度 int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; //子View占据的高度 int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; //换行时候 if(lineWidth + childWidth > sizeWidth){ //对比得到最大的宽度 width = Math.max(width, lineWidth); //重置lineWidth lineWidth = childWidth; //记录行高 height += lineHeight; lineHeight = childHeight; }else{//不换行情况 //叠加行宽 lineWidth += childWidth; //得到最大行高 lineHeight = Math.max(lineHeight, childHeight); } //处理最后一个子View的情况 if(i == childCount -1){ width = Math.max(width, lineWidth); height += lineHeight; } } //wrap_content setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width, modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub mAllChildViews.clear(); mLineHeight.clear(); //获取当前ViewGroup的宽度 int width = getWidth(); int lineWidth = 0; int lineHeight = 0; //记录当前行的view List<View> lineViews = new ArrayList<View>(); int childCount = getChildCount(); for(int i = 0;i < childCount; i ++){ View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); //如果需要换行 if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){ //记录LineHeight mLineHeight.add(lineHeight); //记录当前行的Views mAllChildViews.add(lineViews); //重置行的宽高 lineWidth = 0; lineHeight = childHeight + lp.topMargin + lp.bottomMargin; //重置view的集合 lineViews = new ArrayList(); } lineWidth += childWidth + lp.leftMargin + lp.rightMargin; lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin); lineViews.add(child); } //处理最后一行 mLineHeight.add(lineHeight); mAllChildViews.add(lineViews); //设置子View的位置 int left = 0; int top = 0; //获取行数 int lineCount = mAllChildViews.size(); for(int i = 0; i < lineCount; i ++){ //当前行的views和高度 lineViews = mAllChildViews.get(i); lineHeight = mLineHeight.get(i); for(int j = 0; j < lineViews.size(); j ++){ View child = lineViews.get(j); //判断是否显示 if(child.getVisibility() == View.GONE){ continue; } MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int cLeft = left + lp.leftMargin; int cTop = top + lp.topMargin; int cRight = cLeft + child.getMeasuredWidth(); int cBottom = cTop + child.getMeasuredHeight(); //进行子View进行布局 child.layout(cLeft, cTop, cRight, cBottom); left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } left = 0; top += lineHeight; } } /** * 与当前ViewGroup对应的LayoutParams */ @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { // TODO Auto-generated method stub return new MarginLayoutParams(getContext(), attrs); } }
package com.example.administrator.yizhoukao; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity { private MyView mv; private String mNames[] = { "笔记本","空气净化器","安卓手机", "超级大号圆珠笔","空气滤芯","超级大号钢笔", "气垫cc","防晒霜" }; private MyViewGroup mvg; private Dao dao; private ListView lv; private Button del; private TextView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mv = findViewById(R.id.mv); mvg = findViewById(R.id.mvg); lv = findViewById(R.id.lv); del = findViewById(R.id.del); dao = new Dao(MainActivity.this); mv.setOnMyViewClickListener(new MyView.OnMyViewClickListener() { @Override public void onClickListener(View v) { String et_shop = mv.getEt_shop(); Toast.makeText(MainActivity.this,et_shop,Toast.LENGTH_LONG).show(); dao.insert(et_shop); List<String> select = dao.select(); ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,select); lv.setAdapter(arrayAdapter); } }); initChildViews(); del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dao.deleteData(); List<String> select = dao.select(); ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,select); lv.setAdapter(arrayAdapter); } }); } private void initChildViews() { ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.leftMargin = 5; lp.rightMargin = 5; lp.topMargin = 5; lp.bottomMargin = 5; for(int i = 0; i < mNames.length; i ++){ view = new TextView(this); view.setText(mNames[i]); view.setTextSize(15); view.setPadding(15,3,15,4); //view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg)); mvg.addView(view,lp); final int finalI = i; view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mv.setEt_shop(mNames[finalI]); dao.insert(mNames[finalI]); List<String> select = dao.select(); ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,select); lv.setAdapter(arrayAdapter); Toast.makeText(MainActivity.this,mNames[finalI],Toast.LENGTH_LONG).show(); } }); } } }
package com.example.administrator.yizhoukao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2018/4/16 0016. */ public class Dao { private final SQLiteDatabase db; public Dao(Context context) { MyHelper helper = new MyHelper(context); db = helper.getWritableDatabase(); } //插入数据 public void insert(String sname){ ContentValues values = new ContentValues(); values.put("name",sname); db.insert("shops",null,values); } //查询 public List<String> select(){ List<String> list = new ArrayList<>(); Cursor cursor = db.query("shops", null, null, null, null, null, null); while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); list.add(name); } return list; } public void deleteData() { db.delete("shops",null,null); } }
package com.example.administrator.yizhoukao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by Administrator on 2018/4/16 0016. */ public class MyHelper extends SQLiteOpenHelper { public MyHelper(Context context) { super(context, "test.db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table shops(id integer primary key autoincrement,name text)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
activity_main.xml
<?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" tools:context="com.example.administrator.yizhoukao.MainActivity" android:orientation="vertical"> <com.example.administrator.yizhoukao.MyView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mv" app:tv_color="#f00" android:layout_marginBottom="20dp"> </com.example.administrator.yizhoukao.MyView> <com.example.administrator.yizhoukao.MyViewGroup android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/mvg" > </com.example.administrator.yizhoukao.MyViewGroup> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv"></ListView> <Button android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/del" android:text="清空历史记录" android:background="#fff" android:layout_gravity="center_horizontal"/> </LinearLayout>
myview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal"> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:hint="请输入搜索内容" android:id="@+id/et_shop" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sou" android:text="搜索" android:textSize="20sp" /> </LinearLayout>