1.添加依赖
①.在项目的 build.gradle 文件中添加
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
②.在 module 的 build.gradle 文件中添加依赖
dependencies {
compile 'com.github.LRH1993:AutoFlowLayout:1.0.5'
}
2.属性说明
下表是自定义的属性说明,可在xml中声明,同时有对应的get/set方法,可在代码中动态添加。
3.使用示例
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.library.AutoFlowLayout
android:id="@+id/afl_cotent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
代码设置数据
mFlowLayout.setAdapter(new FlowAdapter(Arrays.asList(mData)) {
@Override
public View getView(int position) {
View item = mLayoutInflater.inflate(R.layout.special_item, null);
TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
tvAttrTag.setText(mData[position]);
return item;
}
});
与ListView,GridView使用方式一样,实现FlowAdapter即可。
简单代码应用:
模拟数据
private String[] mData = {"Java","C++","Python","JavaScript","Ruby","Swift","MATLAB","Scratch","Drat","ABAP","COBOL","Fortran","Scala","Lisp",
"Kotlin","Erlang","Groovy","Scheme","Rust","Logo","Prolog","LabVIEW"};
找控件
mFlowLayout = (AutoFlowLayout) findViewById(R.id.afl_cotent);
mLayoutInflater = LayoutInflater.from(this);
加入数据
for (int i = 0; i< 10; i ++ ){
View item = mLayoutInflater.inflate(R.layout.sub_item, null);
tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
tvAttrTag.setText(mData[i]);
mFlowLayout.addView(item);