1. 程式人生 > >Android中的封裝流式佈局FlowLayout

Android中的封裝流式佈局FlowLayout

鴻洋的GitHub:https://github.com/hongyangAndroid/FlowLayout

第一步:加依賴

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

第二步:建立一個Adapter繼承TagAdapter

public class MyFlowAdapter extends TagAdapter<String> {

    private Context context;

    public MyFlowAdapter(Context context,List<String> datas) {
        super(datas);
        this.context=context;
    }

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

    /**
     * 選中時的回撥方法
     * @param position
     * @param view
     */
    @SuppressLint("ResourceAsColor")
    @Override
    public void onSelected(int position, View view) {
        super.onSelected(position, view);
        view.setBackgroundColor(R.color.colorAccent);
    }

    /**
     * 預設的回撥方法
     * @param position
     * @param view
     */
    @SuppressLint("ResourceAsColor")
    @Override
    public void unSelected(int position, View view) {
        super.unSelected(position, view);
        view.setBackgroundColor(R.color.colorPrimaryDark);
    }
}

第三步:建立一個子佈局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


</TextView>

第四步:使用頁面的佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--
        max_select:-1為不限制選擇數量,>=1的數字為控制選擇tag的數量
    -->
    <com.zhy.view.flowlayout.TagFlowLayout
        android:padding="20dp"
        app:max_select="-1"
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.zhy.view.flowlayout.TagFlowLayout>

</LinearLayout>

第五步:在Activity中的使用方式

public class MainActivity extends AppCompatActivity {

    List<String> list = new ArrayList<>();
    private TagFlowLayout mFlow;

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

    private void initData() {
        for (int i = 0; i < 50; i++) {
            list.add("標籤"+(i+i*20));
        }
        MyFlowAdapter adapter=new MyFlowAdapter(this,list);
        mFlow.setAdapter(adapter);
        //點選時的回撥方法
        mFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Toast.makeText(MainActivity.this, "點選", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        //點選時回撥所選中的集合
        mFlow.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                
            }
        });
        //預先設定選中
        adapter.setSelectedList(1,3,5,7,8,9);
        //選中的下標集合
        Set<Integer> selectedList = mFlow.getSelectedList();
    }

    private void initView() {
        mFlow = (TagFlowLayout) findViewById(R.id.flow);
    }
}