I want to show images in a gridview and want to show them in a single row.
No of columns in the gridview will be equal to no of images. Now what I want is when the no. of images are more and taking the width more than the screen size, I want to enable horizontal scrolling to the gridview, so that the images will be visible.
How can I achieve this, please help me if you have any idea.
I have already tried Gallery widget but it is not giving me the desired results.
Here is a Library called Two Way Grid View You can use this GridView in Both Orientations (Horizontal and Vertical)
Link to the Library:
https://github.com/jess-anders/two-way-gridview
Sample Usage:
<?xml version="1.0" encoding="utf-8"?>
<com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="auto_fit"
app:numRows="auto_fit"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center"/>
I hope this solves your problem.
P.S: You can also check out the sample provided with the Library for further usage demo.
You can achieve the functionality using RecyclerView with spanCount (number of rows in your horizontal case)
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, spancount, GridLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(gridLayoutManager);
If you need Staggered behavior the use StaggeredGridLayoutManager
GridLayoutManager StaggeredGridLayoutManager
Related
So I have a RecyclerView that uses a GridLayoutManager and uses a super simple adapter that does nothing besides put text in textviews.
My issue is that I want the text to evenly fill out the entire grid (down to the buttons) while making the grid not scrollable. I just want a grid that doesn't scroll and fills up the layout.
Here is what I currently have (the grid is everything between the "you win" and the buttons at the bottom):
val numberOfColumns = 3
resultsRecyclerView.layoutManager = GridLayoutManager(this, numberOfColumns)
val adapter = GridAdapter(this, gridData)
resultsRecyclerView.adapter = adapter
grid item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:textColor="#color/textColor"/>
</LinearLayout>
I was told this would be a good way of making a grid without doing each textview individually. Is there a better way of making a non-scrollable, evenly spaced grid? Any help is appreciated.
Try changing linear layout's layout_weight property.
The RecyclerView has scrolling baked in the widget. If you just need data on the screen consider the ScrollView and add these two lines of code
View.setVerticalScrollBarEnabled(false);
View.setHorizontalScrollBarEnabled(false);
Then I am guessing just put a bunch of TextViews in the ScrollView to display data
I think what you're trying to reach for a non-scroll list is:
GridLayoutManager glm = new GridLayoutManager(this, 3) {
#Override
public boolean canScrollVertically() {
return false;
}
}
It's in Java but not hard to port it to Kotlin, hope it helps.
I am using a Recycler view inside a Horizontal Scroll View to display threaded comments like below:
Comment1
Comment1Child1
Comment1Child2
Comment1Child2Child1
Comment1Child3 Comment2
etc.
I have done this with the following XML:
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbarSize="2dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="47dp"
android:minWidth="200dp" />
</HorizontalScrollView>
You'll see I set a min width of 200dp so that the comments never get too small and the deeper down the thread they go the further off screen it will be pushed.
It is displaying perfectly but I am unable to scroll horizontally. I can see the scroll bars but I cannot scroll horizontally.
I have fiddled with disabling Touch and nestedScrollingEnabled in the RV but not sure what the real solution is. Nothing seems to work?
Any help would be much appreciated! Thank you
Remove HorizontalScrollView you can give LinearLayoutManager with horizontal orientation like this
LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
reclyclerView.setLayoutManager(layoutManager);
//recyclerView.setMinimumWidth(200); optional for width if u need
I am working in android material design api & want to display some data in grid format. I tried both GridLayout and StaggeredGridlayout and both look same. For general information, i want to ask what is the difference between Gridlayout and StaggeredGridlayout?
Thank you.
Grid View : It is is a ViewGroup that displays items in a two-dimensional, scrollable grid. In this each Grid is of same size (Height and width). Grid View shows symmetric items in view.
Staggered Grid View : It is basically an extension to Grid View but in this each Grid is of varying size(Height and width). Staggered Grid View shows asymmetric items in view.
Tutorial to implement Staggered Grid View :
Staggered Grid View
Pinterest Masonry layout Staggered Grid View
My time at Oodles Technologies taught me about staggered.
I'll share that.
StaggeredGridLayout is a LayoutManager, it is just like a GridView but in this grid each view have its own size (height and width). It supports both vertical and horizontal layouts.
Below are some basic steps to create a staggered grid:
1) Create a view.
As we know StaggeredGrid is not a direct view, it is a LayoutManager that lays out children in a staggered grid formation. We use RecyclerView as a view for the staggerd grid.
Here is our RecyclerView in layout:
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="#+id/favPlaces"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
2) Set StaggeredGridLayout LayoutManager.
Once our view is ready, let's use LayoutManager to create grids on the view:
RecyclerView favPlaces = (RecyclerView) findViewById(R.id.favPlaces);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
favPlaces.setLayoutManager(layoutManager);
favPlaces.setHasFixedSize(true);
3) Adapter to inflate the StaggeredGrid views.
To inflate the data in form of a grid, we first need a layout which will represent that data. We are using CardView for this and the layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardcornerradius="4dp"
app:cardusecompatpadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<ImageView
android:id="#+id/placePic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustviewbounds="true"
android:scaletype="fitXY" />
<TextView
android:id="#+id/placeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textsize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
After we setup our all the basic steps, it's time to complete our main activity. Here is the complete code of MainActivity:
public class MainActivity extends AppCompatActivity {
int placeImage[]= {R.drawable.agattia_airport_lakshadweep,R.drawable.nainital,R.drawable.goa,
R.drawable.lotus_temple,R.drawable.valley_of_flowers,R.drawable.ranikhet,R.drawable.dehradun,R.drawable.nainital1};
String placeName[]= {"Lakshadweep, India","Nainital, India","Goa, India","Lotus-Temple, India","Valley-Of-Flowers, India","Ranikhet, India",
"Dehradun, India","Nainital, India"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView favPlaces = (RecyclerView) findViewById(R.id.favPlaces);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
favPlaces.setLayoutManager(layoutManager);
favPlaces.setHasFixedSize(true);
ArrayList<PlaceDetails> placeList = getPlaces();
StaggeredAdapter staggeredAdapter = new StaggeredAdapter(placeList);
favPlaces.setAdapter(staggeredAdapter);
}
private ArrayList<PlaceDetails> getPlaces() {
ArrayList<PlaceDetails> details = new ArrayList<>();
for (int index=0; index<placeImage.length;index++){
details.add(new PlaceDetails(placeImage[index],placeName[index]));
}
return details;
}
}
StaggeredGridlayout
This lays out children in a staggered grid formation.
It supports horizontal & vertical layout as well as an ability to layout children in reverse.
Staggered grids are likely to have gaps at the edges of the layout.
To avoid the gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int).
GridLayout
This lays out its children in a rectangular grid.
The grid is composed of a set of infinitely thin lines that separate the viewing area into cells.
Children occupy one or more contiguous cells, as defined by their rowSpec and columnSpec layout parameters.
A staggered grid Layout includes multiple columns with multiple rows of varying sizes.
It allows for a flexible column/row view with a header and footer and looks fairly easy to implement, though Gradle users will have an easier time than those working with Eclipse and Ant. This is what the view looks like in the Etsy Github app for which it was developed.
Whereas a GridLayout is a layout that places its children in a rectangular grid.
It was introduced in API level 14, and was recently backported in the Support Library. Its main purpose is to solve alignment and performance problems in other layouts. Check out this tutorial if you want to learn more about GridLayout.
I want to use a RecyclerView(list like) below another RecyclerView(Grid like) as in flipboard
I tried two RecyclerView inside ScrollView with wrap content but nothing is showing.
I can able to see two views if it is placed in linear layout and equal weight is added to it. But that does not look like this app View.
This is my layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/parent_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="#dimen/default_small_padding"
android:gravity="center"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/category_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:gravity="center"
android:visibility="visible"/>
</LinearLayout>
There's actually a really simple way to achieve the desired layout with just one RecyclerView!
The key is using a GridLayoutManager and its SpanSizeLookup.
First of all define your GridLayoutManager like this:
GridLayoutManager layoutManager = new GridLayoutManager(context, spanCount);
where spanCount is the maximum amount of spans/columns you want to have. In your case, spanCount should be 2;
Now you need a way to tell this layoutManager how many spans an item should span.
A simple way would be to use a viewtype/ViewHolder for items that should span just one column and another one for items that span the whole width.
Let's assume you define the viewtype for grid-items as VIEWTYPE_GRID_ITEM and the viewtype for standard listitems as VIEWTYPE_LIST_ITEM.
You can then use these viewtypes to tell the layoutManager when to use just one span:
layoutManager.setSpanSizeLookup(new SpanSizeLookup() {
#Override
public int getSpanSize(int position){
return adapter.getItemViewType(position) == VIEWTYPE_GRID_ITEM ? 1 : spanCount;
}
});
Finally, set the layoutManager to your RecyclerView:
recyclerView.setLayoutManager(layoutManager);
And that's it!
I don't think that they use two ListViews (or ListView + GridView).
I think they use a ListView ( the one in the bottom) and they add a custom Header to this list (the one in the top), which look like a GridView.
Or if they use a Recyclerview, they can achieve this by using a different layout in the adapter for the first item.
PS: it's not recommended to use two scrollable view inside each other (like Recyclerview inside ScrollView).
In android we are advise to use only one scrollable view at time , if you want to use one scrollable view inside other one refer below links.
1)Android list view inside a scroll view
2) https://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android
I am doing a task which retrieves images from server and displays in GridView in the application. This Grid view is scrolling up and down. But i want to scroll this view left to right as the menu screen scrolls.
Is it possible with grid view? Or Is there any better way to do this? please help me in doing this.
thanks in advance.
This isn't easily possible with the stock Android GridView. Try using this library:
two-way-gridview
(I found this library in this other answer: Horizontal scrolling grid view)
Is there any better way to do this?
Yes, there is.
You can achieve this in 3 lines using a RecyclerView with a horizontal GridLayoutManager:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(new CustomAdapter(arrayList));
The RecyclerView supports applications built with the SDK 7 or larger.
If you want to make it even easier, take a look at the HorizontalGridView class if you are working with an application that is built for the API 17 or larger.
Here's a link of an example of a simple RecyclerView Adapter.
Try with below layout
<HorizontalScrollView android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<GridView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/gridview" android:columnWidth="50dp" android:padding="10dp"
android:horizontalSpacing="8dp" android:verticalSpacing="12dp"
android:numColumns="3" android:scrollbars="horizontal"/>
</HorizontalScrollView>