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);
});
相关推荐
liang_jy15 小时前
Android SparseArray
android·源码
liang_jy16 小时前
Activity 启动流程扩展篇(一)—— startActivityInner 任务决策全解析
android·源码
NPE~17 小时前
[App逆向]脱壳实战
android·教程·逆向·android逆向·逆向分析
木易 士心17 小时前
别再只会用 drawCircle 了!一文搞懂 Android Canvas 底层机制
android
AtOR CUES19 小时前
MySQL——表操作及查询
android·mysql·adb
怣疯knight20 小时前
安卓App无法增加自定义图片作为图标功能
android
jinanwuhuaguo21 小时前
OpenClaw联邦之心——从孤岛记忆到硅基集体潜意识的拓扑学革命(第二十三篇)
android·人工智能·kotlin·拓扑学·openclaw
Gary Studio1 天前
安卓HAL C++基础-命名域
android
诸神黄昏EX1 天前
Android Google XTS
android
eSsO KERF1 天前
MySQL Workbench菜单汉化为中文
android·数据库·mysql