自定義View 流式佈局(歷史搜索,熱門搜索)

xml佈局android

<?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"
tools:context=".activity.SearchActivity"
android:orientation="vertical"
>
<bawei.com.week2.customview.HeadView
android:id="@+id/head_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></bawei.com.week2.customview.HeadView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<bawei.com.week2.customview.TitleView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
app:textColor="@color/colorBlack"
android:text="搜索歷史"
android:textSize="25sp"
/>
<ImageView
android:id="@+id/image_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/del"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
/>
</RelativeLayout>
<bawei.com.week2.customview.CustonFlowView
android:id="@+id/flow_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
></bawei.com.week2.customview.CustonFlowView>
<bawei.com.week2.customview.TitleView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
app:textColor="@color/colorRed"
android:text="熱門搜索"
android:textSize="25sp"
/>
<bawei.com.week2.customview.CustonFlowView
android:id="@+id/hot_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
></bawei.com.week2.customview.CustonFlowView>數據庫


</LinearLayout>

HeadView的佈局(headitem)canvas

<?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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageview"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/search"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
/>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:ems="20"
android:hint="請輸入想要輸入的內容"
/>app

</LinearLayout>dom

HeadView代碼ide

public class HeadView extends LinearLayout {
private Context mContext;
private ImageView imageView;
private EditText editText;佈局

public HeadView(Context context) {
super(context);
mContext=context;
initView();
}ui

public HeadView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext=context;
initView();
}this

public void initView(){
//添加布局
View view=View.inflate(mContext,R.layout.headitem,null);
//獲取資源ID
imageView = view.findViewById(R.id.imageview);
editText = view.findViewById(R.id.edit_text);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//6.在將要判斷的地方,判斷是否爲空
if(mCallBack!=null){
//7.執行回調方法,傳入參數
mCallBack.getData(editText.getText().toString());
}
}
});
addView(view);
}
//3.定義成員變量
CallBack mCallBack;
//4.傳入,而且給成員變量賦值
//5.在想要的地方進行回調
public void setOnCallBack(CallBack callBack){
mCallBack=callBack;
}
//1.定義接口
public interface CallBack{
//2.定義方法,傳入參數
void getData(String edit);
}
}.net

自定義屬性在values下建一個attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TitleView">
<attr name="textColor" format="color"></attr>
</declare-styleable>
</resources>
TitleView代碼(自定義屬性)

@SuppressLint("AppCompatCustomView")
public class TitleView extends TextView {

public TitleView(Context context) {
super(context);
}

public TitleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
int color = typedArray.getColor(R.styleable.TitleView_textColor, Color.BLACK);
setTextColor(color);
//屬性回收
typedArray.recycle();

}
}
CustonFlowView中的代碼

public class CustonFlowView extends LinearLayout {
private int mChildHeight;//最高的孩子
private int mLeft=20;//左邊距
private int mTop=20; //上邊距
public CustonFlowView(Context context) {
super(context);
}

public CustonFlowView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//定義父窗口的寬高
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//測量孩子的大小
measureChildren(widthMeasureSpec,heightMeasureSpec);
//找到最高的孩子
findMaxHeight();
//初始化
int left=0,top=0;

int childCount = getChildCount();
for (int i=0;i<childCount;i++){
View view = getChildAt(i);
if(left!=0){
if((left+view.getMeasuredWidth())>widthSize){
top+=mChildHeight+mTop;
left=0;
}
}
left+=view.getMeasuredWidth()+mLeft;
}
setMeasuredDimension(widthSize,(top+mChildHeight)>heightSize?heightSize:top+mChildHeight);


}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findMaxHeight();
//初始化
int left=0,top=0;

int childCount = getChildCount();
for (int i=0;i<childCount;i++){
View view = getChildAt(i);
if(left!=0){
if((left+view.getMeasuredWidth())>getWidth()){
top+=mChildHeight+mTop;
left=0;
}
}
view.layout(left,top,view.getMeasuredWidth()+left,top+mChildHeight);
left+=view.getMeasuredWidth()+mLeft;
}

}

private void findMaxHeight() {
mChildHeight=0;
int childCount = getChildCount();
for(int i=0;i<childCount;i++){
View view = getChildAt(i);
if(view.getMeasuredHeight()>mChildHeight){
mChildHeight=view.getMeasuredHeight();
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}

數據庫
SqlieHelper

public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(@Nullable Context context) {
super(context, "Search.db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table search(id integer primary key autoincrement," +
"name text," +
"mTag text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}
數據庫中查詢用到的bean類

public class NameBean {
public String name;
public String mTag;

public NameBean(String name, String mTag) {
this.name = name;
this.mTag = mTag;
}

public String getName() {
return name;
}

public String getmTag() {
return mTag;
}
}

Dao層

public class Dao {
private SqliteHelper helper;
private SQLiteDatabase database;
public Dao(Context context){
helper=new SqliteHelper(context);
database=helper.getReadableDatabase();
}
//添加
public void add(String name,String mTag){
ContentValues values=new ContentValues();
values.put("name",name);
values.put("mTag",mTag);
database.insert("search",null,values);
}
//查詢
public List<NameBean> select(){
List<NameBean> list=new ArrayList<>();
Cursor query = database.query("search", null, null, null, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String mTag = query.getString(query.getColumnIndex("mTag"));
NameBean bean=new NameBean(name,mTag);
list.add(bean);

}
return list;
}
//刪除
public void delAll(){
database.delete("search",null,null);
}
public void del(String tj){
database.delete("search","mTag=?",new String[]{tj});
}

}

Activity中的代碼

public class SearchActivity extends AppCompatActivity {

private HeadView headView;
private CustonFlowView custonFlowView;
private CustonFlowView hot_view;
private String str[]=new String[]{"辣條","薯片","火腿腸","酸辣粉","米線","奶茶嫁給粉","麻辣燙","黃燜雞","大蝦","酸菜魚"};
private Dao dao;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
initView(savedInstanceState);
initData();
}

private void initData() {
dao=new Dao(this);
//查詢
final List<NameBean> list = dao.select();
for(int i=0;i<list.size();i++){
TextView textView=new TextView(SearchActivity.this);
textView.setText(list.get(i).getName());
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
textView.setPadding(10,10,10,10);
textView.setBackgroundResource(R.drawable.shape_text);
custonFlowView.addView(textView);
//刪除
final int index=i;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(list.get(index).getmTag());
custonFlowView.removeView(v);
}
});
}

headView.setOnCallBack(new HeadView.CallBack() {
@Override
public void getData(String edit) {
String uuid = UUID.randomUUID().toString();

final TextView textView=new TextView(SearchActivity.this);
textView.setTag(uuid);
textView.setText(edit);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
textView.setPadding(10,10,10,10);
textView.setBackgroundResource(R.drawable.shape_text);
dao.add(edit,uuid);
custonFlowView.addView(textView);
//刪除
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(v.getTag().toString());
custonFlowView.removeView(v);
}
});


}
});
//所有刪除
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.delAll();
custonFlowView.removeAllViews();
}
});

//熱門搜索www.yaoqikj.com
for (int i=0;i<str.length;i++){
TextView textView=new TextView(SearchActivity.this);
textView.setText(str[i]);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
textView.setPadding(10,10,10,10);
textView.setBackgroundResource(R.drawable.shape_text);
hot_view.addView(textView);
}

}

private void initView(Bundle savedInstanceState) {
headView = findViewById(R.id.head_view);
custonFlowView = findViewById(R.id.flow_view);
hot_view = findViewById(R.id.hot_view);
imageView = findViewById(R.id.image_del);

}
}

shape

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="20dp" ></corners> <stroke android:width="1dp" android:color="@color/colorPrimary" ></stroke>

相關文章
相關標籤/搜索