答案 0 :(得分:3)
以下是完整的示例 使用自定义库,其行为类似于列表GitHubLibrary TagLayout
mAdapter.setSelectedList(1,3,5,7,8,9);
使用以下代码,您可以预先设置所需的选择: -
<target name="tests" depends="testscompile,mkdirtests_clean,mkdirtests">
<junit fork="yes" forkmode="once" haltonfailure="no" printsummary="yes" >
<jvmarg value="-javaagent:../JUnitlib/jmockit.jar"/>
<sysproperty key="jmockit-coverage-output" value="html"/>
<sysproperty key="jmockit-coverage-outputDir" value="${reports.dir}"/>
<sysproperty key="jmockit-coverage-srcDirs" value="${src}"/>
<sysproperty key="jmockit-coverage-metrics" value="all"/>
<classpath>
<pathelement location="${classes}" />
<pathelement location="../JUnitlib/jmockit.jar" />
<pathelement location="../JUnitlib/junit-4.12.jar" />
<pathelement location="../JUnitlib/hamcrest-core-1.3.jar" />
<fileset dir="../WebContent/WEB-INF/lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${tomcat.root}">
<include name="lib/log4j-1.2.17.jar"/>
</fileset>
</classpath>
<batchtest todir="${docs.dir}">
<fileset dir="${classes}">
<include name="**/*Test.class" />
</fileset>
</batchtest>
<formatter type="xml"/>
</junit>
<!-- Junit Report generation -->
<junitreport todir="${reports.dir}">
<fileset dir="${docs.dir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${reports.dir}" />
</junitreport>
<antcall target="report.zip"/>
</target>
将显示如下结果: -
答案 1 :(得分:2)
您可以使用FlowLayout并将其作为ScrollView的子项。 存储库中提供了用于流布局的示例。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.wefika.flowlayout.FlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:minHeight="50dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</com.wefika.flowlayout.FlowLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world" />
</LinearLayout>
</ScrollView>
&#13;
您还可以使用示例中给出的以下方法以编程方式添加或删除视图。
public void addItem(View view) {
int color = getResources().getColor(R.color.holo_blue_dark);
View newView = new View(this);
newView.setBackgroundColor(color);
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(100, 100);
params.rightMargin = 10;
newView.setLayoutParams(params);
mFlowLayout.addView(newView);
}
public void removeItem(View view) {
mFlowLayout.removeView(getLastView());
}
public void toggleItem(View view) {
View last = getLastView();
if(last.getVisibility() == View.VISIBLE) {
last.setVisibility(View.GONE);
} else {
last.setVisibility(View.VISIBLE);
}
}
private View getLastView() {
return mFlowLayout.getChildAt(mFlowLayout.getChildCount() - 1);
}
&#13;
答案 2 :(得分:0)
最好的解决方案是将 RecyclerView 与 google FlexLayoutManager 结合使用
// Set layout manager
val layoutManager = FlexboxLayoutManager(context)
recyclerview.layoutManager = layoutManager
// Now you can add your normal recyclerview adapter
recyclerview.adapter = MyListAdapter(list)
在 build.gradle 文件中添加以下依赖项
implementation 'com.google.android:flexbox:2.0.1'
这会起到很好的作用。