How to make recycler view start adding items from center? - android

I have a recyclerView with "Horziontal Linear Layout" as layout manager.
Recycler View is in a frame layout, with layout_gravity = "center", and layout_width="wrap_content"
I want recycler view start adding items from center.
Here is what I want:
And Here is what I am getting:
You can see that in the last image items are added from left. I want it to add items from center as shown in the first three images .

i had same issue and solved like this for my horizontal RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="68dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
Here the main part is android:layout_centerHorizontal="true"
and layout_width="wrap_content"

Solved:
How do I make WRAP_CONTENT work on a RecyclerView
This was the solution, I had to enforce the wrap_content for recycler view, So a custom Layout manager worked for me. Hope this will help others:)

Related

Android recyclerview itemDecoration divider is invisible until scroll

I've got a simple RecyclerView like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/tempRv"
android:layout_width="match_parent"
android:layout_height="match_parent"
</android.support.v7.widget.RecyclerView>
I add divider in onCreate() method in Fragment:
mRv = (RecyclerView) view.findViewById(R.id.tempRv);
mRv.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
I also have a simple Adapter that holds the items.
The problem is, that the divider is not visible until I scroll Up or Down the recyclerView.
I tried:
Scrolling programmatically by using scrollTo(position) method to
the last item and the first item right after updating items in the
adapter.
Adding NestedScrollView as a parent and scrolling
programmaticaly the scrollView by fullScroll(View.FOCUS_UP).
Nothing helps. The dividers are visible only after physical scrolling.
The wierd thing is that I have another RecyclerViews that work fine.
EDIT
Due to Sharan Salian request to add the item layout to the post so he could reproduce this behaviour, I actually tried to reproduce it by myself. The item parent layout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:transitionGroup="true"
android:background="?android:attr/selectableItemBackground"
android:padding="16dp">
...
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
...
</RelativeLayout>
I didn't mention before that I'm using the item as a SharedElement, that's why I added an attribute android:transitionGroup="true" to the parent tag.
After removing this attribute, the DividerItemDecoration works fine. Once I add it, the divider is invisible until the scroll.
Seems like just a bug.
Recycler View Divider Programmatically
mRv.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
Try this above code snippet replace with LinearLayout.Vertical & let me know.
Can you show your item layout that you are inflating in the Adapter?
I think your item layout has an android:orientation = "horizontal" & you want a vertical divider. It's just an assumption as there is a very few insights I'm getting from the Question.

HorizontalScrollView's child not matching it's parent view's width even on setting android:layout_width="match_parent"

I want to display a horizontal list of scrollable buttons at top of the phone screen containing 30 items, for this purpose I am using a HorizontalScrollView with a LinearLayout with "horizontal" orientation as it's child but the linear layout is not taking up the entire phone width even on setting its width as "match-parent". Here's the code :
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<GridView
android:id="#+id/gridView_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchMode="columnWidth" />
</LinearLayout>
</HorizontalScrollView>
Here is shown in the image that linear layout is not taking up the entire space and LinearLayout is only covering some of the space. Also on changing the size to a fixed size, I noticed that the HorizontalScrollView was actually behaving like a vertical Scroll View only.
NOTE: Also if there is an alternative way to display a horizontal list of buttons with 30 items with numbers from 1 to 30 on it, please suggest it.
To achive this easily you may use Recyclerview with Horizotanl Layout Manager.For Example
recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
I don't think you need a HorizontalScrollView with a nested LinearLayout for what you are trying to achieve. It would be a better idea to simply use a horizontal RecyclerView if your button layouts are similar and the functionality of the buttons is similar too. Refer to this answer for help https://stackoverflow.com/a/40584425/9119277

FrameLayout with two children( Some View and a ProgressBar, with only one visible at any moment). How to set LayoutParams properly

If I want to show a View with content which takes time to fetch, I'd usually include it in a FrameLayout with a ProgressBar alongside. Show the progressBar while fetching and show the view once the content become available.
Example with a textView.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true">
<TextView
android:id="#+id/addressTv"
android:layout_width="match_parent"
android:minLines="4"
android:maxLines="4"
android:layout_height="match_parent"/>
<ProgressBar
android:id="#+id/addressLoadingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true" />
</FrameLayout>
In this example only one of 'addressTv' or 'addressLoadingBar' is showing at any given moment, the other one is 'GONE'.
But I can't figure out how to make the FrameLayout have a fixed height. In this case the max of the children's heights. With the above layout, it changes when we switch between the children.
I suggest switching to RelativeLayout, with both children being visible when the layout is inflated. Then set the one not needed to invisible, and change once it is ready. Then make the progress bar invisible.
You have a circular dependency problem - your FrameLayout height is wrap_content while your TextView height is match_parent. You may want to change your TextView height to wrap_content
As #Disco mentioned in the comments, using View.INVISIBLE instead of View.GONE will probaly fix problem. When view is "gone", it just disappear from the view hierarchy, while when it is "invisible", it cannot being seen, but is still there logically, so layout dependent upon the invisible view are not effected by it's "disappearance".

RecyclerView with GridLayoutManager - layouting items

I have a simple layout with a recycler. I feed it with Adapter and set GridLayoutManager for the recycler.
<android.support.v7.widget.RecyclerView
android:id="#+id/scripts_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:verticalSpacing="20dp"
android:clipToPadding="false"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
tools:listitem="#layout/tmpl_script"
android:paddingBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingTop="30dp"
app:layout_gravity="center" />
Activity
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.scripts_recycler_view);
recyclerView.setAdapter(adapter);
int columns = getResources().getInteger(R.integer.scripts_columns);
recyclerView.setLayoutManager(new GridLayoutManager(this, columns));
recyclerView.setHasFixedSize(true);
Item
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/game_border"
android:gravity="center"
android:orientation="vertical">
I have two problems:
there is no space between rows
items are not centered within the row
I tried all properties in Android Studio but they do not have any effect. I googled, opened many questions but I still have no luck. What magic property shall I use?
As you can see items are positioned on left side of the screen. I want to move it to center. And there is no space between first and second row.
The easiest solution for (1) is to add top and/or bottom margin, like #suraj said.
Regarding (2) - the critical issue is that your items are square. To get it working, set android:layout_width="match_parent" and change the design of your items to use the actual smaller dimension (width or height) for both dimensions (maybe all you need to do in your case is to make the background square). Then display the content centered within the item's LinearLayout (you are already doing this). You have to do it this way because GridLayoutManager does not support layout_gravity at this time.
For 1.there is no space between rows
Try android:layout_marginBottom="10dp" in the linear layout of item.This should provide the spacing below each item.
For 2.items are not centered within the row
Looks like you have given right margin to your recyclerview and no left margin.
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
should give equal spacing on both sides.
or set
app:layout_gravity="center"
in linear layout of the item.

View invisibility when RecyclerView is empty

I am using a LinearLayout with some views inside. The second last is a RecyclerView, and the last one is an <include> tag linked with a RelativeLayout. I want the last view to be visible permanently, as I want to use it to add items to the RecyclerView.
My problem is that the <include> view below the RecyclerView disappears whenever the adapter is empty. Is there any way to avoid this behaviour?
EDIT
There is a constraint in the way I want the RecyclerView and the "add" View to work together. I would like that it feels as the "add" view is always the last item in the RecyclerView, as it happens in Google Keep lists.
Example Google Keep list example (I cannot add images yet)
You could try to replace your LinearLayout with a RelativeLayout; then you anchor the bottom view to the bottom of the screen, and the RecyclerView would be set above that view. Something like this (just a skeleton):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent>
<-! other views here -->
<include layout="#layout/something"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#bottomView" />
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/some_view_above_recyclerview"
android:layout_above="#+id/bottomView" />
</RelativeLayout>

Categories

Resources