FlowLayout標籤流佈局

轉載請註明出處:https://blog.csdn.net/mr_leixiansheng/article/details/80924038

話不多說先上圖

   

偷懶引用了三方庫,啊哈哈,代碼如下

佈局

<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">

    <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"
        >
    </com.zhy.view.flowlayout.TagFlowLayout>
</LinearLayout>
<?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>

backgroubd、textColor自己設置,就是選擇一個顏色,不選擇則另一個顏色。在此就不貼出了

Main

package com.example.leixiansheng.flowlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

	private String[] mVals = new String[]
			{"Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello",
					"Android", "Weclome", "Button ImageView", "TextView", "Helloworld",
					"Android", "Weclome Hello", "Button Text", "TextView"};

	private TagFlowLayout mFlowLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
		//mFlowLayout.setMaxSelectCount(3);		//設置最大選擇條數
		mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
		{

			@Override
			public View getView(FlowLayout parent, int position, String s)
			{
				TextView tv = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.tv,
						mFlowLayout, false);
				tv.setText(s);
				return tv;
			}

			/**
			 * 設置被選擇項
			 * @param position
			 * @param s
			 * @return
			 */
			@Override
			public boolean setSelected(int position, String s)
			{
				return s.equals("Android");	// 等於 Android 字段的標籤默認選中
			}
		});

		/**
		 * 標籤點擊響應
		 */
		mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
		{
			@Override
			public boolean onTagClick(View view, int position, FlowLayout parent)
			{
				Toast.makeText(MainActivity.this, mVals[position], Toast.LENGTH_SHORT).show();
				//view.setVisibility(View.GONE);
				return true;
			}
		});

		/**
		 *  被選中標籤位置集合
		 */
		mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
		{
			@Override
			public void onSelected(Set<Integer> selectPosSet)
			{
				setTitle("choose:" + selectPosSet.toString());

				setSelectedList(selectPosSet);
			}
		});
	}

	private void setSelectedList(Set<Integer> selectPosSet) {
		List<String> tagList = new ArrayList<String>();
		Iterator it = selectPosSet.iterator();
		while (it.hasNext()) {
			tagList.add(mVals[(int)it.next()]);
		}
		Log.i("MainActivity", tagList.toString());
	}
}

打印Log如下

07-05 11:05:01.382 22522-22522/com.example.leixiansheng.flowlayout D/zhy: onSelected 3
07-05 11:05:01.385 22522-22522/com.example.leixiansheng.flowlayout I/MainActivity: [Button]
07-05 11:05:02.745 22522-22522/com.example.leixiansheng.flowlayout D/zhy: onSelected 4
07-05 11:05:02.749 22522-22522/com.example.leixiansheng.flowlayout I/MainActivity: [Button, TextView]
07-05 11:05:03.738 22522-22522/com.example.leixiansheng.flowlayout D/zhy: onSelected 8
07-05 11:05:03.740 22522-22522/com.example.leixiansheng.flowlayout I/MainActivity: [Button ImageView, Button, TextView]

參考與:https://github.com/hongyangAndroid/FlowLayout

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章