美文网首页
Android 流式布局FlowLayout

Android 流式布局FlowLayout

作者: 落叶随风花落入泥 | 来源:发表于2019-08-27 16:11 被阅读0次

FlowLayout

现在项目中为了美观,设计师们都比较喜欢流式布局(FlowLayout),秉着不重复造轮子的原则,在网上搜索了一堆,后来还是根据名气选择了鸿阳的😄。
废话不多说,下面是我做的Demo示例图片:

屏幕快照 2019-08-27 下午4.38.12.png

1、使用鸿阳大神的代码首先要引入依赖:

 implementation 'com.hyman:flowlayout-lib:1.1.2'

2、剩下就是在代码中使用啦
1⃣️、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zhy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.activity.FlowLayoutActivity">

<com.zhy.view.flowlayout.TagFlowLayout
    android:id="@+id/tab_flowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    zhy:max_select="-1"></com.zhy.view.flowlayout.TagFlowLayout>

<Button
    android:id="@+id/btn_count"
    android:layout_width="match_parent"
    android:layout_height="50dp" />
</LinearLayout>

2⃣️、项目中使用,写了两个方法:

  /**
 * 设置选择事件
 */
private void setFlowItemListener() {
    //选择监听
    tabFlowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
        @Override
        public void onSelected(Set<Integer> selectPosSet) {

            Toast.makeText(FlowLayoutActivity.this, "chose" + selectPosSet.toString(), Toast.LENGTH_SHORT).show();
        }
    });

    //标签点击监听
    tabFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
        @Override
        public boolean onTagClick(View view, int position, FlowLayout parent) {
            Toast.makeText(FlowLayoutActivity.this, "标签" + tags.get(position), Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

  /**
 * 给流式布局设置值
 */
private void setFlowData() {

    tabFlowlayout.setAdapter(new TagAdapter<String>(tags) {
        @Override
        public View getView(FlowLayout parent, int position, String o) {

            LayoutInflater inflater = LayoutInflater.from(getApplication());
            TextView textView = (TextView) inflater.inflate(R.layout.tv_layout, tabFlowlayout, false);
            textView.setText(o);
            textView.setBackground(getDrawable(R.drawable.textview_nomal_shape));
            return textView;
        }
        //当选中的时候
        @Override
        public void onSelected(int position, View view) {
            super.onSelected(position, view);
            TextView textView = (TextView) view;
            textView.setBackground(getDrawable(R.drawable.textview_shape));
            textView.setTextColor(Color.parseColor("#FFFFFF"));
        }

        //当没选中的时候
        @Override
        public void unSelected(int position, View view) {
            super.unSelected(position, view);
            TextView textView = (TextView) view;
            textView.setBackground(getDrawable(R.drawable.textview_nomal_shape));
            textView.setTextColor(Color.parseColor("#000000"));
        }
    });
}

3、使用的时候我遇到了一个问题,就是当我给TextView设置选择器的时候,发现只有边框颜色变化,背景没有变化。造成这种问题的原因是我Item布局父布局是LinearLayout 里面嵌套一个TextView,改成Item布局直接是TextView这个问题就解决啦,(分析的原因是,这样写的目的是,我们代码设置选中监听的时候,方法中有一个view如果这个view不是TextView我们就不能设置字体颜色啦,这样就局限啦,有可能分析的不对,只是给自己找了个理由而已😂)可以在第二个方法中随便设置背景色啦,而且还能给条目字体设置颜色。

4、有现成的API可以直接获取我们选中条目的索引值,代码如下,查看了数据,数据准确。

List<Integer> list = new ArrayList<>();

@OnClick(R.id.btn_count)
public void onViewClicked() {
    list.clear();

    Set<Integer> selectedList = tabFlowlayout.getSelectedList();
    Iterator<Integer> iterator = selectedList.iterator();
    while (iterator.hasNext()) {
        Integer next = iterator.next();
        list.add(next);
    }
    Toast.makeText(this, "被选中的条目数量是:" + list.size(), Toast.LENGTH_SHORT).show();

}

5、以上就是使用流程,自学完毕。

相关文章

网友评论

      本文标题:Android 流式布局FlowLayout

      本文链接:https://www.haomeiwen.com/subject/zcbeectx.html