I have an intent that loads a new activity, I also have some extra intent data to get when the new activity is started. Depending on what button is clicked on the first activity, it should scroll to a certain part of the scroll view on the next activity
It would be easiest to have it scroll down enough so that a certain item is at the top and the first thing read (defined by its ID)
Does anyone know how to do this?
Thanks
Check out the smoothScrollToPosition method in ListView:
http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)
This is what you can call to automatically show your list view to the desired position
ListView.setSelectionFromTop(index, 0);
to find out where to start you must compare the item that you want the list to show at startup from the list and the item that is received from your intent.
if you exactly know the position of a specific item in the listview you can call the method written above directly
Related
I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.
So I have a standard RecyclerView that takes a list of posts and displays them to the user. My problem is when the user comes into the activity, I want to be able to change the focus to a particular post.
Ex. User A sees that he has a notification that of someone has liked his/her post. He/she clicks that notification, and it loads the activity of user's post, and then the focus changes to the position of the post that had bee liked.
I pass in the postID to the activity using an intent. I then run the Recycler View, and onBindHolder i try to see if the post's ID matches to the postID I sent in via intent. If it does, I now set the activity's variable postFocusPosition to the position of that post.
Now I'm stuck on what that magic method i need to call in RecyclerView class to actually change the focus to that item position!
Hope someone can help enlighten me on that magic line(s) of code that changes focus!
recyclerView.setAdapter(adapter);// set adapter on recyclerview
recyclerView.scrollToPosition(mSkipTo); //use to focus the item with index
adapter.notifyDataSetChanged();
mProgressDialog.dismiss();
For smooth scrolling effect and to avoid any resizing issue of recyclerview items, you can use
recyclerView.smoothScrollToPosition(itemposition); //you can get this position, from your adapter class through an interface method
As with "scrollToPosition", I was facing resizing issue of recyclerview items.
Note : You could use this method for both horizontal and vertical recyclerview.
just use this
recyclerView.scrollToPosition(position);
I've created a context menu for my recycler view items. In the menu I have Watch (like favourites), Share and Hide. Now what I want to do is get the specific position of the Recycler View and check if the Watch or Hide button has been pressed. How can I go about doing this? I've used a flag but that just checks the whole Recycler View so that doesn't work.
I can show source code on request too. I know it needs to go in my Adapter but can't figure out the position of it
Thanks in advance.
just add a boolean variable in your List to check whether to show Watch or Hide button.
use getAdapterPosition() method when user clicked the clicked on button and then use that position to update value in your model class.
I'm wondering right now how to implement following functionality in best and most reusable way.
Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.
I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?
You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.
To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.
The most simplest thing I could suggest for it is
Remove the item from the list and the add the item to the list on calling on activity result.
Set the data to the adapter (may be using a public method in adapter class)
Call notify data set changed.
Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().
I have 3 activities containing listviews with custom adapters, an item selected at the first one drives the user to the second one, and so on...
At these listviews, some items are highlighted as "new itens" (each time adapter's getView is called, I check at a database if the current item should be highlighted). Once user has reached the 3rd listview, I mark these items as "checked", and want to propagate this change back to the other listviews...
That means, when user comes back, pulling the 2nd and 1st listviews up from the stack, I want the viewed items not to be highlighted anymore.
I've tried this answer on SO, without success. When executing notifyDataSetChanged() from onResume(), my listview simply didn't shows up. And I'd prefer not to use startActivityforResult()...
here's my code to refresh the listview at onResume():
#Override
public void onResume(){
super.onResume();
//I have basically the same code at onCreate()
adapter = new ListingsAdapter(getApplicationContext(), this);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
here's another answer that sounds promising, but I wasn't sure how can I retrieve an specific view from the adapter... I want to check all visible items with adapter.getView(), but it asks for a convertView and a parent ViewGroup and I couldn't get that
Thanks in advance for any hint on this
Why don't you wanna use startActivityforResult()?
You don't need to create a new adapter every time in onResume(). You only need to change the underlying data (set the checked flag for the visited item) and then call adapter.notifyDataSetChanged(). This will trigger the adapters getView() method for all the visible list elements.