Android 流式布局,支持單選,多選

流式布局 FlowLayout

Android流式布局,支持單選、多選等,適合用于產(chǎn)品標(biāo)簽等。
支持?jǐn)?shù)組,集合,集合對(duì)象數(shù)據(jù)(Demo 中以集合對(duì)象示例)。

GitHub 地址:https://github.com/lyyRunning/MyFlowLayout

特色:

  • 以setAdapter形式注入數(shù)據(jù)
  • 直接設(shè)置selector為background即可完成標(biāo)簽選則的切換,類似CheckBox
  • 支持控制選擇的Tag數(shù)量,比如:?jiǎn)芜x、多選
  • 支持setOnTagClickListener,當(dāng)點(diǎn)擊某個(gè)Tag回調(diào)
  • 支持setOnSelectListener,當(dāng)選擇某個(gè)Tag后回調(diào)
  • 支持adapter.notifyDataChanged
  • Activity重建(或者旋轉(zhuǎn))后,選擇的狀態(tài)自動(dòng)保存

效果圖:

image

簡(jiǎn)單用法:

dependencies {
    //FlowLayouty依賴
    compile 'com.hyman:flowlayout-lib:1.1.2'
    compile 'com.zhy:base-adapter:2.0.1'
}

布局聲明:

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

支持屬性:

  • max_select:-1為不限制選擇數(shù)量,>=1的數(shù)字為控制選擇tag的數(shù)量

  • 支持通過(guò)state=checked來(lái)控制選中和取消,也可以自己在Adapter 的onSelected和unSelected中分別處理顯示。

設(shè)置數(shù)據(jù)

idFlowlayout.setAdapter(new TagAdapter<String>(mVals)  {
       @Override
       public View getView(FlowLayout parent, int position, String s)
       {
           TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                   mFlowLayout, false);
           tv.setText(s);
           return tv;
       }
   });

getView中回調(diào),類似ListView等用法。

點(diǎn)擊某個(gè)標(biāo)簽回調(diào)

 /**
    * 點(diǎn)擊某個(gè) Tag 返回
   */
idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                String tag = mVals[position];
                tvText1.setText(tag);
                Toast.makeText(SingleActivity.this, "Clicked====" + tag, Toast.LENGTH_SHORT).show();

                return true;
            }
        });

選擇多個(gè)標(biāo)簽回調(diào)

idFlowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {

                tvText1.setText(String.valueOf(selectPosSet));
                Toast.makeText(MultipleSelectionActivity.this, "selectPosSet" + selectPosSet, Toast.LENGTH_SHORT).show();

            }
        });

預(yù)先設(shè)置Item選中

//預(yù)先設(shè)置選中
mAdapter.setSelectedList(1,3,5,7,8,9);
//獲得所有選中的pos集合
idFlowlayout.getSelectedList();

貼一個(gè)單選完整例子;

SingleActivity的活動(dòng)


public class SingleActivity extends Activity {

    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    @BindView(R.id.tv_text1)
    TextView tvText1;
    List<User> list;
    TagAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_single_choose);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        final LayoutInflater mInflater = LayoutInflater.from(this);
         list = new ArrayList<>();
        list.add(new User("Hello"));
        list.add(new User("Android"));
        list.add(new User("Weclome Hi "));
        list.add(new User("Button"));
        list.add(new User("TextView"));
        list.add(new User("Weclome Hello"));
        list.add(new User("Button Text"));
        list.add(new User("TextView"));
        list.add(new User("utton ImageView"));
        list.add(new User("Weclome"));
        list.add(new User("Hello"));
        list.add(new User("Weclome Hi "));
        list.add(new User("Button"));
        list.add(new User("TextView"));
        list.add(new User("Weclome Hello"));
        list.add(new User("Button Text"));
        list.add(new User("TextView"));
        list.add(new User("utton ImageView"));
        list.add(new User("Weclome"));
        list.add(new User("Hello"));
        list.add(new User("Weclome Hello"));

        /**
         * 點(diǎn)擊某個(gè) Tag 返回
         */
 idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
   public boolean onTagClick(View view, int position, FlowLayout parent) {

                String tag = list.get(position).getName();
                tvText1.setText(tag);
Toast.makeText(SingleActivity.this, "Clicked====" + tag, Toast.LENGTH_SHORT).show();

                return true;
            }
        });

 idFlowlayout.setAdapter(mAdapter = new TagAdapter<User>(list) {
            @Override
            public View getView(FlowLayout parent, int position, User user) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        idFlowlayout, false);
                tv.setText(user.getName());
                return tv;
            }

        });

        //設(shè)置選中的按鈕
        mAdapter.setSelectedList(5);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, SingleActivity.class);
        context.startActivity(intent);
    }
}

布局:

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

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/tv_text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />

    <com.zhy.view.flowlayout.TagFlowLayout
        android:id="@+id/id_flowlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        zhy:max_select="1"
        android:layout_marginTop="10dp"
        >
    </com.zhy.view.flowlayout.TagFlowLayout>
</LinearLayout>

tv.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="5dp"
          android:background="@drawable/tag_bg"
          android:text="Helloworld"
          android:textColor="@drawable/text_color">

</TextView>

User 對(duì)象:

public class User {

    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String name) {
        this.name = name;
    }
}

tag_bg的drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/checked_bg"
        android:state_checked="true"
        >

    </item>
    <item
          android:drawable="@drawable/normal_bg"></item>
</selector>

checked_bg的drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners android:radius="30dp"/>
    <stroke android:color="#c416ff"  android:width="2dp"/>
    <padding
        android:bottom="2dp"
        android:left="10dp"
        android:right="10dp"
        android:top="2dp"/>

</shape>

normal_bg的drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffffff" />
    <corners android:radius="30dp" />
    <padding
        android:bottom="2dp"
        android:left="10dp"
        android:right="10dp"
        android:top="2dp" />
</shape>

text_color的 drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#ff0000" android:state_checked="true"/>
    <item android:color="#f692ff"/>

</selector>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容