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);
});
相关推荐
传奇开心果编程1 小时前
【Compose Multiplatform 跨端开发学与练】第7课 平台适配与互操作
android·windows·学习·ui·ios·kotlin·composer
mmsx1 小时前
Android 地图数据链路:从 GDAL、KML 到多引擎适配
android·kotlin
2601_968900772 小时前
大模型版本回归评估实战:从能力指标到额度口径的完整链路
android·数据挖掘·回归
ao-weilai4 小时前
MySQL数据库:基本查询
android·数据库·mysql
事圆则缓5 小时前
Kotlin 泛型方差实战:out、in、星投影与类型擦除
android·开发语言·kotlin
传奇开心果编程6 小时前
【Compose Multiplatform 跨端开发学与练】第5课 网络与数据层
android·网络·学习·ui·ios·kotlin·composer
ao-weilai7 小时前
MySQL数据库:内置函数
android·数据库·mysql
SWAGGY..8 小时前
【C++进阶】:(7)红黑树的原理与 C++ 实现:结构设计、插入调整及性质验证
android·java·开发语言·c++·算法
Android打工仔8 小时前
CoroutineScheduler 设计解析(上)—— 为什么 Dispatchers.IO 会创建更多线程?
android·kotlin·源码阅读
martindelophy9 小时前
用自然语言剪视频:我们在 Timeline Studio 中实现了 ChatCut 对话剪辑
android·音视频