how can move scroll bar automatically? - android

I am using grid view for display images, I want to scroll move automatically, whenever I add another images in gridview, is it possible?

I think you have to read a little bit more :
https://developer.android.com/reference/android/widget/AdapterView.html#setSelection(int)
ListView.setSelection(position);
https://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)
ListView.smoothScrollToPosition(position).

Either call setSelection() or use smoothScrollToPosition().

I got result using GridView.setSelection(GridView.getCount()),so scroll bar automatically move to last element of Grid View

Use TimerTask to do that since Adapter.notifyDataSetChanged() takes delay to update the grid or list.
Code is below:
new Timer().schedule(new TimerTask()
{
#Override
public void run() {
nameHandler.sendEmptyMessage(0);
}
},500);
//// And in handler::
Handler nameHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
gridView.setSelection(selectorIndex);
// OR
gridView.smoothScrollToPosition(selectorIndex);
gridView.invalidate();
}
} ;

Related

Endless auto scroll view pager with page indicator

I have to build view pager with endless auto scroll. Also I need to show a page indicator below view pager which should respond as per the scroll events. I have applied following logic for endless auto scroll currently:
public void setupAutoPager(final int size) {
final Handler handler = new Handler();
final Runnable update = new Runnable() {
public void run() {
promotionViewPager.setCurrentItem(currentPage);
if (currentPage >= size - 1) {
currentPage = 0;
} else {
++currentPage;
}
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
}, 500, 2500);
}
Problem: When 1st item comes again from last item on auto scroll, animation of view pager is in backward direction as position is reached again (looks like flicker as going back), whereas when auto scroll from 1st to 2nd element then animation comes in forward direction (going to next element). I want animation should always be in forward direction. Backward animation comes as I am setting current item of view pager to 0 position when it reaches to end. How to implement that.
I know this post is few months old but i am a beginner at building android applications & I am facing the same problem.
I would try the following approach even if it absolutely does not seem to be optimal....
Build an array into your adapter class which will contain all of your fragments.
Populate this array with the fragments you want to display. The trick will be to add the first fragment at the end of the list & remove it after the ViewPager has done its changes.
Override the following methods getItem(int position) and getCount() with your array : return yourFragmentArray.get(position); & return yourFragmentArray.size();
The idea is to populate the ViewPager with the same fragments over & over again (+ new ones if needed) and delete the one you used to populate the VP so you never go back.
So if i take your code i would do something like this :
private int size = yourFragmentArray.size();
public void setupAutoPager(final int size) {
final Handler handler = new Handler();
currentPage = promotionViewPager.getCurrentItem();
final Runnable update = new Runnable() {
public void run() {
if (currentPage == size - 1) {
// Add the fragment to your fragmentArray
// Only the rank 0 needs to be taking care, works like in queue
yourFragmentArray.add(yourFragmentArray.get(0));
// Notify the changes made
yourAdapter.notifyDataSetChanged();
// Set the next item
promotionViewPager.setCurrentItem(currentPage + 1);
yourFragmentArray.remove(0);
// Update the size
size += 1;
}
else {
promotionViewPager.setCurrentItem(currentPage + 1);
}
}
};
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
}, 500, 2500);
}
The logic is somewhere here for me, again it's probably not the best way to do it but i can't see another for now.
Good luck ! I will update this post as soon as i can test it myself.
UPDATE :
A much better way to do it is to add in this method :
Save into a specific variable firstMaxSize the size of the ViewPager before the method is called.
Use a counter (if it's the first time you loop through the adapter fragments, we won't remove them), but if(counter != 0) we will loop through the yourFragmentArray() and remove from 0 to firstMaxSize.
Call yourAdapter.notifyDataSetChanged()
Then add all over again the new fragments. If the problem of an existing fragment is already added happens, create a new fragment based on the content of this one.
Call again yourAdapter.notifyDataSetChanged()
It is indeed very heavy & i am pretty sure another easiest way exists but have not found it yet.

Programmatically make a click or touch an item in recyclerview

How to force a click or touch an item on a recyclerview?
I found people talking to use a command recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick() but recyclerView.findViewHolderForAdapterPosition(0) always returns null!
I want initially that the first recyclerview item is clicked/ touched.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
recyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
}
},100);

Set a ListView scrolled to bottom at app start? (android)

I dont want to scroll it (becz i think it takes time and perfomance to scroll). I want to set it already scrolled to bottom at the start of the activity like whatsapp.
How could this be done?
If you want your ListView to be always scrolled at the bottom even on updating, you can add these attributes to the list:
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Otherwise use :
listView.setSelection(adapter.getCount() - 1);
listview.setSelection(LastItemIndexInListView);
LastItemIndexInListView : size of listview - 1.
listView.post(new Runnable() {
#Override
public void run() {
listView.scrollTo(0, listView.getBottom());
}
});
Give this a shot.
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(listView.getCount() - 1);
}
});
This should work for you

Android runOnUiThread not updating UI elements

Hi I am developing android application in which I am using grid view. I want to updated grid item dynamically.My grid item contains one title text. I tried to do it like this but it is not working for me.
((Activity)context).runOnUiThread(new Runnable()
{
public void run()
{
Debug.print("this is update progress inside thread ...");
owner.setText("Uploading...");
invalidate();
requestLayout();
}
});
So in above code its printing debug statement. But not updating my owner text which is inside grid item.
I tried this also...
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
owner.setText("Uploading...");
}
};
handler.sendEmptyMessage(0);
Try to refresh your adapter which you used to load the grid item.
Either you could use invalidateViews() method or adapter.notifyDataSetChanged()
This will help you to resolve your issue.
Happy coding..
So in above code its printing debug statement. But not updating my
owner text which is inside grid item.
I think it's textview does not reference to your item in grid adapter. You can check it by set tag when init and getTag() inside runonUIThread method.
Let check this textview is local or global variable. If global, it's only reference to the last item - then check the last item change?
OK if it's not. Let post full ur code.
runOnUiThread(new Runnable() {
#Override
public void run() {
Debug.print("Updating UI ...");
owner.setText("Uploading ...");
invalidate();
requestLayout();
}
});
Check if you have any Thread.sleep() in your code that might be blocking the UI.

how to scroll horizontal scroll view without animation

The problem is that I'm using the fullScroll() and scrollTo() functions to scroll but it is animated and I need it happen without user observation.
Is there a way to solve this problem?
hScrollView.post(new Runnable() {
#Override
public void run() {
hScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
Use this
// Disable the animation First
hScrollView.setSmoothScrollingEnabled(false);
// Now scroll the view
hScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
// Now enable the animation again if needed
hScrollView.setSmoothScrollingEnabled(true);
You can use hScrollView.setSmoothScrollingEnabled(false); before running the Runnable command, this will give you the desired output
// Set smooth scrolling to false
hScrollView.setSmoothScrollingEnabled(false);
// Then call the fullScroll method
hScrollView.post(new Runnable() {
#Override
public void run() {
hScrollView.fullScroll(View.FOCUS_RIGHT);
}
});

Categories

Resources