Android第三方流式布局FlowLayout简单实用(搜索历史记录)

效果图:

Android第三方流式布局FlowLayout简单实用(搜索历史记录)_第1张图片

导入大Model下:

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

builde.gradle依赖: 

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

 布局文件:



    

        

        

 条目布局文件:


    

        

shape :


    
    
    

代码: 

public class HomeFragment extends Fragment {

    private Button sousuo;
    private EditText editText;
    private AutoFlowLayout flowLayout;
    private ArrayList 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 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;
            }
        });
    }
}

 

 

你可能感兴趣的:(Android)