我基本上会访问这个网站:http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
但我遇到了这个问题:
引起:java.lang.IllegalStateException:HorizontalScrollView只能托管一个直接子项“
在这一行:
addView(internalWrapper);
这是myHorizontalScrollView类:
public class MyHorizontalScrollView extends HorizontalScrollView {
int scrollToViewPos = 0;
public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyHorizontalScrollView(Context context) {
super(context);
init(context);
}
void init(Context context) {
// remove the fading as the HSV looks better without it
setHorizontalFadingEdgeEnabled(false);
setVerticalFadingEdgeEnabled(false);
}
public void initViews(final MyHorizontalScrollView me, View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
LinearLayout internalWrapper = new LinearLayout(getContext());
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
final int w = me.getMeasuredWidth();
final int h = me.getMeasuredHeight();
sizeCallback.onGlobalLayout();
int[] dims = new int[2];
scrollToViewPos = 0;
for (int i = 0; i < children.length; i++) {
sizeCallback.getViewSize(i, w, h, dims);
internalWrapper.addView(children[i], dims[0], dims[1]);
if (i < scrollToViewIdx) {
scrollToViewPos += dims[0];
}
}
new Handler().post(new Runnable() {
public void run() {
me.scrollBy(scrollToViewPos, 0);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Allow touch events.
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Allow touch events.
return true;
}
public interface SizeCallback {
public void onGlobalLayout();
public void getViewSize(int idx, int w, int h, int[] dims);
}
}
xml中的MyHorizontalScrollView:
<?xml version="1.0" encoding="utf-8"?>
<com.yahya.LeftSlideMenu.MyHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0px"
android:fadingEdge="none"
android:fadingEdgeLength="0px"
android:padding="0px"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0px"
android:background="#ffffffff"
android:orientation="horizontal"
android:padding="0px" >
</LinearLayout>
</com.yahya.LeftSlideMenu.MyHorizontalScrollView>
答案 0 :(得分:1)
您遗失addView(internalWrapper);
这就是为什么不可见。