Two columns ListView with items of varying heights - android

I want to create a two columns listView with items of varying heights like that:
image
I have try this custom class found on GitHub and it was perfect but I have different problems because maybe the class is old (last update: 2014):
Child Item's images with onClickListener block the custom listView's scrolling
SwipeRefreshLayout appear always when scroll up (I want only when I am at the top of the list)
[Edit] Solution:
Proposed by Piyush:
StaggeredGridLayoutManager with RecyclerView

you can use StaggeredGridLayoutManager
you can use StaggeredGridLayoutManager
private StaggeredGridLayoutManager gaggeredGridLayoutManager;
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
for more information follow this link StaggeredGridLayoutManager

Why don't you use a ScrollView instead? So you'll have a parent ScrollView containing just one RelativeLayout. Then you'll place two vertical LinearLayout inside the Relative one in which you are going to place your varying heights cells.
Pseudo-code follows:
<ScrollView>
<RelativeLayout>
<LinearLayout orientation="vertical">
//insert you items here via GroupView.addView inside your activity
</LinearLayout>
<LinearLayout orientation="vertical">
</LinearLayout>
</RelativeLayout>
<ScrollView>

Related

Center RecycleView in Parent - Android

I am following a tutorial on grid layout. In particular, I am replicating Suragch's answer.
The code itself is working fine, except I would like to center the RecyclerView in the activity.
I've tried
android:gravity="center"
and
android:layout_gravity="center"
on both the RelativeLayout AND RecyclerView, but neither works.
Also, I would like to eliminate the gaps between each column. Does anyone know how to go about doing this?
Try android:layout_centerInParent=true into <RecyclerView>
Depending on your parent Layout
If its RelativeLayout then
android:layout_centerHorizontal=true in RecyclerView
if its LinearLayout then
android:layout_gravity=center_horizontal in RecyclerView
One more interesting thing you can do in Adapter
the main parent Layout in adapter
try doing this
android:gravity=center_horizontal in main layout of adapter

Showing full height layout above recyclerview inside scrollview

As the title says. I'm trying to display extremely complex layout with a full height of the viewport but I need it scrollable because under it there is a simple recyclerview with some items. I already thought about putting everything inside a multi type recyclerview adapter but the logic of the upper layout is so complex that I don't think it's possible.
I tried using NestedScrollView with fillViewport set to true but I'm stuck defining dimensions of this upper layout and recyclerview below it. Everything needs to be inside one layout because scrollview can't have more than one child, but when I put everything in a linearlayout and set the upper layout to match_parent it's showing fullscreen until data loads in the recyclerview below it. Then it's treating this upper layout as if it was wrap_content.
I'm out of ideas how can I do something like this. Preferably best would be to have some sort of ViewGroup which would support scrolling and resize the recyclerview below it as we scroll, but I'm not sure how to do it.
you need to set the layout to something like this:
<NestedScrollView - height:match_parent>
<LinearLayout - height:wrap_content>
<LinearLayout(topview) - height:wrap_content/>
<RecyclerView - height:wrap_content />
</LinearLayout>
</NestedScrollView>
And then you programmatically change the height of the "topview" to equal nestedscrollview.

Listview scroll with other elements on page

In my layout, I have two items; an ImageView and a ListView, the ImageView appears on top of the ListView in a LinearLayout. The ImageView displays a logo or a profile image while the ListView displays a list of personal / company info.
Now this is my problem, the ListView scrolls on it's own with the ImageView fixed at the top but I want the ImageView to scroll with the ListView so that it is not fixed statically on top.
How do I do this? Is this possible? And if yes can I see an example. Thanks. :)
Edit: I have considered removing the scroll from the ListView and simply just displaying the items without scroll then I can wrap both the ImageView and ListView inside a ScrollView. However I do not know if this is possible or how I can implement it.
Take NestedScrollView as you parent layout and add a LinearLayout as it's child and add your ImageView and listView as child to this linear layout .
<NestedScrollView>
<LinearLayout>
<ImageView/>
<ListView/>
</LinearLayout>
</NestedScrollView>

How to center RecyclerView in the center (and without scrolling) when possible?

This is a short question:
Suppose I have a RecyclerView that has undefined number of items, what should I do to it, so that if there is a small number of items that (all) can fit the screen, they will be centered and the user won't be able to scroll ?
Of course, if there are too many items, that cannot fit the screen, I would like to have the RecyclerView to show them all as normal (from the beginning to how much it can show, and allow to scroll).
To understand what I mean, I think such a thing is possible when using ScrollView (or HorizontalScrollView if in horizontal), together with a LinearLayout that sets the gravity to be centered .
OK, I think I've found a way:
first, we wait for the RecyclerView to finish its layout process, as I've found about here .
Then, you need to check which child views are shown (available in the LayoutManager that you use), and look at the first and last ones.
If both of them are exactly the same as those that of the total items, it means all needed views are shown, so I can add margins on both sides of the RecyclerView (or padding on its container), according to the space that's left.
I found one complicate way to achieve that.
Main concept: set the height of RV dynamicly in code
RecyclerView rv= (RecyclerView) findViewById(R.id.rv);
rv.setAdapter(new MySlideUpAdapter());
rv.setLayoutManager(new LinearLayoutManager(this));
ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
layoutParams.height=100;
rv.setLayoutParams(layoutParams);
You may need to calculate the RV's height by childcount*childheight to get pricise value. And don't forget to compare the height to the Parent Layout height, make sure RV's height is less than its Parent Layout height.
Here is my Layout
<RelativeLayout...>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_centerInParent="true"
android:layout_width="match_parent"
<-height will be changed in code, ignore the 50dp->
android:layout_height="50dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

How to add a LinearLayout in the same activity which has Listview?

I have an Activity where there is a Linear Layout which occupies nearly half of the screen and below that I need to keep a listview which has hold any number of items. Now initially I was doing like this
<ScrollView>
<LinearLayout>
</LinearLayout>
<ListView>
</ListView>
</ScrollView>
But reading all the threads I think it is not really a good idea keeping listview inside Scrollview. Also I am facing a lot of issues like the height of the Listview is not proper.
So how do I keep the layout and listview inside the same activity without using ScrollView?
It is not recommended to include a scrolling View (ListView) inside a ScrollView. What you want to achieve can be done by adding a HeaderView which can be inflated from another XML.
LinearLayout ll = inflater.inflate(R.layout.my_layout, null);
listView.addHeaderView(ll);

Categories

Resources