I have a recyclerview with 3 rows maximum. This RV located on the top of my view. Below located container for my fragment. And I want to make only last element of my rv visible, when I will scroll content inside my fragment and show all elemenets of rv when I will on the top of my fragment content. How to do this?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"/>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
First, you have to use nestedscrollview in your layout for proper scrolling of recyclerView and other views in your parent layout.
The second thing you can do is send only the last element of your list to the adapter.
like ,
ArrayList<Test> tempList=new ArrayList();
tempList.add(customList.get(customList.size()-1);
where customList is the Type of class data which are you sending as an argument to your adapter.
this way you can do your work.
Related
Here is a screenshot of my app.
The circled area is a RecyclerView with a TouchHelper so you can swipe left to delete items in the list.
Each item in the recycler list is a CardView that contains a linear layout and the text is a simple TextView.
I want to be able to scroll the text within each of the TextViews as well as be able to swipe to delete, but only swipe to delete is working.
What do I need to do to enable scrolling of these TextViews, which have more text than can fit in the 4 lines?
Here's some of the relevant code and layout.
// Initialize comment list adapter
commentAdapter = CommentListAdapter(this)
binding.comments.adapter = commentAdapter
binding.comments.layoutManager = LinearLayoutManager(this)
binding.comments.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
val itemTouchHelper = ItemTouchHelper(SwipeToDeleteCallback(commentAdapter, (ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)))
itemTouchHelper.attachToRecyclerView(binding.comments)
Here's the XML for each of the "comment" TextViews. Note how I'm setting a vertical scrollbar, as most of the Internet recommends. And I've limited the number of lines to 4 in order to keep each individual item smaller.
My limiting of the number of lines to 4 in the TextView is perhaps not what the system expects, since normally the entirety of the text would be displayed within each RecyclerView item. And indeed it works that way if I remove the maxLines limitation.
<TextView
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:maxLines="4"
android:text="#string/comment_text" />
Wrap the TextView inside a ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:maxLines="4"
android:text="#string/comment_text" />
</LinearLayout>
</ScrollView>
I'm trying to implement nested RecyclerView. I did that but parent RecyclerView scroll was not smooth on scroll. I did alot of optimization, but still didn't manage to be successful with scroll till I though of putting parent recyclerview inside NestedScrollView. Scroll is flawless now, but I've a problem.
If I scroll (even tiny bit) my inner RecyclerView (horizontal), I immediately get back to the start [of vertical recyclerview - parent].
This happens one time. Why is that and is it possible to fix it?
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/parent_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
As far as I have understood your problem, you parent RecyclerView gets updated and scrolls to position 0 when the inner horizontal RecyclerView items get updated. You have also stated that the horizontal RecyclerView stays in the same place update the items are updated. In this case, you need to save the state of the parent RecyclerView and then put it back where it was after the items in your horizontal RecyclerView get updated.
// Save state
private Parcelable recyclerViewState;
recyclerViewState = parentRecyclerView.getLayoutManager().onSaveInstanceState();
// Restore state
parentRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Save the state before you call the API for updating the data in your horizontal RecyclerView and then restore the position of the parent RecyclerView when the update finishes.
I got a solution.
Basically, what I did was wrap recycler view inside of relative layout and set android:descendantFocusability="blocksDescendants" and now it works good.
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="#+id/parent_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
I want to implement two RecyclerView with different layout in single activity. The above RecyclerView should scroll vertical and the one below should scroll horizontal. But when I run the app, only either one RecyclerView is displayed. If first view is displayed then it works properly and scrolls vertical, while second RecyclerView is missing. And if second one is displayed then it scrolls vertical when it should do horizontal scroll and the first RecyclerView is missing.
Here is what i want. Source: Github,CardView-Recyclerview-Picasso
Here is my layout
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundLight"
android:smoothScrollbar="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/CategoriesRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/videoRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:layout_below="#+id/CategoriesRecyclerView"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
You may directly use the 2 Recycler Views without NestedScrollView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/CategoriesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/videoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="130dp"
android:layout_below="#+id/CategoriesRecyclerView"/>
</RelativeLayout>
And in your CategoriesRecyclerView whose height is wrap_content, use setAutoMeasureEnabled(true) on the Layout manager used for the recyler view.
If you want to scroll the horizontal scrollview full upside on page scroll then use scrollview otherwise you can do without scrollview. Also to achieve your layout just give the horizontal recyclerview fixed height and then you can see both recyclerview .
Im trying to rig a horizontally oriented RecyclerView with child items that contain a HorizontalScrollView. What I have configured right now is not allowing the nested HorizontalScrollView to scroll its content.
Heres an abbreviated version of my activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/home_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
And here's an abbreviated version of the item layout:
<android.support.v7.widget.CardView
android:id="#+id/main_card"
android:layout_width="335dp"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
</android.support.v7.widget.CardView
So Im trying to get the Text content inside the Horizontal ScrollView to be scrollable.
EDIT :
Below is a screenshot of the textview that will not scroll. As you can see, the text is cut off at the end of the view, and this is why Im looking to implement horizontal scrolling.
NOTE: I tried changing the LayoutManager associated with my RecyclerView to have vertical orientation, and horizontal scrolling works as expected. Knowing this, it seems that the RecyclerView is absorbing all scroll events and not allowing the nested HorizontalScrollView to do its thing
I have a RecyclerView matching the height of the parent layout.
I need to have an item there to function as a footer. Doing this is not the problem when there are a bunch of items in the list.
The problem is to get it to stick to the bottom when there are a few items and it's not enough to enable the scroll. So, if I have, say 1 item plus footer item, how can I display this item at the bottom of the RecyclerView?
Try using android:weight property. Or if you're using RelativeLayout align the footer to the bottom of the parent view
try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/places_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<YourView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
YourView is the view where you will actually add the view for your funcionality. Even if there is no items or there is, it will always be on bottom.