update adapter time gridview scroll automatically on top - android

I am integrating endless grid view. end less is working perfect. but on scroll while update my adapter data at that time grid view goes to top. and i need that it should be maintain it's current position where it is.
For the feel adapter in grid view i used below code.
myDataAdapter = new ProductGridAdapter(activity, results, null);
gridView.setAdapter(myDataAdapter);
gridView.invalidate();
myDataAdapter.notifyDataSetChanged();
Can any one tell me that how can i resolve this issue?

Have a variable that captures the top element of your grid view that is being displayed. Then whenever you invalidate the view, use setSelection() function. http://developer.android.com/reference/android/widget/ListView.html#setSelection%28int%29

I got the way. in first time i add adapter in list view and after that i just update the adapter.
if (myDataAdapter == null)
{
myDataAdapter = new ProductGridAdapter(activity, results, null);
gridView.setAdapter(myDataAdapter);
}
else
{
myDataAdapter.updateData(results); //update adapter's data
myDataAdapter.notifyDataSetChanged();
}

Related

Click on last item in AdapterView in Espresso

I'm working on a note taking app. I add a note, and it get's added to the bottom of the list. As the last assertion in the espresso test, I want to make sure that the ListView displays a listItem that has just been added. This would mean grabbing the last item in the listView. I guess you might be able to do it in other ways? (e.g. get the size of adapted data, and go to THAT position? maybe?), but the last position of the list seems easy, but I haven't been able to do it. Any ideas?
I've tried this solution, but Espresso seems to hang. http://www.gilvegliach.it/?id=1
1. Find the number of elements in listView's adapter and save it in some variable. We assume the adapter has been fully loaded till now.:
final int[] numberOfAdapterItems = new int[1];
onView(withId(R.id.some_list_view)).check(matches(new TypeSafeMatcher<View>() {
#Override
public boolean matchesSafely(View view) {
ListView listView = (ListView) view;
//here we assume the adapter has been fully loaded already
numberOfAdapterItems[0] = listView.getAdapter().getCount();
return true;
}
#Override
public void describeTo(Description description) {
}
}));
2. Then, knowing the total number of elements in listView's adapter you can scroll to the last element:
onData(anything()).inAdapterView(withId(R.id.some_list_view)).getPosition(numberOfAdapterItems[0] - 1).perform(scrollTo())

How to show and hide view in recycler view in Android?

In my app I am using recycler view.I want to show and hide view on particular condition.But when I scroll recycler views I am not getting expected behaviour.When I Visible a view it gets visible for other rows as well randomly.
What I understand is when it recycles it reuses view and when previous view when it gets recycled it finds the visibility of that view.How can I hide view on particular condition? here is my adapter code
#Override
public void onBindViewHolder(UrduRhymesViewHolder holder, int position) {
RhymesModel current = mUrduRhymesList.get(position);
AppUtility.setCustomFont(mContext, holder.tvUrduRhymesName, Constants.HANDLEE_REGULAR);
holder.tvUrduRhymesName.setText(current.getRhymeName());
holder.ivUrduRhymesLogo.setImageUrl(current.getThumbnailUrl(), mImageRequest);
int status = AppUtility.getFavouriteStatus(mContext, current.getRhymeName(), new UrduRhymesDb(mContext));
if (status == 0)
holder.btnFavourite.setBackgroundResource(R.mipmap.btn_star_unactive);
else
holder.btnFavourite.setBackgroundResource(R.mipmap.btn_star);
ProgressbarDetails progressbarDetails = ProgressbarDetails.getProgressDetail(current.getRhymeName());
if (progressbarDetails == null) {
progressbarDetails = new ProgressbarDetails();
progressbarDetails.prgProgressBar = holder.pbRhymeDownload;
progressbarDetails.download_btn_settings = holder.downloadButtonLayout;
} else {
progressbarDetails.prgProgressBar = holder.pbRhymeDownload;
progressbarDetails.download_btn_settings = holder.downloadButtonLayout;
holder.pbRhymeDownload.setProgress(progressbarDetails.progress);
}
ProgressbarDetails.addUpdateProgressDetail(current.getRhymeName(), progressbarDetails);
if (progressbarDetails != null && progressbarDetails.isDownloading) {
Log.e("test","downloading foe position "+position );
holder.downloadButtonLayout.setBackgroundResource(R.mipmap.btn_download);
holder.pbRhymeDownload.setVisibility(View.VISIBLE);
holder.pbRhymeDownload.setProgress(progressbarDetails.progress);
} else {
Log.e("test","should not be visible for position "+position);
holder.pbRhymeDownload.setVisibility(View.GONE);
}
}
Here progressbarDetails.isDownloading (having value true) is the criteria when I want to show my view but is else clause it is not hiding my view
Edit: Here ProgressbarDetails (Singleton )is a class keeping reference of every row of adapter's progress bar.
No direct way of hiding and unhiding recylerview childitems.
Solution:
Let us assume that the recyclerview adapter is ArrayList
Now make another arraylist (temp_list)
Scenarios:
Hide: iterate through your adapter items and remove the ones that you want to hide. Put each of these into temp_list. After iteration is over, call notifyDataSetChanged()
Show: iterate through your temp_list items and remove the ones that you want to show. Put each of these into adapter. After iteration is over, call notifyDataSetChanged()
You should add a flag in your viewHolder that indicates if this view should be displayed or not . and check this flag every time in the onBindViewHolder.
because the recyclerView reuses the same views you should make a decision depending on something special for every view in you viewHolder.
do u mean when your data has been changed?
and your layout want to change ?
adapter.notifyDataSetChanged();

GridView animation

I want add animation to every single grid view item. I did something like this in method GetView():
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.animation));
And animation works, but when I am adding new elements to my grid view, it refreshes whole grid view and all items animates again. My purpose is to invoe animation only when item is created and when I scroll. How to achieve that aim?
I think you have to set up a map of Boolean in your adapter.
For each adapter pos, check if the position has already been animated.
Something like that :
if(map.get(position) == null || !map.get(position)){
map.put(position, true);
convertView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.animation));
}

expandablelist view scrolling position

I am trying to keep my last scrolled position and populate the adapter with new data but same scrolling position. In my scenerio, I have updating expandable listview every 5 second and updating new items very 5 seconds but with last scrolling position left by user.
I have searched a lot about it and found few solutions but still it is behaving strangly each time I set scrolling position.
I am trying to achieve it with this method:
activity.runOnUiThread(new Runnable()
{
#Override public void run() {
state = list.onSaveInstanceState();
if(settings != null) {
if (mode.equals("Now")) {
info_panel = new esInfoListAdapter(activity, data, expanHash, false, esActivity.lastgrpPosition);
}
else {
info_panel = new esInfoListAdapter(Activity.this, data, tchildData, true, lastgrpPosition);
}
}
list.setAdapter(info_panel);
if(state != null)
list.onRestoreInstanceState(state);
info_panel.notifyDataSetChanged();
}
}
});
So this method gets called just before I updating expandable listview. So in short in every five seconds. I am using Parceable object to save list.onSaveInstanceState(); and then use list.onRestoreInstanceState(state); when updating new data, but the issue is sometimes it works and sometimes not? Am I missing some trick here? Thanks for your help.
I have not yet used ExpandableListView yet but I might. I think you should use combination of methods like getFirstVisiblePosition, getLastVisiblePosition, and getExpandableListPosition. I think getting the groupPosition is sufficient for a user. A link I referenced is ...get index of first/last visible group in an ExpandableListView

Android simple stuff about scrolling

As I don't know how to ask it to google, I'll try here.
I basically have a listView displayed from an intent. This listView is feeded by a custom BaseAdapter.
This listView is actually a calendar. I want on the opening of the activity having this calendar listView that it has been already scrolled to te current day.
Basically I'm asking how can I pre-scroll a listview to an index of the listView ? Is this possible ? With a function of custom BaseAdapter ?
Thanks.
You can make use of following methods -
This method to save save list scroll position.
private void saveListScrollPosition() {
// save index and top position
index = _listview.getFirstVisiblePosition();
View view = _listview.getChildAt(0);
top = (view == null) ? 0 : view.getTop();
}
And this one to get it back and scroll it down automatically -
private void restoreListScrollPosition() {
// restore
_listview.setSelectionFromTop(index, top);
}
I think in your case, index will be the current date. Right?
Use,
list.setSelection(index);

Categories

Resources