GridView - Images,Text and ItemClick - android

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.

Related

android RecyclerView inside a RecyclerView

I have a RecyclerView and inside I have a RecyclerView. and child RecyclerView is no scrolling I try put a child RecyclerView inside a scrollView NeastedCrollView but it doesn't work :
No I have this parent recyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/vehicle_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="#dimen/text_dp_20"
android:paddingTop="10dp"
android:scrollbars="none"
tools:ignore="MissingConstraints" />
and this is a child list :
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="none" />
</androidx.core.widget.NestedScrollView>
First of all, don't use NestedScrollView, RecyclerView handles everything about scrolling!
use the below links guidelines for improving and optimizing your RecyclerViews, here some useful references:
https://www.geeksforgeeks.org/how-to-create-a-nested-recyclerview-in-android/
https://android.jlelse.eu/easily-adding-nested-recycler-view-in-android-a7e9f7f04047
follow one of them step by step!
you're first need in one recylerView in main activity
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
2-and in MainActivity.java :
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
ItemAdapter itemAdapter = new ItemAdapter(buildItemList());
rvItem.setAdapter(itemAdapter);
rvItem.setLayoutManager(layoutManager);
3- recyclerView in Layout Adapter:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_sub_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
4- in Adapter :
LinearLayoutManager layoutManager = new LinearLayoutManager(
itemViewHolder.rvSubItem.getContext(),
LinearLayoutManager.VERTICAL,
false
);
layoutManager.setInitialPrefetchItemCount(item.getSubItemList().size());
// Create sub item view adapter
SubItemAdapter subItemAdapter = new SubItemAdapter(item.getSubItemList());
itemViewHolder.rvSubItem.setLayoutManager(layoutManager);
itemViewHolder.rvSubItem.setAdapter(subItemAdapter);
itemViewHolder.rvSubItem.setRecycledViewPool(viewPool);
the end.
you see full code in link

How to create horizontal recycle view with only text?

I'm trying to make a horizontal recyclerview in my Android application so I see a of tutorials to make it, its very complicated because I don't want to add images or on click listeners I just have a card and textView inside it and I want to add an id to every single item I have some background about that below
This is my activity of recyclerview items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="43dp"
app:cardBackgroundColor="#000"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<TextView
android:id="#+id/horizontal_data_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Test"
android:textColor="#38ef7d"
android:textSize="20sp"
android:textStyle="bold"
android:paddingStart="18dp"
android:paddingEnd="18dp"
tools:ignore="HardcodedText"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>
And I created a class named RecyclerViewAdapter.
I will add this RecyclerView to a named activity called SettingsActivity.java
Any ideas?
Assuming you use LinearLayoutManager in your RecyclerView, then you can pass true as to third argument in the LinearLayoutManager constructor.
For example:
mRecyclerView.setLayoutManager(
LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
true
)
)
change RelativeLayout width and height
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and Remove this line android:orientation="vertical"
and
mRecyclerView.setLayoutManager(
LinearLayoutManager(
this,
LinearLayoutManager.HORIZONTAL,
true
)
)
For horizontal scrolling in recycler view set the layout manager for recyclerview as:
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
Hope this should help.

Custom Grid Layout for RecyclerView 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!

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>

Android: RecyclerView Scrolling Issue

I have a fragment which includes a Textview, and a RecyclerView (list)
My problem is when scrolling the RecyclerView, the textview above it stays on top and doesn't get scrolled.
How do I make the textview to be non-sticky and scrollable like in a ScrollView?
My 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"
tools:context="com.easyuni.courserecommender.ResultsCareerTabFragment">
<TextView
android:id="#+id/tv_results_rec_careers_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:text="#string/results_rec_careers_title"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_careers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_results_rec_careers_title"
android:layout_gravity="center"
android:layout_marginTop="20dp"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
Screenshots:
Normal look:
When scrolling down in RecyclerView:
Thanks.
You will need to add your textview as an item that feeds your adapter for your recyclerview. You can use the following code to distinguish between the view that you want to display in your recyclerview:
public int getItemViewType (int position)
Please have a look at this thread:
How to create RecyclerView with multiple view type?
You can fix it with this:
Message message = messageList.get(position);
viewHolder.setIsRecyclable(false);
you can lean on this link:
http://www.homebrewandtechnology.com/blog/recyclevewadaptershowwrongdatawhenscrolling

Categories

Resources