Android自定义View流式布局搜索历史记录热门搜索

Android自定义View流式布局搜索历史记录热门搜索_第1张图片

自定义View流式布局,创建一个类继承ViewGroup


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/*Time:2019/3/25
 *Author:zhaozhiwei
 *Description:
 */public class flowLabout extends ViewGroup {


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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t2, int r2, int b3) {
        int witth = getWidth();
        int low=0;//自定义常量
        int diswith=38;//自定义间距
        for (int j=0;jwitth){
                low++;
                diswith=18;
            }
            child.layout(diswith,low*height,diswith+width,height*(low+1));
            diswith+=width;
        }

    }
}

主页面代码

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.annotation.ColorInt;
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.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.zhaozhiwei20190325.R;

import java.util.ArrayList;

import static android.graphics.Color.*;

public class Main2Activity extends AppCompatActivity {

    private Button nIamge;
    private EditText editText;

    private ImageView shan;
    private ArrayList list;
    private flowLabout zidingyi1;
    private flowLabout zidingyi2;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        nIamge = findViewById(R.id.nImage);
        editText = findViewById(R.id.nEdtext);
        zidingyi1 = findViewById(R.id.liushi);
        zidingyi2 = findViewById(R.id.liushi1);
        Button bt1 = findViewById(R.id.bt1);
        shan = findViewById(R.id.shan);
        getSetlayout();

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main2Activity.this, "启动", Toast.LENGTH_LONG).show();
                final String s = editText.getText().toString();
                TextView textView = new TextView(Main2Activity.this);
                textView.setText(s);
                textView.setBackgroundResource(R.drawable.shape);
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                        , ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(5,5,5,5);
                zidingyi1.addView(textView,params);
                //点击
                getInintevent(textView);
                ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", 110, 5);
                animator.setDuration(3000);
                animator.start();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
                        intent.putExtra("s",s);
                       // startActivity(intent);

                    }
                },3000);
            }
        });
        shan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zidingyi1.removeAllViews();
            }
        });

    }

    private void getSetlayout() {

        list = new ArrayList<>();
        list.add("衣服  "+"  ");
        list.add("鞋子  "+"  ");
        list.add("化妆品  "+"  ");
        list.add("丫头  "+"  ");
        list.add("小阿辰  "+"  ");
        for (int i = 0; i < list.size(); i++) {
            gettextview(list.get(i));
        }

    }

    private void gettextview(String s) {
        TextView textView = new TextView(Main2Activity.this);
        textView.setText(s);
       // textView.setBackgroundResource(R.drawable.shape);
      //  textView.setBackgroundColor(R.drawable.shape);
        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(115, 10, 5, 15);
        getInintevent(textView);
        zidingyi2.addView(textView,params);

    }

    private void getInintevent(final TextView textView) {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
                intent.putExtra("s",textView.getText().toString());
                startActivity(intent);
            }
        });

    }
}

布局




    

        

你可能感兴趣的:(开发)