android: simultaneously scrolling horizontal listView - android

Consider I have two Horizontal ListView as shown below:
list1
list2
I want to know if it is possible to make the second list (list2) view scroll horizontally simultaneously along with the scroll of first list (list1).....
That is when I scroll list1 (horizontal), even list2 must scroll by the same offset...
Is this possible, if yes, please do help...
![link to image]: https://picasaweb.google.com/109389839906668906213/January132012#5697019272538269218

You can do that - just create such layout and use scroll events:
list1.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
list2.setSelectionFromTop(firstVisibleItem, list1.getChildAt(0).getTop());
}
});
Some explanation:
Better use list.setSelectionFromTop() than list.scrollTo() - because first visible item of first list can be shown partially.
list1.getChildAt(0).getTop() - is construction for getting value of X coordinate of first visible item.

As There is no HorizontalListView in Android, you must having another adapter view, anyway implement following:
list1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView adapterView adapterView, View view, int position, long id){
list2.setSelection(position);
}
});

Related

Can't scroll in a ListView in a swipeRefreshLayout

I'm having an issue with the SwipeRefreshLayout. I have a list view within the layout and every time I scroll upward in the list view the swipe to refresh layout is tiggered and you can never scroll back to the top of the layout. Anyone have any ideas?
I found an answer to my question. I am manually enabling the swipeRefreshLayout when the first child in the listview is at the top of the list.
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView listView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ?
0 : expandableListview.getChildAt(0).getTop();
refresh.setEnabled((topRowVerticalPosition >= 0));
}
});
https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
According to Android docs at the above link,
This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.
If the ListView is the 1st direct child of the SwipeRefreshLayout, refresh won't be triggered before scrolling back to the top of the ListView. You don't need to do anything in onScroll().

How to implement scroll in list view to implement pagination?

I am building an application like techcrunch.
I am fetching data from server in JSON format and displaying the data in list view like article title,author name and image.
I have applied pagination means when user scroll more articles load in a list view. My pagination works fine but there is an issue in the scroll function as the fresh or new data loads the scroll dose not aligns with the data.
To clarify more in simple words my scroll-er goes at the top of the page when i am actually scrolling down
this is my code :
listView.setOnScrollListener(new AbsListView.OnScrollListener()
{
#Override
public void onScrollStateChanged(AbsListView absListView, int i)
{
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount){
if (mPreLast != lastItem)
{
mPreLast = lastItem;
onStart();
}
}
}
});`
You can use:
listView.setSelectionFromTop(newPosition, 0);
With this method you can set the position to a ListView, using the first parameter as the new position on the list, and the second parameter can be used to set the distance from the top

How do I change the list item's background after smooth scrolling to it?

I'm having an issue fetching the view of the list item when it's off screen.
First I'm smooth scrolling to it
//locationsList is a listview
locationsList.post(new Runnable() {
#Override
public void run() {
locationsList.smoothScrollToPosition(k);
}
});
Then I want to do
View listItem = locationsList.getChildAt(k);
listItem.setBackgroundColor(getResources().getColor(R.color.highlight));
How do I call this after it's done scrolling?
Set a unique tag to listitem view. Access this list item with this tag whenever you want to change the scrolling.
If all you want to know is when your scrolling did complete, you need to add an onScrollListener to your listview:
locationsList.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){
View listItem = locationsList.getChildAt(k);
listItem.setBackgroundColor(getResources().getColor(R.color.highlight));
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
Of course the above needs a logic to update the background ONLY if scroll has before initiated by you, so you should probably use a boolean flag and set it to true on the locationsList.smoothScrollToPosition(k); execution, then if this flag is true, run the code in the scrollListener and set the flag back to false

Move/scroll clicked listitem view at top of screen in the expandablelistview

I seen SimpleExpandableListAdapter example when i clicked expanded group item at moving top of the screen. i created NewAdapter which extends
BaseExpandableListAdapter. I want to do same thing but dont know how to do. i searched lot of things which is not worked for me. Please let me know how to do.
Thank you in Advance.
This one is working for me
expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (!parent.isGroupExpanded(groupPosition)) {
parent.expandGroup(groupPosition);
} else {
parent.collapseGroup(groupPosition);
}
parent.setSelectedGroup(groupPosition);
return true;
}
});
As the main working part for scroll is
parent.setSelectedGroup(groupPosition);
may this solve your problem .
I think using duration gives a better user experience. So you can use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
descrease duration to make scrolling faster
What you are looking for is,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
myListview.setSelectionFromTop(position, 5);
}
This will position your selected list item as the first visible item on screen. However, it does so without any smooth scroll animation, the moment you tap the item, it becomes the first item visible.
If you want the scroll animation, you could use,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
int offset = position - myListview.getFirstVisiblePosition();
if(myListview.getFirstVisiblePosition() > 0)
offset -= 1;
myListview.smoothScrollByOffset(offset);
}
Note that smoothScrollByOffset is available from API-Level 11 onward.
However, both these methods will not work if you select an item close to the bottom of the list, as the list will not scroll further upwards if the last list item is visible. To overcome this, you could convert your listview into a circular listview, as described here.

List views do not scroll smoothly

I have two list views. I need to scroll one list view automatically
when the other list view is scrolled. both list views should have this ability
i implemented the onScrollListner in both listviews
for listview 1
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (l1.getChildAt(0) != null) {
Rect r = new Rect();
l1.getChildVisibleRect(l1.getChildAt(0), r, null);
l2.setSelectionFromTop(l1.getFirstVisiblePosition(), r.top);
}
}
for listview 2
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (l2.getChildAt(0) != null) {
Rect r = new Rect();
l2.getChildVisibleRect(l2.getChildAt(0), r, null);
l1.setSelectionFromTop(l2.getFirstVisiblePosition(), r.top);
}
}
I have 2 problems regarding this
1 - the lists do not scroll smoothly. (not like normall listview)
2 - I can only scroll both listviews using one list view.(when i scroll using l2
both get scrolled. but it does not work when i scroll using l1. both stay fixed)
Thanks in advance
It happened becoz you put listview inside listview :)
You can achieve you problem using this.
suppose L1 is inside L2 then
L1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN || arg1.getAction() == MotionEvent.ACTION_MOVE)
{
L2.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
It's your application so you could do whatever you want but I am confused with what you want to achieve. The sole idea of having two list views is to have separate representation of two different categories of data so that they can be accessed(scrolled or selected) separately.
If you have requirement of representing two categories of data that need to be displayed side by side as a list then you can have a list row with two columns.
something like this will work:
You can populate these two rows with different kind of data and this can be scrolled together.
I found out a different way to get the same result. I remove my two list views and replaced it
with a grid view with two columns. this way i can scroll both without implementing onScrollListner and i can even use the custom adapter i used for the two listviews.

Categories

Resources