FlowLayoutDemo
来源:互联网 发布:外交部流氓回答知乎 编辑:程序博客网 时间:2024/06/06 01:19
添加依赖 compile ‘com.hyman:flowlayout-lib:1.1.2’
MainActivity
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TagFlowLayout tagFlowLayout = findViewById(R.id.id_flowlayout); Button btn=findViewById(R.id.btn); final TextView tv=findViewById(R.id.tv); final List<String>list=new ArrayList<>(); char a='A'; while (a<='Z'){ list.add(String.valueOf(a)); a++; } tagFlowLayout.setAdapter(new MyTagAdaper(list)); //事件 tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() { @Override public boolean onTagClick(View view, int position, FlowLayout parent) { Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show(); return true; } }); //点击标签时回调 tagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() { @Override public void onSelected(Set<Integer> selectPosSet) { Toast.makeText(MainActivity.this, selectPosSet.toString(), Toast.LENGTH_SHORT).show(); } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //获取全部选择数据 HashSet<Integer>set= (HashSet<Integer>) tagFlowLayout.getSelectedList(); StringBuilder sb=new StringBuilder(); for (Integer aSet : set) { sb.append(list.get(aSet)); sb.append(","); } tv.setText(sb.toString()); } }); } class MyTagAdaper extends TagAdapter<String>{ MyTagAdaper(List<String> datas) { super(datas); } @Override public View getView(FlowLayout parent, int position, String s) { TextView tv= (TextView) View.inflate(MainActivity.this,R.layout.tv,null); tv.setText(s); return tv; } }}
activity_main
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:zhy="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.chengqi.my_application.MainActivity" android:orientation="vertical"> <com.zhy.view.flowlayout.TagFlowLayout android:id="@+id/id_flowlayout" zhy:max_select="-1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="20dp"> </com.zhy.view.flowlayout.TagFlowLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="显示所选内容"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:layout_margin="10dp"/> </LinearLayout></LinearLayout>
tv.xml
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" android:background="@drawable/tv_selector" android:textColor="@drawable/text_color_selector" android:textSize="14sp" android:gravity="center" android:text="@string/text"/>
tv_selector
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/normal_bg" android:state_checked="true"/> <item android:drawable="@drawable/checked_bg"/></selector>
normal_bg
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/normal_bg" android:state_checked="true"/> <item android:drawable="@drawable/checked_bg"/></selector>
checked_bg
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF99CC" /> <corners android:radius="10dp"/></shape>
text_color_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/tag_select_textcolor_selected" android:state_checked="true"/> <item android:color="@color/tag_select_textcolor"/></selector>
在values/colors.xml文件中添加颜色
<color name="tag_select_textcolor">#FF99CC</color><color name="tag_select_textcolor_selected">#FFFFFF</color>
阅读全文
0 0
- FlowLayoutDemo
- 3月15日,FlowLayoutDemo,每日20行。
- 文章标题
- cocos js 截屏 (原生 + H5)
- centos7修改网关和配置ip
- 3456. Huge Numbers (Large) ECNU
- poj3481 Double Queue
- FlowLayoutDemo
- Spinner android:entries属性/ xml 中 @array 写法
- POJ 3984 迷宫问题(BFS+路径记录)
- uva 1584
- java(循环,分支)语句总结
- JavaEE之反射
- mysql 查看及修改表结构
- 在Windows平台上安装Node.js及NPM模块管理
- Markdown入门操作