flowlayout--搜索框

导入依赖

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

 

搜索框布局

  

点击搜索框跳转

  editFind.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               Intent intent = new Intent(getActivity(),SearchActivity.class);
               startActivity(intent);
               return true;
           }
       });

SearchActivity布局



    
        
        
        
    

    




SearchActivity类

public class SearchActivity extends AppCompatActivity {
    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.search_text)
    TextView searchText;
    @BindView(R.id.flow_layout)
    FlowLayout flowLayout;
    private List stringList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        stringList = new ArrayList<>();
        stringList.add("小米3是一代机王");
        stringList.add("华为");
        stringList.add("华为5g");
        stringList.add("华为");
        stringList.add("百度ai");
        stringList.add("小米ai是产业链的一个布局产品");
        stringList.add("虚拟于现实");
        stringList.add("小米");
        searchClick();
    }

    private void searchClick() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10,5,10,5);
        if (flowLayout!=null){
            flowLayout.removeAllViews();
        }
        for (int i = 0; i < stringList.size(); i++) {
            TextView textView = new TextView(this);
            textView.setPadding(28,10,28,10);
            textView.setText(stringList.get(i));
            textView.setMaxEms(10);
            textView.setSingleLine();
            textView.setLayoutParams(layoutParams);
            flowLayout.addView(textView, layoutParams);
        }

    }

    @OnClick({R.id.back_img, R.id.search_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                Intent intent = new Intent(SearchActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.search_text:
                String string = editSearch.getText().toString();
                if (string.equals("")){
                    Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    stringList.add(string);
                    searchClick();
                    break;
                }
        }
    }
}

 

你可能感兴趣的:(flowlayout--搜索框)