Android第三方流式佈局FlowLAyout簡單實用(搜索歷史記錄)

效果圖:

導入大Model下:

maven { url 'https://jitpack.io' }

builde.gradle依賴: 

 implementation 'com.github.LRH1993:AutoFlowLayout:1.0.5'

 佈局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/l"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/sousuo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索" />
    </LinearLayout>

    <!--控件-->
    <com.example.library.AutoFlowLayout
        android:id="@+id/flowLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/l" />

    <Button
        android:id="@+id/clear_history"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:text="清除歷史記錄" />


</RelativeLayout>

 條目佈局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--負責展示歷史數據的條目-->

        <TextView
            android:id="@+id/auto_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/serch_bg"
            android:gravity="center"
            android:padding="5dp"
            android:text="1111" />
</LinearLayout>

shape :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke
        android:width="1.5dp"
        android:color="#00c4ff" />
    <corners android:radius="15dp" />
</shape>

代碼: 

public class HomeFragment extends Fragment {

    private Button sousuo;
    private EditText editText;
    private AutoFlowLayout flowLayout;
    private ArrayList<String> list;
    private Button clear_history;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null, false);
        //控件
        sousuo = view.findViewById(R.id.sousuo);
        editText = view.findViewById(R.id.edit_text);
        flowLayout = view.findViewById(R.id.flowLayout);
        clear_history = view.findViewById(R.id.clear_history);

        list = new ArrayList<>();

        //點擊獲取輸入框的值
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();
                list.add(text);
                //添加數據方法
                addData(list);
            }
        });


        //點擊清除歷史記錄
        clear_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.getText().clear();
                list.clear();
                flowLayout.removeAllViews();

            }
        });
        return view;
    }


    private void addData(final ArrayList<String> list) {
        //流式佈局適配器
        flowLayout.setAdapter(new FlowAdapter(list) {
            @Override
            public View getView(int i) {
                //引入視圖
                View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item_flowlayout, null, false);
                //獲取視圖控件
                TextView auto_tv = inflate.findViewById(R.id.auto_tv);
                //修改值
                auto_tv.setText(list.get(i));
                //清空當前集合
                list.clear();
                //返回視圖
                return inflate;
            }
        });
    }
}

 

 

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