繁体   English   中英

Android-ViewFlipper问题

[英]Android - Problem with ViewFlipper

我制作的游戏在一个Activity中需要多个视图,因此我决定使用Viewflipper来做。

事情是。 我需要在viewflipper中具有3个视图,而LAST视图将转移回第一个视图。

我的问题是这些按钮的行为很奇怪,它们要么不转到下一个,要么不跳过第三个视图。 我试图把vf.setDisplayedChild(R.Id.gamescreen1); 在最后一个视图,然后整个事情崩溃了。

感谢您提前提出所有答案,我已经在DAYS遇到了这个问题! 是的,我知道我是菜鸟:(

[SOURCECODE]公共类GameActivity扩展了Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_game);


    final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);

    //SCREEN 1

    Button btnSTART = (Button) findViewById(R.id.btnSTART);

    //SCREEN 2

    Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);        

    //SCREEN 3

    Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);

//screen 1

    btnSTART.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

//screen 2 // Either button will go to view 3

    btnALT1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });

btnALT2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });


//screen 3 // Either button will go back to view 1

    btnALT3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();

        }        
    });

    btnALT4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            vf.showNext();
        }        
    });
}
}

[XML]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gamescreen1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
        <ListView android:layout_width="fill_parent" android:id="@+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:id="@+id/btnSTART" android:layout_width="200dp" android:text="@string/btnstart"></Button>
    </LinearLayout>

</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gamescreen2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
        <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list2"></ListView>
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT1"></Button>
        <Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT2"></Button>

    </LinearLayout>

</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gamescreen3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
    </LinearLayout>

    <LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT3"></Button>
        <Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/btnALT4"></Button>
    </LinearLayout>

</LinearLayout>

尝试这个

flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);

if(flipper.getCurrentView() == 3)
{
    flipper.setDisplayedChild(1);
}

setDisplayChild采用一个整数参数,该整数参数是该子项的从零开始的索引,以不显示该子项的ID。 我知道有点困惑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM