电商_自定义流式布局

布局
layout_h.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="wrap_content"
    android:orientation="horizontal"
  >

</LinearLayout>

layout_text.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_weight="1"
    android:background="@drawable/edit_bg"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_txt"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        />
</RelativeLayout>

layout_v.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/layout_all"
        >
    </LinearLayout>

</RelativeLayout>

activity_liu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".activity.LiuLayoutActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <EditText
            android:id="@+id/ed_text"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/btn_seach"
            android:background="@drawable/edit_bg" />

        <Button
            android:id="@+id/btn_seach"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="搜索" />

    </RelativeLayout>

    <com.example.myapplication.view.LiuLayoutView
        android:id="@+id/layout_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp" />

</RelativeLayout>

Acitivity

package com.example.myapplication.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.myapplication.R;
import com.example.myapplication.view.LiuLayoutView;

import java.util.ArrayList;
import java.util.List;


public class LiuLayoutActivity extends AppCompatActivity {


    private LiuLayoutView mLiuLayoutView;
    private EditText mEdText;

    private List<String> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liu_layout);
        mEdText = findViewById(R.id.ed_text);
        mLiuLayoutView = findViewById(R.id.layout_view);
        findViewById(R.id.btn_seach).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mEdText.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    if (mList.contains(content)) {
                        mList.remove(content);
                    }
                    mList.add(0, content);
                    mLiuLayoutView.setList(mList);
                }
            }
        });


    }
}

自定义View

package com.example.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class LiuLayoutView extends RelativeLayout {
    private LinearLayout mLayoutV;
    private LinearLayout mLayoutH;
    private Context context;

    public LiuLayoutView(Context context) {
        super(context);
        init(context);
    }

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

    //初始化视图
    private void init(Context context) {
        this.context = context;
        //第一步,创建垂直的layout
        View view = View.inflate(context, R.layout.layout_v, null);
        mLayoutV = (LinearLayout) view.findViewById(R.id.layout_all);
        addView(view);
    }

    //传递数据
    private List<String> mListContent=new ArrayList<>();
    public void setList(List<String> mList) {
        mListContent=mList;
        mLayoutV.removeAllViews();
        int len = 0;
        //第二步,创建水平的
        mLayoutH = (LinearLayout) View.inflate(context, R.layout.layout_h, null);
        mLayoutV.addView(mLayoutH);
        for (int a = 0; a < mList.size(); a++) {
            String data = mList.get(a);
            len += data.length();
            if (len > 22) {
                mLayoutH = (LinearLayout) View.inflate(context, R.layout.layout_h, null);
                mLayoutV.addView(mLayoutH);
                len = 0;
            }
            //第三步,创建展示数据的View
            final View mLayoutText = View.inflate(context, R.layout.layout_text, null);
            final TextView textView = (TextView) mLayoutText.findViewById(R.id.tv_txt);
            textView.setText(mList.get(a));
            mLayoutH.addView(mLayoutText);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLayoutText.getLayoutParams();
            params.weight = 1;
            params.topMargin = 10;
            params.rightMargin = 10;
            params.leftMargin = 10;
            mLayoutText.setLayoutParams(params);

            mLayoutText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,textView.getText(),Toast.LENGTH_LONG).show();
                }
            });

            mLayoutText.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    String con=textView.getText().toString();//获取到内容
                    mListContent.remove(con);
                    setList(mListContent);
                    return true;
                }
            });

        }

    }
}

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值