Using ViewFlipper with onclick to switch views

≯℡__Kan透↙ 提交于 2020-01-06 19:56:48

问题


I'm using ViewFLipper with Random.nextInt(). to change layouts onClick (Button). Now I have 3 xml layouts. 1_view.xml 2_view.xml 3_view.xml. Starting from 1_view.xml I have a button there. When button clicked I should get a random layout. it works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks a button on (1_view.xml), I want them to go to the layouts I have left (2_view.xml and 3_view.xml).

Codes

Main.xml

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:id="@+id/TVLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="left|top"
            android:text="0"
            android:textColor="#4CB848"
            android:textSize="40sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TVRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/TVLeft"
            android:gravity="right"
            android:text="0"
            android:textColor="#FF0000"
            android:textSize="40sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/1_View"
            layout="@layout/1_view" />

        <include
            android:id="@+id/2_View"
            layout="@layout/2_view" />

        <include
            android:id="@+id/3_View"
            layout="@layout/3_view" />
    </ViewFlipper>
</LinearLayout>

Main.java
        // FlipperView
        MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

        TVRight = (TextView) findViewById(R.id.TVRight);
        TVLeft = (TextView) findViewById(R.id.TVLeft);

        // 1_view.xml
        Q1_btn1 = (Button) findViewById(R.id.btn_1);
        Q1_btn2 = (Button) findViewById(R.id.btn_2);
        Q1_btn3 = (Button) findViewById(R.id.btn_3);
        Q1_btn4 = (Button) findViewById(R.id.btn_4);

        Q1_btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }

        });

        Q1_btn2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Rightnum++;
                RightResult.setText(Integer.toString(Rightnum));

                Random RandomView = new Random();
                MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));

            }
        });
        Q1_btn3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });
        Q1_btn4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });

回答1:


You can do this in your code:

 Random RandomView = new Random();
 int nextViewIndex = RandomView.nextInt(3);
 while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
     nextViewIndex = RandomView.nextInt(3);
 }
 MyViewFlipper.setDisplayedChild(nextViewIndex);

Basically just call Random.nextInt() until it doesn't match current viewFlipper's index.

To only show a viewFlipper once randomly:

// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
   vfIndices.add(i);
}

// when button is clicked
if (vfIndices.size() == 0) {
   return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);

The idea is use an ArrayList to keep track of the viewFlipper index that has not been shown. When button is clicked, pick an index randomly from this array and set the viewFlipper to. Then remove that index from the ArrayList. Next time the button is clicked, it will show one of the remaining flippers.



来源:https://stackoverflow.com/questions/11442287/using-viewflipper-with-onclick-to-switch-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!