Android 1个父级ScrollView,内含3个可纵向滚动的TextView

3
我有一个关于Android编程的问题。
我的整个Activity中有一个父滚动视图,其中有三个带有滚动功能的文本视图。但是,当我使用以下代码时,似乎根本不起作用。只有父滚动可用。
        final View view = inflater.inflate(R.layout.activity_register_terms_fragment, container, false);
        TextView basicTermView = (TextView) view.findViewById(R.id.register_terms_basic_info);
        TextView purposeTermView = (TextView) view.findViewById(R.id.register_terms_purpose_info);
        TextView provideTermView = (TextView) view.findViewById(R.id.register_terms_provide_info);
        TextView previous = (TextView) view.findViewById(R.id.register_terms_pre);
        TextView next = (TextView) view.findViewById(R.id.register_terms_next);

        basicTermView.setMovementMethod(new ScrollingMovementMethod());
        purposeTermView.setMovementMethod(new ScrollingMovementMethod());
        provideTermView.setMovementMethod(new ScrollingMovementMethod());

我该如何修改代码? 谢谢你的帮助!

1个回答

4
您不能在ScrollView中放置可滚动的视图,如TextViewListViewRecyclerView。因此,请将简单的TextView放置在普通布局中,并为其添加android:scrollbars属性,或者您可以使用自定义类来计算视图的宽度/高度并将ScrollView用作其父级。
例如,要在ScrollView中使用Listview,我们需要使用以下自定义Listview类,该类将计算列表项的高度并设置它。
public class ExpandedListView extends ListView {

private ViewGroup.LayoutParams params;
private int old_count = 0;

public ExpandedListView(Context context, AttributeSet attrs) { 
    super(context, attrs);
}   

public ExpandedListView(Context context) {
    super(context);
}

public ExpandedListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();    
}
@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        this.setScrollContainer(false);
        old_count = getCount();
        params = getLayoutParams();
        params.height = getCount()
                * (old_count > 0 ? getChildAt(0).getHeight() : 0);
        setLayoutParams(params);
    }

    super.onDraw(canvas);
}

}

这是什么?您可以使用视图的自定义类。您能详细说明一下吗?@Beena - DJphy
是的,当然。我已经编辑了我的答案。你可以看一下。 - Beena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接