问题
In my android application, there are two HorizontalScrollViews I am using. Here is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/hScroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/Blue" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtFriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtFriend"
android:textSize="17sp" />
<TextView
android:id="@+id/txtList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/txtList"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/txtWork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/txtWork"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/txtPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/txtPlay"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="@+id/txtBuddies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/txtBuddies"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="@+id/hScroll2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="@+id/listFriend"
android:layout_width="67dp"
android:layout_height="280dp" />
<ListView
android:id="@+id/listList"
android:layout_width="67dp"
android:layout_height="280dp"
android:layout_marginLeft="6dp" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Now, what I have to do is when I move 2nd horizontal scrollview (here id =hScroll2) then Simultaneously my 1st horizontal scroll view must be moved. Here is my java code.
hs2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int scrollX = v.getScrollX();
int curX = scrollX;
hs1.scrollBy(scrollX, 0);
return false;
}
});
But, when I am moving 2nd scroll view horizontally from Left to Right, 1st scroll view is moving with it at same time, but when I am moving 2nd back to its position(Right to Left), then 1st one is not moving. It sticks on new position. What am I missing?
回答1:
OK, I come with a small workaround for such needs. I have implemented it in one of my projects and it works fine:
make a custom HorizontalScrollView and a ScrollListener interface to get the results You want:
ExampleScrollView.java
public class ExampleScrollView extends HorizontalScrollView {
private ScrollViewListener scrollViewListener = null;
/**
* constructor
*
* @param context
* current contex
* @param attrs
* AttributeSet
* @param defStyle
* style
*/
public ExampleScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
* constructor
*
* @param context
* current contex
* @param attrs
* AttributeSet
*/
public ExampleScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* constructor
*
* @param context
* current context
*/
public ExampleScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
/**
* sets the scrollViewListener
*
* @param scrollViewListener
*/
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
}
ScollViewListener Interface
public interface ScrollViewListener {
void onScrollChanged(ExampleScrollView scrollView, int x, int y, int oldx,
int oldy);
}
Use the ScrollView
private ExampleScrollView mScrollViewOne;
private ExampleScrollView mScrollViewTwo;
mScrollOne = (ExampleScrollView) findViewById(R.id.horizontal_one);
mScrollOne.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged(ExampleScrollView scrollView, int x,
int y, int oldx, int oldy) {
// TODO Auto-generated method stub
mScrollTwo.scrollTo(x, y);
}
});
mScrollTwo = (ExampleScrollView) findViewById(R.id.horizontal_two);
mScrollTwo.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged(ExampleScrollView scrollView, int x,
int y, int oldx, int oldy) {
// TODO Auto-generated method stub
mScrollOne.scrollTo(x, y);
}
});
Usage in layout.xml
<com.yourpackage.ExampleScrollView
android:id="@+id/horizontal_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
>
I hope this helps a little bit...
来源:https://stackoverflow.com/questions/17760191/horizontalscrollview-motion-in-android