Android GridLayout with dynamic number of columns per row
You can use FlexboxLayoutManager
Add the following dependency to your build.gradle file:
implementation 'com.google.android:flexbox:1.0.0'
SAMPLE CODE
LAYOUT.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
ACTIVTY CODE
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<String> arrayList = new ArrayList<>();
DataAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
initArray();
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setJustifyContent(JustifyContent.FLEX_END);
recyclerView.setLayoutManager(layoutManager);
adapter = new DataAdapter(this, arrayList);
recyclerView.setAdapter(adapter);
}
private void initArray() {
arrayList.add("ioreeoe");
arrayList.add("fghfgh");
arrayList.add("ftyjyjhghgh");
arrayList.add("jfgewrg");
arrayList.add("rwrewr");
arrayList.add("ghyjtyfghh");
arrayList.add("gfhfgh");
arrayList.add("gfhfht");
arrayList.add("retretret");
arrayList.add("retret");
arrayList.add("ioreeoe");
arrayList.add("fghfgh");
arrayList.add("ftyjyjhghgh");
arrayList.add("jfgewrg");
arrayList.add("rwrewr");
arrayList.add("ghyjtyfghh");
arrayList.add("gfhfgh");
arrayList.add("gfhfht");
arrayList.add("retretret");
arrayList.add("retret");
arrayList.add("ioreeoe");
arrayList.add("fghfgh");
arrayList.add("ftyjyjhghgh");
arrayList.add("jfgewrg");
arrayList.add("rwrewr");
arrayList.add("ghyjtyfghh");
arrayList.add("gfhfgh");
arrayList.add("gfhfht");
arrayList.add("retretret");
arrayList.add("retret");
}
}
Adapter code
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by nilesh on 3/4/18.
*/
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
Context context;
ArrayList<String> arrayList = new ArrayList<>();
public DataAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, int position) {
holder.title.setText(arrayList.get(position));
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.nilu);
}
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nilu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/test"
android:padding="10dp"
android:textColor="#050505" />
</LinearLayout>
@drawable/test
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="30dp"/>
<solid android:color="#d10e0e"/>
<stroke android:width="1dip" android:color="#070fe9" />
</shape>
</item>
</selector>
RESULT
StaggeredGridLayout with dynamic # of objects per row, line-wrapping
You should consider FlexboxLayout
. Take a look at the readme at the GitHub project site. I think you will find that it will fit your needs. There is also a FlexboxLayoutManager
for RecyclerView
. This is all supported by Google.
You can download the GitHub project to play with it to see if you can use it. There are also some demo playground apps in the Google Play store. Just search for it.
Android grid view Change number of columns depending on data
You need to spacify viewType in recycler adapter.
DEMO HERE
public class RVStatisticAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Statistic> mList;
public RVStatisticAdapter(List<Statistic> list) {
this.mList = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case CITY_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.statistic_row_one, parent, false);
return new CityViewHolder(view);
case EVENT_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.statistic_row_two, parent, false);
return new EventViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Statistic object = mList.get(position);
if (object != null) {
switch (object.getType()) {
case CITY_TYPE:
((CityViewHolder) holder).mTitle.setText(object.getTitle());
((CityViewHolder) holder).no.setText(object.getNo());
((CityViewHolder) holder).playerone.setText(object.getPlayer_one());
break;
case EVENT_TYPE:
((EventViewHolder) holder).mTitle.setText(object.getTitle());
((EventViewHolder) holder).no.setText(object.getNo());
((EventViewHolder) holder).playerone.setText(object.getName());
((EventViewHolder) holder).playertwo.setText(object.getPlayer_two());
break;
}
}
}
@Override
public int getItemCount() {
if (mList == null)
return 0;
return mList.size();
}
@Override
public int getItemViewType(int position) {
if (mList != null) {
Statistic object = mList.get(position);
if (object != null) {
return object.getType();
}
}
return 0;
}
public static class CityViewHolder extends RecyclerView.ViewHolder {
private TextView mTitle,no,playerone;
public CityViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
no = (TextView) itemView.findViewById(R.id.tv_no);
playerone = (TextView) itemView.findViewById(R.id.tv_player_one);
}
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
private TextView mTitle,no,playerone,playertwo;
public EventViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
no = (TextView) itemView.findViewById(R.id.tv_no);
playerone = (TextView) itemView.findViewById(R.id.tv_player_one);
playertwo = (TextView) itemView.findViewById(R.id.tv_player_two);
}
}
}
GridView with dynamic number of columns in each row
I guess that this is not a single GridView but a combination of multiple Layouts. Just make a LinearLayout and decide according to the content, which layout you want to have in a row.
Variable number of columns in GridLayoutManager
Take a look at the setSpanSizeLookup
method of the GridLayoutManager
. It lets you specify the span size for specific positions of your RecyclerView
. So maybe you could use it to fit with your requirements for the variable column number.
Edit:
GridLayoutManager manager = new GridLayoutManager(context, 2); // MAX NUMBER OF SPACES
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position == 1 || position == 6) {
return 2; // ITEMS AT POSITION 1 AND 6 OCCUPY 2 SPACES
} else {
return 1; // OTHER ITEMS OCCUPY ONLY A SINGLE SPACE
}
}
});
When using this sort of layout manager
your RecyclerView
should look like this:
+---+---+
| 0 | |
+---+---+
| 1 |
+---+---+
| 2 | 3 |
+---+---+
| 4 | 5 |
+---+---+
| 6 |
+---+---+
(only boxes with numbers represent items of your RecyclerView
, other boxes are just empty spaces)
Dynamically change the number of columns of a GridLayoutManager
mGridLayoutManager = new GridLayoutManager(mContext, 2);
mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);
/**
* Helper class to set span size for grid items based on orientation and device type
*/
GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return mHomeListAdapter.getItemViewType(position) == TYPE_PREMIUM ? 2 : 1;
}
};
Android GridView wrap_content cell width and dynamic number of items per row
You can use tag view for android using that you can create view as you want.
see below library for more information
- https://github.com/whilu/AndroidTagView
Gridlayout manager with different column numbers in each row
Best light weight library i use
compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
Benifit is you dont need to change your existing code, you just need to change recyclerview's LayoutManager like
recyclerView.setLayoutManager(new FlowLayoutManager());
You are all set, Happy coding :)
Update You know already how to populate recycler view. But verify by following
private void setAdapter() {
recyclerView = (RecyclerView) fragment_view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new FlowLayoutManager());
myRecyclerAdapter = new AdapterOrders(getActivity(), list);
recyclerView.setAdapter(myRecyclerAdapter);
}
Call myRecyclerAdapter.notifyDataSetChanged();
after list is change from any source.
Related Topics
How to Place Fragment Arrylist Scrollabletabsactivity
Android Dialogfragment VS Dialog
Show an Alert Dialog in Broadcast Receiver After a System Reboot
Better Way to Format Currency Input Edittext
How to Check If an Activity Is the Last One in the Activity Stack for an Application
How to Get the List of Running Applications
Notifydatasetchange Not Working from Custom Adapter
Android Log.V(), Log.D(), Log.I(), Log.W(), Log.E() - When to Use Each One
Android List View Drag and Drop Sort
Adding Table Rows Dynamically in Android
Detecting Whether a Headset Is Plugged into an Android Device or Not
How to Copy Text Programmatically in My Android App
How to Convert Dp, Px, Sp Among Each Other, Especially Dp and Sp
Android 2.2 Mediaplayer Is Working Fine with One Shoutcast Url But Not with the Other One
Ffmpeg for a Android (Using Tutorial: "Ffmpeg and Android.Mk")