flowlayout.zip

  • 大地里的土豆
    了解作者
  • Java
    开发工具
  • 5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-02-23 16:11
    上传日期
流失布局,可以根据字体长度自动换行,使用方便简单
flowlayout.zip
  • flowlayout
  • FlowLayout.java
    7.5KB
  • TagAdapter.java
    2KB
  • TagFlowLayout.java
    8.3KB
  • TagView.java
    1.4KB
内容介绍
package com.ykt.common.widgets.flowlayout; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Parcelable; import android.text.TextUtils; import android.util.AttributeSet; import android.util.Log; import android.view.View; import com.ykt.common.R; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * Created by zhy on 15/9/10. */ public class TagFlowLayout extends FlowLayout implements TagAdapter.OnDataChangedListener { private TagAdapter mTagAdapter; private int mSelectedMax = -1;//-1为不限制数量 private static final String TAG = "TagFlowLayout"; private Set<Integer> mSelectedView = new HashSet<Integer>(); private OnSelectListener mOnSelectListener; private OnTagClickListener mOnTagClickListener; public interface OnSelectListener { void onSelected(Set<Integer> selectPosSet); } public interface OnTagClickListener { boolean onTagClick(View view, int position, FlowLayout parent); } public TagFlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout); mSelectedMax = ta.getInt(R.styleable.TagFlowLayout_max_select, -1); 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) { int cCount = getChildCount(); for (int i = 0; i < cCount; i++) { TagView tagView = (TagView) getChildAt(i); if (tagView.getVisibility() == View.GONE) { continue; } if (tagView.getTagView().getVisibility() == View.GONE) { tagView.setVisibility(View.GONE); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void setOnSelectListener(OnSelectListener onSelectListener) { mOnSelectListener = onSelectListener; } public void setOnTagClickListener(OnTagClickListener onTagClickListener) { mOnTagClickListener = onTagClickListener; } public void setAdapter(TagAdapter adapter) { mTagAdapter = adapter; mTagAdapter.setOnDataChangedListener(this); mSelectedView.clear(); changeAdapter(); } @SuppressWarnings("ResourceType") private void changeAdapter() { removeAllViews(); TagAdapter adapter = mTagAdapter; TagView tagViewContainer = null; HashSet preCheckedList = mTagAdapter.getPreCheckedList(); for (int i = 0; i < adapter.getCount(); i++) { View tagView = adapter.getView(this, i, adapter.getItem(i)); tagViewContainer = new TagView(getContext()); tagView.setDuplicateParentStateEnabled(true); if (tagView.getLayoutParams() != null) { tagViewContainer.setLayoutParams(tagView.getLayoutParams()); } else { MarginLayoutParams lp = new MarginLayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp.setMargins(dip2px(getContext(), 5), dip2px(getContext(), 5), dip2px(getContext(), 5), dip2px(getContext(), 5)); tagViewContainer.setLayoutParams(lp); } LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); tagView.setLayoutParams(lp); tagViewContainer.addView(tagView); addView(tagViewContainer); if (preCheckedList.contains(i)) { setChildChecked(i, tagViewContainer); } if (mTagAdapter.setSelected(i, adapter.getItem(i))) { setChildChecked(i, tagViewContainer); } tagView.setClickable(false); final TagView finalTagViewContainer = tagViewContainer; final int position = i; tagViewContainer.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { doSelect(finalTagViewContainer, position); if (mOnTagClickListener != null) { mOnTagClickListener.onTagClick(finalTagViewContainer, position, TagFlowLayout.this); } } }); } mSelectedView.addAll(preCheckedList); } public void setMaxSelectCount(int count) { if (mSelectedView.size() > count) { Log.w(TAG, "you has already select more than " + count + " views , so it will be clear ."); mSelectedView.clear(); } mSelectedMax = count; } public Set<Integer> getSelectedList() { return new HashSet<Integer>(mSelectedView); } private void setChildChecked(int position, TagView view) { view.setChecked(true); mTagAdapter.onSelected(position, view.getTagView()); } private void setChildUnChecked(int position, TagView view) { view.setChecked(false); mTagAdapter.unSelected(position, view.getTagView()); } private void doSelect(TagView child, int position) { if (!child.isChecked()) { //处理max_select=1的情况 if (mSelectedMax == 1 && mSelectedView.size() == 1) { Iterator<Integer> iterator = mSelectedView.iterator(); Integer preIndex = iterator.next(); TagView pre = (TagView) getChildAt(preIndex); setChildUnChecked(preIndex, pre); setChildChecked(position, child); mSelectedView.remove(preIndex); mSelectedView.add(position); } else { if (mSelectedMax > 0 && mSelectedView.size() >= mSelectedMax) { return; } setChildChecked(position, child); mSelectedView.add(position); } } else { setChildUnChecked(position, child); mSelectedView.remove(position); } if (mOnSelectListener != null) { mOnSelectListener.onSelected(new HashSet<Integer>(mSelectedView)); } } public TagAdapter getAdapter() { return mTagAdapter; } private static final String KEY_CHOOSE_POS = "key_choose_pos"; private static final String KEY_DEFAULT = "key_default"; @Override protected Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(KEY_DEFAULT, super.onSaveInstanceState()); String selectPos = ""; if (mSelectedView.size() > 0) { for (int key : mSelectedView) { selectPos += key + "|"; } selectPos = selectPos.substring(0, selectPos.length() - 1); } bundle.putString(KEY_CHOOSE_POS, selectPos); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; String mSelectPos = bundle.getString(KEY_CHOOSE_POS); if (!TextUtils.isEmpty(mSelectPos)) { String[] split = mSelectPos.split("\\|"); for (String pos : split) { int index = Integer.parseInt(pos); mSelectedView.add(index); TagView tagView = (TagView) getChildAt(index); if (tagView != null) { setChildChecked(index, tagView); } } }
评论
    相关推荐
    • java辅导
      Java 程序编码规范.doc Java的文件与磁盘操作.doc Java多线程编程详解.doc Java远程方法调用.doc Java中的时间操作.doc 深入理解abstract class和interface.doc ……
    • javabank
      Java银行 Java com的模拟操作将在bancárias上发布! 要求:Java 8。
    • java 李兴华
      java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java 李兴华 java ...
    • Java204
      支持中文,支持动态注释的超强Java语言反编译器。 注意:Java204的工作目录中不能包含中文字符!! 支持现有所有Java环境产生的Class代码,而最重要的是它生成的源代码可读性极好,又充分考虑了国人的Java编码习惯,...
    • java swing
      精通java swing 是一部学习java swing的好书,讲的很深,但只要有点java基础的人应该看的懂,适合java初学者.
    • JAVA教程
      详细介绍了JAVA语言程序设计,对JAVA的基础知识运用实例的方式来讲解,使初学者能够非常轻松的掌握。
    • java程序
      java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点java程序-吃点点
    • Java
      Java 2021/02/19 .java Main.java 学生.java Bmi.java 。班级 Bmi类 主类 MyBmi.class 学生班 学生样本类 2021/02/20 .java DeleteFile.java WriteFile.java MyDataPrint.java 。班级 DeleteFile.class ...
    • Java
      Java是一种面向对象的编程语言,可用于Internet上的分布式环境。 Java是易于阅读和理解的高级语言。 Java通常用于控制台,GUI,基于Web的应用程序和移动应用程序,游戏开发中,并用于制作嵌入式系统。 除此以外,...
    • java java java
      javajavajavajavjjavaavjavajavaaa