I want to show multiple itemview at once like this
I am using recycler view 23.2.1
compile 'com.android.support:recyclerview-v7:23.2.1'
My recycler view xml
<android.support.v7.widget.RecyclerView
android:id="#+id/gallary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
Corresponding Java code
mRecyclerView = (RecyclerView) findViewById(R.id.gallary);
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
With this configuration I getting only one item at once. Like image below
It seems like wrap_content in recycler view is not working as the space between images are there. Is there anyway to remove the space between the items.
I found one bug related to wrap_contect in recycler view link which is fixed. Not sure if this is causing the problem. Any help to fix this will be appreciated
My row view:
<?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="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/facebook" />
<TextView
android:id="#+id/titletv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:text="Testing"
android:textSize="#dimen/fourteen_sp" />
<ProgressBar
android:id="#+id/mainimgloading"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:layout_gravity="center" />
</RelativeLayout>
</LinearLayout>
Your parent LinearLayout for your row cannot be match_parent you must use wrap_content for your width or else you will get what you see there.
if you had a vertical recyclerview then you would be using wrap_content for your height and not your width
Related
I am using the RecyclerView to inflate a Linear Vertical listViews, However, although the width of a child is set to match_parent, RecyclerView wraps that at runtime.
Here is a screenshot of the problem:
item_recyclerView
<?xml version="1.0" encoding="utf-8"?>
<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="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="#+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="#drawable/ic_user"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
When inflating a layout file, the layout_ attributes on the root view will be ignored unless you specify a parent ViewGroup in the inflate() call. You are not currently doing so.
In your onCreateViewHolder() method, replace this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
with this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
Note also that you probably don't want to be using match_parent for the height of your RecyclerView's items:
android:layout_width="match_parent"
android:layout_height="match_parent"
This will give the appearance that only a single item is visible in your RecyclerView, since each item will be the full height of the screen. This is currently not a problem for the exact same reason that the match_parent width is being ignored.
give for your parent linearlayout weightSum 5
give for imageView weight 1 and do his width 0
and for child linearlayout weight 4
enter code here
I have a very simple RecyclerView that loads data from a Firestore database. There are no issues with the code to get the data but the RecyclerView does not show anything when I set the view holder to wrap_content. However it does load the data when I set it to match_parent. I have tried using
firestoreRecycler.setHasFixedSize(true)
but it doesnt do anything, the data is just not shown at all when I use wrap_content and using match_parent makes it so that i have to scroll a lot to see a single item.
Here is the code for the RecyclerView row layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:padding="15dp">
<TextView
android:id="#+id/listOfficeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/listOfficeAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/listOfficeName" />
</androidx.constraintlayout.widget.ConstraintLayout>
There is nothing relevant about the rest, there is functionality only for the RecyclerView that I got from a tutorial in which wrap_content actually worked.
Thanks in advance!
The distance of the recyclerview used in ConstraintLayout as a view is disabled because it does not fit the logic of ConstraintLayout. So edit the height value of recyclerview as follows:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp" />
height = 0dp
I have such layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".jobAGENT.JobsList">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="75dp"
android:gravity="center_horizontal"
android:text="#string/no_jobs"
android:textSize="32sp"
android:textStyle="italic"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refresh_t"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:background="#color/white">
<android.support.v7.widget.RecyclerView
android:id="#+id/job_list_t"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:id="#+id/loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="gone" />
</RelativeLayout>
I would like to change margin of RecyclerView and SwipeRefreshLayout programmatically when I reach some conditions. For example, at my onScrollListener when my list reaches its end I would like to change bottom margin of my views and show progressbar below it. I saw this and this questions and I have also managed to change margin of refreshLayout but the last item of RV become cut and I think that I have to change margin of RV also. But I can't get layout params of this view. For refreshLayout I used smth like that:
val param = refreshLayout.layoutParams as RelativeLayout.LayoutParams
param.setMargins(5,10,5,150)
refreshLayout.layoutParams = param
and it works good, but how I can also change margin of recyclerview which is placed inside refresh layout?
Last item in your RecyclerView cuts, meaning, you'll have to add padding only to the last element of RecyclerView, instead of giving padding to complete RecyclerView.
You can do that by setting android:clipToPadding="false", and giving it paddingBottom equal to the height of your SwipeRefreshLayout.
You can do that as follows:
<android.support.v7.widget.RecyclerView
android:paddingBottom="56dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Adjust the paddingBottom as per your need.
A suggestion, instead add the progress bar as an item of the recyclerview at the last position when you reach the scroll end... this will ensure that the bar is displayed.. It is easy enough to have multiple types of viewholders in a recyclerview and this should solve your issue without needing to add any margin/padding
try this..
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.MATCH_PARENT
);
params.setMargins(50, 50, 50, 50);
jobListT.setLayoutParams(params);
I have a
1. Framelayout(frame1) in which I am inflating a RecyclerView layout with weight 0.5
2. Framelayout(frame2) with visibility gone and weight 0.5
My Recyclerview is occupying only half the width though frame2 is gone. If I use listview instead of RecyclerView, I dont have this issue. Can anyone suggest how to make Recyclerview occupy whole width ?
EDIT:
If I scroll the recyclerview , it is taking up the whole width as expected.
Adapter
#Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
ListViewHolder tvh = new ListViewHolder (v);
return tvh;
}
main_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="0dp" // If I put match_parent here it works
android:layout_height="match_parent"
android:layout_weight="0.5"
/>
<FrameLayout
android:id="#+id/frame2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="gone" />
</LinearLayout>
frame1_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
recycler_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="5dp" >
<ImageView
android:id="#+id/imageIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_main" />
<TextView
android:id="#+id/textName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_toRightOf="#+id/imageIcon"
android:textSize="14sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Test"
android:layout_below="#+id/textName"
android:textSize="12sp" />
</RelativeLayout>
Solved the issue by following this SO post . Any better solution will be appreciated.
Recyclerview with width 0dp
Solution is to basically call
recyclerView.setAdapter(adapter);
/* Make sure you call all the below stuff after you set the adapter or
in your loadFinished() incase you are using loading data using Loaders*/
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setAutoMeasureEnabled(false); // This is the key
recyclerView.setLayoutManager(llm);
Since you have two frame layouts with each having an equal weight, they will each occupy half the screen. Even if the visibility of the second frame layout is gone.
What do you need the second frame layout for? When are you planning to make it visible? And what do you want the first recycler view to look like when the second one is also visible?
hello I'm trying to use Recyclerview,
below code results only a blank screen, can you please tell me where I'm going wrong
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top|center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/ImageSelectorRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
<ImageView
android:id="#+id/ImageViewSelectPhoto"
android:layout_width="80dp"
android:layout_height="76dp"
android:src="#drawable/ic_add_a_photo_black_361px"
android:background="#drawable/back"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
Since you have provided
android:layout_width="match_parent"
android:layout_height="match_parent"
for your RecyclerView, its taking the full width and height of your device or parent view. And the ImageView is outside the device screen.
Now you have just declared the RecyclerView in your XML. You need a RecyclerView Row Layout which should define the layout for the RecyclerView items.
To show something in the RecyclerView you can set static data in the Row Layout layout iteself or need to create a RecyclerView adapter and model for dynamic/static data.
You need to make a list item layout for your Horizontal-scrollview and a seperate adapter also for it.
Try adding android:viewfillport="true"
I too asked a similar type of question ,please have a look to it yu will surely get a simple way to implement this .
Horizontal Listview Not Working from github
I think it should be similar to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top|center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/ImageSelectorRecycler"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/ImageViewSelectPhoto"
android:layout_width="80dp"
android:layout_height="76dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:src="#android:drawable/ic_dialog_alert"
android:background="#android:color/holo_green_dark" />
</LinearLayout>