流式布局使用鸿洋大神写的库:com.hyman:flowlayout-lib:1.1.2

在项目中一般都会用到流式布局

布局文件

代码设置适配器

private TagAdapter tagScreenAdapter;
id查找,因为我是用在dialog中的,所以在dialog中查找
TagFlowLayout id_flow_layout_screen = bottomScreenDialog.findViewById(R.id.id_flowlayout_screen);

item_organization_tag为标签列表的item布局   布局文件如下:


创建适配器时需要创建集合数据tagScreenResultBeanList集合数据,集合中存放的对象GetScreeningUnitsByOrganizationUnitCodeResponse

tagScreenAdapter = new TagAdapter(tagScreenResultBeanList) {
    @Override
    public View getView(FlowLayout parent, int position, GetScreeningUnitsByOrganizationUnitCodeResponse o) {
        //can use viewHolder
        TextView tv = (TextView) LayoutInflater.from(SearchPatientActivity.this).inflate(R.layout.item_organization_tag,
                parent, false);
        if ((o != null)) {
            tv.setText(o.getDisplayName());
        }

        return tv;
    }
};
id_flow_layout_screen.setAdapter(tagScreenAdapter);//设置适配器
//当数据改变时,适配器要执行刷新操作  tagScreenAdapter.notifyDataChanged();
//下面是设置标签点击事件 点击事件处理 screenTagClick(position);自己写的点击事件处理
id_flow_layout_screen.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
    @Override
    public boolean onTagClick(View view, int position, FlowLayout parent) {
        screenTagClick(position);
        return true;
    }
});

你可能感兴趣的:(Android,android,安卓,移动开发)