Custom Grid Layout for RecyclerView Android - android

I needed a Grid Layout build like this.
I have create RecyclerView and set GridLayoutManager
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgPost"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
But it generates empty space. Please help me achieve this.

What you're looking for is a StaggeredGridLayout
Try this -
StaggeredGridLayoutManager gridLayoutManager;
gridLayoutManager = new StaggeredGridLayoutManager(3, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
Hope this helps!

Related

How to scroll whole activity

I wrote an application and home page is LinearLayout and is in Fragment. In this LinearLayout there are two RecyclerViews and SearchBar etc. I want to scroll whole activity. I spent 2 days for it but cannot succeed. How can I do that in easy way? There are lots of adapters and connections in that LinearLayout. How can I achieve that without broke any code.
I want to scroll whole activity.
Thanks in advance.
Set Scroll/NestedScrollView as a parent view in xml to scroll whole layout.
Add below attribute in recycler view to stop recycler scroll:
android:overScrollMode="never"
Try below code:
<android.support.v4.widget.NestedScrollView
android:id="#+id/navigation_nested"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/margin_15">
<TextView
android:id="#+id/video_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_15"
android:drawableLeft="#drawable/ic_video_tutorial"
android:drawablePadding="#dimen/margin_20"
android:fontFamily="#font/poppins"
android:paddingLeft="#dimen/text_15"
android:text="#string/videos"
android:textColor="#color/blackTextColor"
android:textSize="#dimen/text_15" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
You just add this line:
//java
yourRecyclerView.setLayouManager(new LinearLayoutManager(this));
//kotlin
yourRecyclerView.layoutManager = LinearLayoutManager(this)
or from xml:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Notice that there is not only LinearLayoutManager. If you are using a Grid RecyclerView than you might want to use GridLayoutManager
I solved the problem.
Just write ScrollView over the LinearLayout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
.....
</LinearLayout>
</ScrollView>

Horizontal Recyclerview not scrolling inside FramLayout or CoordinatorLayout

<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/carousal_parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
and in Inner FrameLayout i am adding a Horizontal Recyclerview with fixed Size .
carousel_parent_layout = findViewById(R.id.carousal_parent_layout);
carouselPager = view.findViewById(R.id.carouselPager);
carouselPagerAdapter = new CarouselPagerAdapter(mContext, carouselData, carouselPager);
carouselPager.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
carouselPager.setHasFixedSize(true);
carousel_parent_layout.addView(carouselPager);
But on touching Recyclerview it is not flinging . I have tried with SnapHelper and android:descendantFocusability="blocksDescendants" but nothing is working. Any help is highly appreciated. Thanks a lot in advance for reading the question.

GridView - Images,Text and ItemClick

my friends
I am a new in android and I wanted to do an example (code) like the picture
GridView - Images,Text and ItemClick
Thanks to all
You are looking for RecyclerView. Create a layout and add recyclerView to it then create another layout file for the rows of the recyclerview as below:
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Create another layout row_layout.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</RelativeLayout>
Then in your MainActivity, add following lines of code:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2, false);
recyclerView.setLayoutManager(gridLayoutManager);
After this, create an Adapter according to your specific need for the recyclerView which can be found with a quick google search. Just remmeber to infalte your row_layout in the adapter's onCreateViewHolder. After creating the adapter just set it to your recyclerView.
recyclerView.setAdapter(your_custom_adapter);
This is just a gist of what you ought to do. It'll get you started in the right direction. The rest is just a search away.

How to properly display a grid in RecyclerView?

I'm developing an app that displays popular movies right now. It uses TMDB API to fetch this data. I'm using RecyclerView which displays clickable ImageViews in a grid consisting 2 columns.
This is the result I want to achieve:
an edge to edge grid of all movies that I can only achieve by hard coding values:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">`
<ImageView
android:layout_width="185dp" <!--HARDCODED VALUES-->
android:layout_height="278dp"<!--HARDCODED VALUES-->
android:contentDescription="#string/movie"
android:id="#+id/rv_image_view" />
</LinearLayout>
If I use layout_width="match_parent" or layout_height="wrap_content" I get extremely weird and skewed results. Like this one. How should I fix this?
Please don't mark this as duplicate. I've searched far and wide for this and came up with absolutely nothing.
try this create a layout file like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/movies_item"
android:layout_width="185dp"
android:layout_height="278dp"
android:scaleType="fitXY"/>
</LinearLayout>
In activity :
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(layoutManager);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp">
<ImageView
android:scaleType="fitXY"
android:id="#+id/movies_item"
android:layout_width="match_parent"
android:layout_height="180dp"/>
</LinearLayout>

RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount size

When I put RecyclerView inside NestedScrollView then onBindViewHolder is calling for all row like say I have list which has size of 30 then onBindViewHolder is called for all 30 rows at one time even without scrolling
RecyclerView list;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
list.setLayoutManager(layoutManager);
layoutManager.setAutoMeasureEnabled(true);
list.setNestedScrollingEnabled(false);
list.addItemDecoration(new VerticalSpaceItemDecoration(5));
list.setAdapter(adapter);
my xml is
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_views"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/info"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAlignment="center"
android:visibility="visible"
/>
but if I remove NestedScrollView it's working properly.
I'm going to assume that since your are using appbar_scrolling_view_behavior you are trying to do something with AppBarLayout.
If so, you can use RecyclerView as a direct child of CoordinatorLayout and have support for AppBarLayout scrolling without nesting RecyclerView inside of NestedScrollView.
Try this: RecyclerView inside CoordinatorLayout (with AppBarLayout and CollapsingToolbarLayout):
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#55FF00FF"
app:layout_collapseMode="none"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
And in your Activity or CustomView:
RecyclerView list;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
list.setLayoutManager(layoutManager);
list.addItemDecoration(new VerticalSpaceItemDecoration(5));
list.setAdapter(adapter);
But you set android:layout_height for NestedScrollView to wrap_content - here, it's zero by default (because there no content for him at the moment of the declaration). Next, for RecyclerView you set android:layout_height to match_parent - which is at the moment is 0. Thus, all your items have 0 height.
Thus, you have such situation.
Solution: use solution above from #dkarmazi https://stackoverflow.com/a/37558761/3546306 or try to change parameter android:layout_height values.
It's right.Because you are using a ScrollView.ScrollView is not recyclable like RecyclerView or ListView.It will show all view contains these out of screen in one time.You should use a other layout instead.
I faced the same issue. After a bit of research fount the solution.
You need to make sure your recyclerview height is fixed by setting it to MATCH_PARENT. Or if its in a contraint layout then set height to 0dp and set the required height constrains.
Then set recyclerview.setHasFixedSize to true.
The onBindViewHolder will start getting called after this.

Categories

Resources