伪京东搜索页面

属性

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FluidLayout">
        <attr name="gravity" >
            <flag name="top" value="0x30" />
            <flag name="bottom" value="0x50" />
            <flag name="center" value="0x11" />
        </attr>
    </declare-styleable>

    <declare-styleable name="FluidLayout_Layout">
        <attr name="layout_gravity">
            <flag name="top" value="0x30" />
            <flag name="bottom" value="0x50" />
            <flag name="center" value="0x11" />
        </attr>
    </declare-styleable>
</resources>
布局

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"
    android:orientation="vertical"
    tools:context="com.example.asus.myapplication.MainActivity">
    
            <com.example.asus.myapplication.view.Mytitle
                android:id="@+id/mtl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


            </com.example.asus.myapplication.view.Mytitle>
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="热搜"
                android:textSize="25dp" />
            <RelativeLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="150dp">
                <com.example.asus.myapplication.view.zidingyi
                    android:id="@+id/flowlayout"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"></com.example.asus.myapplication.view.zidingyi>
            </RelativeLayout>
            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="历史记录"
                android:textSize="25dp" />


            <ListView
                android:id="@+id/lv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <Button
                android:id="@+id/bt"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/line2"

                android:text="清空历史记录"/>
       

</LinearLayout>
item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvv1"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
title_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_height="50dp">
    <ImageView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:src="@drawable/leftjiantou" />

    <LinearLayout
        android:layout_marginLeft="20dp"
        android:id="@+id/rl"
        android:gravity="center"
        android:background="@drawable/line"
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_weight="6"
        android:layout_height="40dp">


        <ImageView
            android:id="@+id/img"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:src="@drawable/a_4" />


        <EditText
            android:id="@+id/et"
            android:layout_weight="8"
            android:layout_width="0dp"
            android:singleLine="true"
            android:ellipsize="end"
            android:background="@null"
            android:textCursorDrawable="@null"
            android:layout_height="wrap_content"
            android:hint="美图超级品牌日,赢1127元卡"
            android:textSize="15sp" />


        <ImageView
            android:id="@+id/img1"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:src="@drawable/root" />
    </LinearLayout>


    <TextView
        android:id="@+id/tvv"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_marginLeft="15dp"
        android:text="搜索"
        android:textSize="20sp" />
</LinearLayout>
shape

line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F0F2F5" />
    <stroke
        android:width="0.01dp"
        android:color="#bfbfbf" />
    <corners
        android:radius="20dp"
        />
</shape>

line2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F0F0F0" />
    <corners android:radius="10dp" />
    <padding
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp"
        />
</shape>

适配器

myBaseAdapter
package com.example.asus.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.asus.myapplication.R;

import java.util.List;

/**
 * Created by asus on 2018/4/14.
 */

public class myBaseAdapter extends BaseAdapter {
    private Context context;
    private List<String> list;

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

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold hold=null;
        if(convertView==null){
            if(hold==null){
                hold=new ViewHold();
                convertView=View.inflate(context, R.layout.item,null);
                hold.tv = convertView.findViewById(R.id.tvv1);
            }
            convertView.setTag(hold);
        }else{
            hold = (ViewHold) convertView.getTag();
        }
        hold.tv.setText(list.get(position));
        return convertView;
    }
}
class ViewHold{
    TextView tv;
}
标题

Mytitle
package com.example.asus.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.asus.myapplication.R;

/**
 * Created by asus on 2018/4/14.
 */

public class Mytitle extends LinearLayout {
    private onsetHuida onsethuida;
    private EditText et;
    private ImageView img;
    private TextView tvv;

    public Mytitle(Context context) {
        this(context, null);
    }

    public Mytitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Mytitle( Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater from = LayoutInflater.from(context);
        View inflate = from.inflate(R.layout.title_main, this, true);
        img = (ImageView)inflate.findViewById(R.id.img);
        tvv = inflate.findViewById(R.id.tvv);
        et = (EditText)inflate.findViewById(R.id.et);
        tvv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onsethuida.huida(et.getText().toString());
            }
        });
    }
    public interface onsetHuida{
        void huida(String aa);
    }
    public void setJiekou(onsetHuida onsethuida){
        this.onsethuida=onsethuida;
    }
}

zidingyi
package com.example.asus.myapplication.view;

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 asus on 2018/4/14.
 */

public class zidingyi extends ViewGroup {
    //存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    //每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();
    public zidingyi(Context context) {
        this(context,null);
    }

    public zidingyi(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {


        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;
        }
    }

    @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);
    }
    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub
        return new MarginLayoutParams(getContext(), attrs);
    }
}
MainActivity
package com.example.asus.myapplication;

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.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.myapplication.adapter.myBaseAdapter;
import com.example.asus.myapplication.view.Mytitle;
import com.example.asus.myapplication.view.zidingyi;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String mNames[] = {
            "welcome","android","TextView",
            "apple","jamy","kobe bryant",
            "jordan","layout","viewgroup",
            "margin","padding","text",
            "name","type","search","logcat"
    };



    private ListView lv;
    private Mytitle mtl;
    private Button bt;
    private List<String> list=new ArrayList<String>();
    private zidingyi mFlowLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFlowLayout = findViewById(R.id.flowlayout);
        lv = (ListView) findViewById(R.id.lv);
        mtl = (Mytitle) findViewById(R.id.mtl);
        bt = (Button) findViewById(R.id.bt);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.clear();
                lv.setAdapter(new myBaseAdapter(MainActivity.this,list));
            }
        });
        initChildViews();
        jilu();
    }



    private void initChildViews() {
        ViewGroup.MarginLayoutParams ip = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ip.leftMargin=5;
        ip.rightMargin = 5;
        ip.topMargin = 5;
        ip.bottomMargin = 5;
        for( int i = 0; i < mNames.length; i ++){
            TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.BLACK);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.line2));
            mFlowLayout.addView(view,ip);
            final int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    list.add(mNames[finalI]);
                    lv.setAdapter(new myBaseAdapter(MainActivity.this,list));


                }
            });


        }
    }
    private void jilu() {
        mtl.setJiekou(new Mytitle.onsetHuida() {
            @Override
            public void huida(String aa) {
                Toast.makeText(MainActivity.this, aa+"", Toast.LENGTH_SHORT).show();
                list.add(aa);
                lv.setAdapter(new myBaseAdapter(MainActivity.this,list));
            }


        });

        lv.setAdapter(new myBaseAdapter(MainActivity.this,list));
    }
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页