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);
});
相关推荐
传奇开心果编程3 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限
alexhilton4 小时前
卡顿定位:从 UI 线程到 RenderThread
android·kotlin·android jetpack
FungLeo5 小时前
FengCMS· PHP+MySQL升级实录 · 后台参数统一治理:id[]数组、id=undefined 跳转与312组回调组合
android·mysql·php·工程实践·fengcms
北斗星_And5 小时前
七年后再看 Android 底层:机制没变,路变短了
android·ai编程
王二蛋与他的张大花6 小时前
反编译后只剩 2 个类:一次自研 DEX 壳加固的完整复盘
android·安全·逆向
StarsGreen11 小时前
Android 12 中的“域“与“权限“介绍
android
恋猫de小郭13 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
FlightYe13 小时前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频
StarsGreen1 天前
Android中的seccomp相关信息介绍
android
阿杰m1 天前
Android源码第二十四章:OTA 与 APEX——重启后换了什么?
android