android TagFlowLayout 标签流式布局

复制代码
package com..easytrans.orallearning.lb.sentence.view.flowlayout;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.design.internal.FlowLayout;
import android.util.AttributeSet;
import android.view.View;

import com.iflytek.easytrans.orallearning.lb.R;

@SuppressLint("RestrictedApi")
public class TagFlowLayout extends FlowLayout implements BaseTagAdapter.OnDataChangedListener {

    private BaseTagAdapter mTagAdapter;
    private static final String TAG = "TagFlowLayout";

    private OnTagClickListener mOnTagClickListener;

    public interface OnTagClickListener {
        void onTagClick(View view, int position, FlowLayout parent, boolean isPerform);
    }

    public TagFlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout);
        ta.recycle();
    }

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

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

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

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        mOnTagClickListener = onTagClickListener;
    }

    public void setAdapter(BaseTagAdapter adapter) {
        mTagAdapter = adapter;
        mTagAdapter.setOnDataChangedListener(this);
        changeAdapter();
    }

    private void changeAdapter() {
        removeAllViews();
        BaseTagAdapter adapter = mTagAdapter;
        for (int i = 0; i < adapter.getCount(); i++) {
            View tagView = adapter.getView(this, i, adapter.getItem(i));
            addView(tagView);
            final int position = i;
            tagView.setOnClickListener(v -> {
                if (mOnTagClickListener != null) {
                    mOnTagClickListener.onTagClick(tagView, position, TagFlowLayout.this, false);
                }
            });
        }
    }

    public BaseTagAdapter getAdapter() {
        return mTagAdapter;
    }

    public void performItemClick(int position) {
        click(position);
    }

    private void click(int position) {
        View childView = getChildAt(position);
        if (childView != null) {
            if (mOnTagClickListener != null) {
                mOnTagClickListener.onTagClick(childView, position, TagFlowLayout.this, true);
            }
        }
    }

    @Override
    public void onChanged() {
        changeAdapter();
    }

}

package com..easytrans.orallearning.lb.sentence.view.flowlayout;

import android.support.design.internal.FlowLayout;
import android.view.View;

import java.util.List;

public abstract class BaseTagAdapter<T> {
    protected List<T> mTagDataList;
    private OnDataChangedListener mOnDataChangedListener;

    public void setTagDataList(List<T> dataList) {
        this.mTagDataList = dataList;
    }

    interface OnDataChangedListener {
        void onChanged();
    }

    void setOnDataChangedListener(OnDataChangedListener listener) {
        mOnDataChangedListener = listener;
    }

    public int getCount() {
        return mTagDataList == null ? 0 : mTagDataList.size();
    }

    public void notifyDataChanged() {
        if (mOnDataChangedListener != null) {
            mOnDataChangedListener.onChanged();
        }
    }

    public T getItem(int position) {
        return mTagDataList.get(position);
    }

    public abstract View getView(FlowLayout parent, int position, T t);


}

package com..easytrans.orallearning.lb.sentence;

import android.content.Context;
import android.support.design.internal.FlowLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.easytrans.orallearning.lb.R;
import com.iflytek.easytrans.orallearning.entity.TagInfo;
import com.iflytek.easytrans.orallearning.lb.sentence.view.flowlayout.BaseTagAdapter;


public class SentenceTagAdapter extends BaseTagAdapter<TagInfo> {

    private Context mContext;

    public SentenceTagAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public View getView(FlowLayout parent, int position, TagInfo tagInfo) {
        LinearLayout llTag = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_sentence_tag, parent, false);
        TextView tvTag = llTag.findViewById(R.id.tv_sentence_tag);
        TextView tvTagSelect = llTag.findViewById(R.id.tv_sentence_tag_select);
        tvTag.setText(tagInfo.getTagTitle());
        tvTagSelect.setText(tagInfo.getTagTitle());
        if (mTagDataList.get(position).isSelected()) {
            tvTagSelect.setVisibility(View.VISIBLE);
        } else {
            tvTag.setVisibility(View.VISIBLE);
        }
        return llTag;
    }

}

使用方法

复制代码
mTagInfoList = result;
mTagAdapter = new SentenceTagAdapter(SentenceActivity.this);
mTagAdapter.setTagDataList(mTagInfoList);
runOnUiThread(() -> {
    mTagLayout.setAdapter(mTagAdapter);
    mTagLayout.performItemClick(0);
});
相关推荐
恋猫de小郭1 小时前
2026,Android Compose 终于支持 Hot Reload 了,但是收费
android·前端·flutter
mygljx12 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
xinhuanjieyi13 小时前
ruoyimate导入sql\antflow\bpm_init_db.sql报错
android·数据库·sql
闲猫14 小时前
基于RABC的权限控制设计
android
星霜笔记17 小时前
GitMob — 手机端 GitHub 管理工具
android·kotlin·github·android jetpack
LiuYaoheng18 小时前
问题记录:Android Studio Low memory
android·ide·android studio
独隅18 小时前
Python 标准库 (Standard Library) 全面使用指南
android·开发语言·python
always_TT19 小时前
strlen、strcpy、strcat等常用字符串函数
android
qqty121719 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
2401_8955213419 小时前
MySQL中between and的基本用法
android·数据库·mysql