Android - Problem with ViewFlipper
I am making a game that requires multiple views within an Activity, and i decided to use Viewflipper to do.
The thing is. I need to have 3 views in in the viewflipper, and the LAST view will transfer back to the first one.
My problem is that the buttons are acting weird, and they are either not going to the next, or skipping the third view. I tried to put vf.setDisplayedChild(R.Id.gamescreen1); at the last view but then the whole thing breaks down.
Thanks for all answers in advance, i have been stuck with this problem for DAYS! and yes, i know that i am a noob :(
[SOURCECODE] public class GameActivity extends 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 = (But开发者_JAVA百科ton) 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>
try this
flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);
if(flipper.getCurrentView() == 3)
{
flipper.setDisplayedChild(1);
}
setDisplayChild takes an integer parameter that is the zero based index of the child to display NOT the id of the child to display. A bit confusing I know.
精彩评论