[AS3.0.1]简单的标签流式布局

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/g777520/article/details/80510361

一个简单的标签流式布局

效果如下图
img

使用代码如下
首先是依赖

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}


dependencies {
    implementation 'com.github.Gaojianan2016:FlowLayout:1.0.1'
}

几个简单的操作

package com.gjn.flowlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gjn.library.TagAdapter;
import com.gjn.library.TagLayout;

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

public class MainActivity extends AppCompatActivity {

    private TagAdapter<String> adapter;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TagLayout tl = findViewById(R.id.tl);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.add("add " + adapter.getCount());
            }
        });

        adapter = new TagAdapter<String>(this, R.layout.tv_str, null) {
            @Override
            public TextView onBindTextView(View view, int i, String s) {
                TextView textView = (TextView) view;
                textView.setText(s);
                return textView;
            }
        };

        tl.setAdapter(adapter);

        list.add("Welcome to flowlayout");
        list.add("Hello Android");
        list.add("btn one");
        list.add("test all");

        adapter.add(list);

        tl.setOnItemListener(new TagLayout.onItemListener() {
            @Override
            public void onClick(int i, View view, Object o) {
                Log.e("TAG", "点击监听 " + i);
                Log.e("TAG", "==================2");
            }
        });
        //设置最大可选  超过数量范围将设置为-1 即可以无限选择
        tl.setSelectMax(2);

        tl.setOnSelectListener(new TagLayout.onSelectListener() {
            @Override
            public void onSelect(List<View> list, List<Integer> list1, List<Object> list2) {
                Log.e("TAG", "选择监听");
                Log.e("TAG", "当前选中项 ");
                for (Integer integer : list1) {
                    Log.e("TAG", "=>"+integer);
                }
                Log.e("TAG", "==================1");
            }
        });
    }
}

xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.gjn.flowlayout.MainActivity">

    <com.gjn.library.TagLayout
        android:id="@+id/tl"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

    </com.gjn.library.TagLayout>


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:singleLine="true"
    android:background="@drawable/select_str"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="">

</TextView>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <solid android:color="@android:color/holo_blue_bright" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <solid android:color="@android:color/darker_gray" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/str_a" />
    <item android:drawable="@drawable/str_b" />
</selector>

项目git地址 FlowLayout


虽然这篇文章写得很简单,但是我走了一遍,从创建项目到上传到GitHub上,然后使用JitPack生成依赖一个整合的流程。算是一个比较大的记录了!

猜你喜欢

转载自blog.csdn.net/g777520/article/details/80510361