I'm not sure if this is hard to do with a RecyclerView or not, I thought there was an easy way to do it but I'm having some difficulties.
I have a RecyclerView on a activity, and when I go to the activity, I want it to scroll to an item. Seems fine enough. However, my issue is that, say only 5 items fit on screen at a time, and we have 6 items, and we scroll to the 5th or 4th item, the recycler view does scroll a tiny bit, but it doesn't look like it scrolls 'to' the element, as it is confined by the RecyclerView bounds.
Is there a way to make it so if I scroll to the 4th or 5th item in the RecyclerView, it will position it at the top of the view, and the area below the last item will just be the background color of the normal view, and I can just scroll back up normally?
I hope I'm explaining myself correctly. I'm not sure if this can be done easily and I have made a mistake in the XML setup or code setup.
<merge 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:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/timelineSwitcherView"
layout="#layout/view_timeline_view_switcher"/>
<ViewAnimator
android:id="#+id/viewAnimator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/timelineSwitcherView">
<RelativeLayout
android:id="#+id/timeLineRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lighterBlue">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/timeLineSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/timeLineRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Appreciate any help and guidance with this.
You should use
mRecyclerview.scrollTo(x,y);
mRecyclerview.scrollToPosition();
Or even better:
mRecyclerview.smoothScrollBy( x , y);
mRecyclerview.smoothScrollToPosition(List item position);
So you need to get the Y position, or the child position in the list.
Related
I'm creating a simple app for personal use and I'm stuck because I need to add/remove a text from my scrollview. In the screenshot below the blue square is my scrollview and the mail button (placeholder) needs to open a popup where I can put 2 values in 2 different boxes, a string and a double.
Main_Activity
I know how to do almost everything but I don't know how to add a text every time I click on the mail button and it should look like this when I click on it
this is how it should be when is done
The left button of a car block is the "delete from scrollview" button that removes it from the scrollview and preferences and the right button that does like the 2nd image but where I can edit it.
My questions are:
How can I add a "block" of items in a scroll view? Per block I mean: Edit and delete button and the text like in the example.
Suggestions on what to use for the popup that adds 2 input fields?
In my opinion, I suggest to use recyclerView instead because of performance issues.
link
But, if you need to do the way using ScrollView just follow this.
In your xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:fillViewport="true"
android:padding="16dp">
<LinearLayout
android:background="#android:color/holo_red_light"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And add item into linear layout when handle click event.
val linear = view.findViewById<LinearLayout>(R.id.linearLayout)
val item =
LayoutInflater.from(requireContext()).inflate(R.layout.item_list_row, linear, false)
linear.addView(item)
Open source code that I am using
in my design render it shows me the entire background is blue but the actually app only have color on the listviews. Following is my xml code and screen shots.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b8569fee">
<TextView android:id="#+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#b8569fee"
android:textSize="24dp"/>
<TextView
android:id="#+id/device_address"
android:layout_width="match_parent"
android:background="#b8569fee"
android:layout_height="wrap_content"
android:textSize="12dp" />
</LinearLayout>
render
actually app:
I tried to change the textview layout height to match_parent, but it still didn't work, so what do i need to do to change the entire background?
Thnaks
What's happening here is that your XML file is defining the layout for an individual element in that list view, then the list view is programatically applying this XML to build a new view onto the list whenever a new BLE device is discovered. The size of each list element is defined externally, by whatever is putting each of your views into the list. Therefore, when you put match_parent in the LinearLayout and expect that to fix your issue, it does not because it only fills the area allotted by the listview. The fix is to change the background color within the listview itself, which is not done in this XML.
I'd need to see to more code to know exactly how to do this, but I am going to take a shot in the dark and assume that you are using a ListActivity, as in the Google BluetoothLeGatt example. In this case, you can change the background color of the ListView using something like:
getListView().setBackgroundColor(Color.rgb(86, 159, 238));
Placed in the onCreate method.
The view indicated is only a view inside the layout that contains the ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b8569fee">
<TextView android:id="#+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#b8569fee"
android:textSize="24dp"/>
<TextView
android:id="#+id/device_address"
android:layout_width="match_parent"
android:background="#b8569fee"
android:layout_height="wrap_content"
android:textSize="12dp" />
</LinearLayout>
You must change the background color to the layout that contains the ListView
android:background="#b8569fee"
I'm currently working on a Grade Manager for school written in Java for Android 6. I just started with Android so I'm no expert.
The Problem:
If I call the method notifyItemRemoved() on my RecycleView Adapter and the last CardView moves from the left bottom place to the upper right place, the View is resized and the animation is cut of.
Now I don't know why that view is resized because RecycleView's layout_height attribute is match_parent.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".SemesterActivity"
tools:showIn="#layout/activity_semester">
<android.support.v7.widget.RecyclerView
android:id="#+id/subject_list_view"
android:padding="20dp"
android:layout_below="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>
I took a GridLayoutManager as LayoutManager for the RecycleView.
subjectListView.setLayoutManager(new GridLayoutManager(ActivityContext, 2));
How i update the RecycleView:
SubjectAdapterObj.notifyItemRemoved(viewHolder.getAdapterPosition());
The animation is the default animation.
Could the GridLayoutManager be the problem?
Video Example:
Sorry. English is not my native language.
So I finally found out what the problem was.
It actually was the NestedScrollView which resizes it's children.
So what you need to do is to simply add android:fillViewport="true" to your NestedScrollView.
Thats it. I hope it helps someone although it's not a really difficult bug. I just searched at the wrong places.
I have two ListView and I want them to share the same layout position so when I click a button one ListView hides.
Maybe this is not possible or there is a better way like fragments?
Use FrameLayout. This layout view overlies two views over each other.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
For showing the first page (i.e. the first ListView):
findViewById(R.id.list1).setVisibility(View.VISIBLE);
findViewById(R.id.list2).setVisibility(View.GONE);
And for the second page:
findViewById(R.id.list1).setVisibility(View.GONE);
findViewById(R.id.list2).setVisibility(View.VISIBLE);
fragments are the easy way to do this IF you don't plan on changing the data in your views.
Make a button and
/*create fragment of the opposite view, probably through a boolean field and an if block
then*/
getSupportFragmentManager.beginTransaction().replace(/*your fragments*/).commit().
in your onclicklistener.
I have a main menu screen with a simple ListView that contains "links" to further screens in my app (Browse, Bookmarks, Settings, About, etc.). Underneath the ListView there is a TextView (more accurately, a TextSwitcher that rotates TextViews) that changes every 10 seconds to display a new "tip".
In portrait mode, this works fine. There are my five list items in the ListView , and my tip label underneath. However, when I switch to landscape mode, the ListView is taller than the screen. The ListView scrolls normally, but I cannot scroll past the end of the ListView to see the TextView underneath.
I have tried every possible combination of Layouts, wrappers, ScrollViews, and layout_height parameters and I simply cannot get it to behave.
Here is the simplest code I can use to get the result pictured above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ListLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/TipLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/ListLayout">
<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/TipSwitcher">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="7pt"
android:id="#+id/Tip1TextView" android:text="Tip: Hello, Android!"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tip: This is the second TextView in the TipSwitcher!"
android:id="#+id/Tip2TextView" android:textSize="7pt"></TextView>
</TextSwitcher>
</LinearLayout>
</RelativeLayout>
Like I've said, I've already tried so many different combinations that I can't list them, and for the most part I was randomly inserting XML in an attempt to get something to work the way I wanted. So I'd greatly appreciate suggestions as to how I would go about doing this the right way.
Thanks.
EDIT: Something I forgot to mention, this may or may not be relevant. My MainMenuActivity is extending ListActivity. According to the docs, "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen." But, "If you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate()." So I don't think the ListActivity is interfering.
Put the TextSwitcher in the ListView itself. You can use addFooterView() for this.