首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >垂直和水平滚动视图

垂直和水平滚动视图
EN

Stack Overflow用户
提问于 2013-12-05 07:48:21
回答 1查看 223关注 0票数 2

我使用的是一个需要垂直滚动和水平滚动的桌子。我使用了两个滚动视图,如下所示。是否可以同时使用单一的一种?意思是将一个滚动视图启用为水平和垂直的:

代码语言:javascript
运行
复制
LinearLayout contentView = (LinearLayout) findViewById(R.id.contentView);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;

    for (int i = 0; i <28; i++) {
    tableRow = new TableRow(getApplicationContext());
    for (int j = 0; j < 16; j++) {
        textView = new TextView(getApplicationContext());
        textView.setText("test");
        textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
    }
    tableLayout.addView(tableRow);
}
ScrollView scroll = new ScrollView(MainActivity.this);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                     LayoutParams.FILL_PARENT));
scroll.addView(tableLayout);
HorizontalScrollView horizontalScroll = new HorizontalScrollView(MainActivity.this);
horizontalScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                                 LayoutParams.FILL_PARENT));
horizontalScroll.addView(scroll);
contentView.addView(horizontalScroll);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 09:46:37

可以使用可以在xml文件中使用的自定义滚动视图代码。这是垂直滚动视图的一个小示例,在这里,您也可以使用水平视图

代码语言:javascript
运行
复制
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollview extends ScrollView {

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

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            Log.i("VerticalScrollview",
                    "onInterceptTouchEvent: DOWN super false");
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_MOVE:
            return false; // redirect MotionEvents to ourself

        case MotionEvent.ACTION_CANCEL:
            Log.i("VerticalScrollview",
                    "onInterceptTouchEvent: CANCEL super false");
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_UP:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false");
            return false;

        default:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action);
            break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction());
        return true;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20394286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档