以编程方式滚动ScrollView以聚焦按钮或视图 - Android

8
我有一个Activity,其中有一个包含125个按钮的ScrollView。这125个按钮是我的游戏中逐渐解锁的关卡(类似于Candy Crush Saga)。如果用户在第88个关卡上,那么现在用户必须手动滚动到第88个按钮。
我的问题是,如何使得当Activity加载时,它自动向下滚动并将ScrollView居中于某个按钮上?
以下是我创建和以编程方式填充按钮的代码。
ScrollView scrollView;

@Override
public void onCreate(Bundle savedInstanceState) {


    LinearLayout lay = (LinearLayout)findViewById(R.id.linearlayout);

    for(int i = 0; i < 125; i++) {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        params.weight = 1;
        params.gravity = Gravity.CENTER;

        if(i == 0) {
            TextView textview = new TextView(this);
            textview.setLayoutParams(params);
            textview.setTextSize(15);
            textview.setText(c.getResources().getString(R.string.l1to5));

            TextView textview2 = new TextView(this);
            textview2.setLayoutParams(params);
            textview.setTextSize(15);
            textview2.setText(c.getResources().getString(R.string.goal100));

            TextView textview3 = new TextView(this);
            textview3.setLayoutParams(params);
            textview.setTextSize(15);
            textview3.setText(c.getResources().getString(R.string.catRandom));

            if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_LARGE) {
                    textview.setTextSize(22);
                    textview2.setTextSize(22);
                    textview3.setTextSize(22);
            } else if((getResources().getConfiguration().screenLayout & 
                Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                    textview.setTextSize(22);
                    textview2.setTextSize(22);
                    textview3.setTextSize(22);
            }

            lay.addView(textview);
            lay.addView(textview2);
            lay.addView(textview3);     

            View ruler = new View(this);
            ruler.setBackgroundColor(0xFFC2BEBF);
            lay.addView(ruler, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
        }

        if(i == 5) {

 //  ....

}



        Button button = new Button(this);
        int _id = getResources().getIdentifier("b" + (i + 1), "id", this.getPackageName());
        button.setTag(_id);
        button.setLayoutParams(params);

        if((getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 
            Configuration.SCREENLAYOUT_SIZE_LARGE) {
                button.setTextSize(22);
        } else if((getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 
            Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                button.setTextSize(22);
        }

        button.setText("Level " + (i + 1));
        final int x = i + 1;
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                singleton.setLevelJustPlayed(x);
                // ...
                } else {
                 // ...
                    Intent intent = new Intent(Levels.this, Categories.class);
                    startActivity(intent);
                }
            }
        });

        lay.addView(button);

        if(singleton.getLevel() >= (i + 1)) {
            button.setEnabled(true);
        } else {
            button.setEnabled(false);
        }
    }
// end the onCreate() method

你尝试过使用scrollTo方法并将层的坐标传递给滚动视图吗? - Cole Busby
2个回答

25

尝试

yourScrollView.scrollTo(0, (int) button.getY());

啊,好的,那就是我在寻找的东西。但getY()是指什么? - Matt
getY() - 这应该足以找出按钮在滚动视图中的位置。 - trippedout
1
它可以正常运行,但是在使用smoothScrollBy时无法正常工作 => new Handler().post(new Runnable() { @Override public void run() { scrollView.smoothScrollBy(0, centreY); } }); - Zin Win Htet

2

针对Kotlin用户


your_scrollview.post( { your_scrollview.scrollTo(0, your_view_inside_SV.getY().toInt()) })

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