Android: RecyclerView Scrolling Issue - android

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

Related

How to place a view and a recyclerView in the same screen

I am trying to put recyclerView with a screen (Fragment) that has textView.
The .xml file of the fragment i'm trying to add to:
<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"
tools:context=".ui.ways.WaysFragment">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/ways_quote"
android:layout_marginTop="#dimen/card_margin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textSize="18sp"
android:layout_marginBottom="10dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:listitem="#layout/custom_ways_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
I will try to describe the appearance (result) of what I tried to do with code and words, rather than pictures, I think it will be easily understood that way.
At first I added textView and just put the recyclerView under it.
And the result(appearance on my test device) I got look like:
Layout:
....
TextView:
....
ScrollView:
....
and the contents of my recyclerView
By this I mean, the textView doesn't scroll but the recyclerView does.
Next I put the textView and recyclerView inside the a single scrollView. But the textView scrolls seperately from the recyclerView, as if textView and recyclerView were in different scrollView.
the result(appearance on my test device) I got look like:
Layout:
....
ScrollView:
....
TextView:
....
ScrollView:
....
and the contents of my recyclerView
And my desired Appearances, is:
Layout:
....
ScrollView:
....
TextView:
....
Contents of my recyclerView:
....
I have discovered I named this question wrong it should have been "how to put header in a recyclerview using kotlin".
To do I created a new layout for the header, and an adapter.
Then I used ConcatAdapter To add the adapter of the header and recyclerview together.
I followed this tutorial on medium.

How to hide Particular view in RecyclerView?

I have to hide Particular views in recyclerview, I am trying to hide some views but view has gone but space is there,I have tried putting heights=0 in cardview but still did not work , what I should I do?
This is my XML code list data
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-
auto">
<android.support.v7.widget.CardView
android:id="#+id/child_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2sp"
app:cardElevation="12sp"
android:visibility="gone">
<LinearLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:orientation="vertical"
android:padding="3dp"
android:visibility="visible">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="#drawable/bsp_icon" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/label_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:padding="0dp"
android:textColor="#color/Black" />
</LinearLayout>
</android.support.v7.widget.CardView>
</layout>
and this is my recyclerview
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
tools:listitem="#layout/list_layout_dashboard_child_view">
</android.support.v7.widget.RecyclerView>
this is my Adapter code
if (txnTypeMap.get("CUST_CR") != null &&
obj.getChildName().equalsIgnoreCase(txnTypeMap.get("CUST_CR"))) {
binding.childCardView.setVisibility(View.VISIBLE);
}
important always set this if you are sure your data will change in recyclerview mRecyclerView.setHasFixedSize(false);
first remove the item from list e.g. list.remove(position which you want to hide)
next call notifyItemChanged(postion) and it should work ->this is better approach than calling notifyDataSetChanged()
You should hide all views or parent from UsersViewholder layout xml. You should hide entire viewholder or each view
Entire viewholder:
itemView.setVisibility(View.GONE);
or each element:
view.setVisibility(View.GONE);
But don't forget to set them VISIBLE otherwise, you will end up with some strange things from recycling
Without setting setVisibility to View.GONE, try to remove the the data from the data source and call notifyDataSetChanged()
This is the more efficient way to hide a view from RecyclerView
you have to set visibility gone to view in else case because views are reused in recycler view
if (txnTypeMap.get("CUST_CR") != null &&
obj.getChildName().equalsIgnoreCase(txnTypeMap.get("CUST_CR"))) {
binding.childCardView.setVisibility(View.VISIBLE);
} else binding.childCardView.setVisibility(View.GONE);
I hope it will work.

How to force recyclerview to have dynamic height changing items?

Actually I'm trying to use recyclerview as a viewpager which is working fine by the help of PagerSnapHelper() class . But now the problem is how can I force recyclerview to change item heights dynamically and not show white space . Now when I try to load drawable pictures of two different sizes i.e a small and a bigger at first the smaller one is having correct size but when I scroll to next and then first some white space is shown below the imageView.You can see aforementioned problems in the given pictures.
XML:
<android.support.v7.widget.RecyclerView
android:id="#+id/rView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Recyclerview Items:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
Adapter Code:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(ctx).load(pics.get(position)).into(holder.imageView);
}
Activity Code:
rView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL,false));
rView.setAdapter(adp);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rView);
Change your Recyclerview Items with this code by adding scaleType in your image view
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</FrameLayout>
you can change image view width and height like below code..
<ImageView
android:id="#+id/recyclerImgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and recycler view change height like below code..
<android.support.v7.widget.RecyclerView
android:id="#+id/rView"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_100sdp"
/>
add this below dependency into app level gradle file and give size is same all the device like below ..
implementation 'com.intuit.sdp:sdp-android:1.0.4'

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.

ListView only showing first result - only in mobile device : MasterDetail View

I have this problem in my detail view in my Master/Detail Template based app.
im populating expandable list in detail view. it is working fine in tablet view. but in mobile device it is only showing me first result only. i even tried removing CollapsingToolbarLayout to make sure listview not getting block by any other scroll options. but i couldn't find the problem.
Setting up adapter
ArrayList<ClientList> clientLists = ClientList.getClientList(getActivity(), clientResult);
Log.e("Client","--"+clientLists.size()); //Im getting the list size as normal
clientExpandableAdapter = new ClientExpandableAdapter(getActivity(), clientLists);
expandableListView.setAdapter(clientExpandableAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
return false;
}
});
Detail view layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxx.xxx.ItemDetailFragment"
android:textIsSelectable="true"
android:paddingBottom="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#drawable/bottom_border"
android:id="#+id/mainViewHeader">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="#+id/titleHeader"
android:text="Clients" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/titleCountHeader"
android:text="Clients"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mainViewHeader"
android:id="#+id/backgroundView"
android:background="#color/colorWhite">
<ExpandableListView
android:id="#+id/mainExpandableDatalist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
In this type of issues many mistakes are possible according to me.
You might have taken ScrollView as parent of listview which is not at all required.
Height of ListView or custom ListView layout's root element could be match_parent | fill_parent that shows single item in whole screen.
It will be better if you show layout files too.

Categories

Resources