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 {
    protected List mTagDataList;
    private OnDataChangedListener mOnDataChangedListener;

    public void setTagDataList(List 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 {

    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);
});

你可能感兴趣的:(android)