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;
            }
        });
    }
}

 

猜你喜欢

转载自blog.csdn.net/weixin_43917449/article/details/88342450
今日推荐