Access other view with recycler view android studio - java

As described in below image. I have one layout above the recycler view and one below. both tags are the linear layout. I face two problems.
because the recycler view has inner scroll therefore if items in recycler view increase, the first linear layout stays at the same place but I am expecting it to scroll up while scrolling items.
When items increase in recycler view the second layout get disappear. how to make the layout scroll when recycler view reaches its last item.
Tried android:nestedScrollingEnabled="false" it disables the scroll effect but shows only a few items in the recycler view.
Require solutions: if somehow I can make the recycler views items static and display all the available contents. so the parent layout decides whether scroll or not. if size increase it will scroll otherwise not.

You can add both LinearLayouts to the RecyclerView as header and footer views. The top answer in this question should give you an idea how to make this: RecyclerView header and footer

If your two LinearLayout have fixed size, you can fix them inside a RelativeLayout and then insert your RecyclerView "in between".
<RelativeLayout>
<LinearLayout alignParentTop="true" height="100dp" id="#+id/topLayout"/>
<LinearLayout alignParentBottom="true" height="100dp" id="#+id/bottomLayout"/>
<RecyclerView height="match_parent" above="#id/bottomLayout" below="#id/topLayout"/>
</RelativeLayout>
Alternatively, if your top container is a LinearLayout, you can play with weights (1 for top LinearLayout, 1 for bottom LinearLayout, 3 for RecyclerView)
<LinearLayout orientation="vertical">
<LinearLayout height="0dp" weight="1"/>
<RecyclerView height="0dp" weight="3"/>
<LinearLayout height="0dp" weight="1"/>
</LinearLayout>

Related

Android RecyclerView item dynamic layout

Inside my recycler view item layout I want to add something like horizontal recyclerview layout, because I don't know how many ImageViews I need, that's why I can't create them as a static layout. The number of icons depends on server logic. How can achieve something like the picutre below ?
Thank you in advance!
You Need Add Horizontal Recyclerview Inside The Main Recyclerview.
Your First RecyclerView is Verticle list and inside every index create another RecyclerView It's a Horizontal. so you can display n number of Imageview.
You have to create Recyclerview inside the child layout of your main Recyclerview
Something like
Suppose this is the Main Recyclerview
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:listitem="#layout/item_demo" />
In your child layout, Let's say item_demo you have to add another Recyclerview for the imageView that you want to show in horizontal
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:listitem="#layout/item_child" />
item_child layout contains the Imageview that will display horizontally

Recycler View loading very slow for large data when inside NestedScrollView

I have added RecyclerView inside my NestedScrollView. Basically I want RecyclerView to scroll with other Views. The problem that I am facing is that for a small set of data, it is working fine, but for a large set of data(200 entries) whenever I launch the activity, it freezes for about about 3-5 seconds and then loads. I removed the NestedScrollView and it is working flawlessly, but it doesn't provide me the behaviour I want.
(For extra info, I am loading the data from SQLite database. There is no problem in scrolling, as it is smooth. The only problem is the activity is freezing for a while)
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<... Some other Views ...>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This case of RecyclerView inside NestedScrollView.
RecyclerView is calling onCreateViewHolder() times equal to your data size.
If data has 200 items, it freezes for onCreateViewHolder() to be called 200 times.
The problem as said above is because RecyclerView as a child or subChild in NestedScrollView measures its height as infinitive when you use WRAP_CONTENT or MATCH_PARENT for height of RecyclerView.
one solution that solved this problem for me was setting the RecyclerView Height to a fixed size. you could set height to a dp value, or you could set it to a pixel value matching devices height if your requirements needs a vertical infinitive RecyclerView.
here is a snippet for setting the recyclerView size in kotlin
val params = recyclerView.layoutParams
params.apply {
width = context.resources.displayMetrics.widthPixels
height = context.resources.displayMetrics.heightPixels
}
recyclerView.layoutParams = params
I faced the same problem!
The solution was to change NestedScrollView to SwipeRefreshLayout.
add this for enable/disable ToolBar Scrolling:
ViewCompat.setNestedScrollingEnabled(recyclerView, true);
As said by Nancy , recyclerview.setNestedScrollingEnabled(false); will solve scroll stuck issue. i too faced this type of issue and solved by false the NestedScroll.

How to show a left colored border on each ListView row?

How to show only a left colored border on each ListView row?
I have a 5 degree termometer for user reputation and I want to show the respective colors at the left border of each listview row.
Like this link
Thanks guys!!
The easiest way would be to create a dummy View in your item layout. That way you can easily change the color of the background when you bind the layout to the data. Something like this:
item_list_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="#+id/termometherIndicator"
android:height="match_parent" android:width="4dp"
android:background="#color/default_color" />
<LinearLayout ....>
<!-- The rest of the item layout goes here -->
</LinearLayout>
</LinearLayout>
Then get the view reference in code behind using the Id and change the background color to whatever you need.
The ListView row should be an horizontal LinearLayout, and include this at the beginning, as a first child, followed by the TextView:
<View
android:id="#+id/reputationColor"
android:layout_width="4dp" //Aproximately the size from your image
android:layout_height="36dp" //About the size of a List Row, but less, as seen in your image
android:layout_gravity="top"
/>
You will need a CustomAdapter for your ListView, which you can find a full example in here
Inside the CustomAdapter, you can set the colour programatically with
View reputationColor = (View)findViewById(R.id.reputationColor);
switch (reputation) {
case 1:
reputationColor.setBackgroundColor(#ff0000);
break; //Include other cases up to 5
default:
break;
}
For more information on CustomAdapters, go to the following websites:
ONE
TWO
THREE
You can add a panel to the leftmost part of your app. In that panel you can place one of the predefined images of thermometer. You only have 5 levels, right? Once value changes - select corresponding image of thermometer reading and redraw gui element.

Change View Focus android xml

i am using Listview inside the Scroll view. and this Listview is the last item of scroll view. Problem is when data gets load the scroller move down to the bottom. How can i prevent it from scrolling on loading data.
You shouldn't use a ListView inside a ScrollView as they are both scrollable.
However to solve your problem I guess you should use the attribute android:descendantFocusability="blocksDescendants" for your main container which resides in the ScrollView.

Scrolling in a view containing a ListView (Android)

In one of my activities I am showing certain information and at the end I have a ListView.
So my layout looks a bit like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
>
...
<ListView android:id="#android:id/android:list" android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
When I run the application in the emulator I see that the information before the list view is always shown in the screen and there is a vertical scroll for the ListView only.
Is there a way to change this scrolling behaviour ?. What I would like to have is a vertical scroll for all the information in the screen, not only at the level of the ListView.
I tried wrapping the LinearLayout with a ScrollView, with different combinations of the android:layout_height attribute for all the views involved but I did not get the effect that I was looking for. Besides, some people say that it is a pretty bad idea to wrap a ListView with a ScrollView :
Android ScrollView layout problem
Scrolling with Multiple ListViews for Android
Thanks for any other ideas.
I've not tried this, but you might want to look at you tube video:
Google I/O 2010 - The world of ListView
http://www.youtube.com/watch?v=wDBM6wVEO70
Starting at time 27:11, it talks about how to make info above and/or below a ListView scroll with the ListView. It refers to scrolling headers and footers, but it does say you can put anything you want in them.

Categories

Resources