1. Add dependencies
①. Add in the project's build.gradle file
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
②. Add dependencies to the module's build.gradle file
dependencies {
compile 'com.github.LRH1993:AutoFlowLayout:1.0.5'
}
2. Property description
The following table is the custom attribute description, which can be declared in xml, and there are corresponding get/set methods, which can be dynamically added in the code.
3. Example of use
layout
<?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>
code setting data
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;
}
});
In the same way as ListView and GridView, you can implement FlowAdapter.
Simple code application:
模拟数据
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);