Android 仿京东搜索页面


//自定义title

package com.bawei.com.zhouliumoni1.Views;



import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;


import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;






import com.bawei.com.zhouliumoni1.R;






/**
 * Created by lenovo on 2017/12/30.
 */


public class Mytitle extends RelativeLayout {


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

}


//流失布局

package com.bawei.com.zhouliumoni1.Views;


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 lenovo on 2018/1/1.
 */


public class XCFlowLayout  extends ViewGroup {


    //存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    //每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();


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


    public XCFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs,0);
    }


    public XCFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @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) {


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

//MainAcitivy主类

package com.bawei.com.zhouliumoni1;




import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.bawei.com.zhouliumoni1.Views.Mytitle;
import com.bawei.com.zhouliumoni1.Views.XCFlowLayout;


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 XCFlowLayout mFlowLayout;
    private ListView lv;
    private Mytitle mtl;
    private Button bt;
    private List<String> list=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFlowLayout = (XCFlowLayout) 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());


            }
        });
        initChildViews();


        jilu();
    }
    public  void initChildViews() {
        // TODO Auto-generated method stub


        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 ++){
            TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.WHITE);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
            mFlowLayout.addView(view,lp);
            final int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


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


                }
            });


        }




    }
    public 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());
            }


        });


        lv.setAdapter(new myBaseAdapter());




    }




    class myBaseAdapter  extends BaseAdapter{


        @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(MainActivity.this,R.layout.layout,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;
    }


}

//布局xml

.MainActivity


<?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.bawei.com.zhouliumoni1.MainActivity">


    <com.bawei.com.zhouliumoni1.Views.Mytitle
        android:id="@+id/mtl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="热搜"
        android:textSize="50dp" />
    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="150dp">


        <com.bawei.com.zhouliumoni1.Views.XCFlowLayout
            android:id="@+id/flowlayout"
            android:layout_width="match_parent"
            android:layout_height="150dp">


        </com.bawei.com.zhouliumoni1.Views.XCFlowLayout>


    </RelativeLayout>


    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="历史记录"
        android:textSize="50dp" />


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


    <Button
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清空历史记录"/>


</LinearLayout>


//流式布局·1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

//title布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">


    <TextView
        android:id="@+id/tv"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:text="<"
        android:textSize="30sp" />




    <RelativeLayout
        android:layout_marginLeft="30dp"
        android:id="@+id/rl"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/tv"
        android:layout_toRightOf="@+id/tv">


        <ImageView
            android:id="@+id/img"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/order_find" />


        <EditText
            android:id="@+id/et"
            android:singleLine="true"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:hint="美图超级品牌日,赢1127元卡"
            android:paddingLeft="50dp"
            android:textSize="20sp" />


        <ImageView
            android:layout_marginTop="5dp"
            android:id="@+id/img1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/root" />
    </RelativeLayout>


    <TextView
        android:id="@+id/tvv"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="搜索"
        android:textSize="25sp" />

</RelativeLayout>


//建一个shape  

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

//在style样式里

<style name="text_flag_01">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_margin">4dp</item>
        <item name="android:background">#0ff</item>
        <item name="android:textColor">#ffffff</item>
    </style>





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值