Does anyone know how to do flow layout using RecyclerView?
How to change span count dynamically?
Here is the full example of using custom Library which acts like List GitHubLibrary TagLayout
- Sample Code:-
mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
{
@Override
public View getView(FlowLayout parent, int position, String s)
{
TextView tv = (TextView) mInflater.inflate(R.layout.tv,
mFlowLayout, false);
tv.setText(s);
return tv;
}
});
Using below code you can pre set selection you wanted:-
mAdapter.setSelectedList(1,3,5,7,8,9);
Will show result like below:-
You can use FlowLayout and put it as a child of ScrollView. Samples for flow layout are available in repository.
<?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>
Also you can add or remove views programatically using following methods given in sample.
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);
}
来源:https://stackoverflow.com/questions/34012470/does-anyone-know-how-to-do-flow-layout-using-recyclerview