我是 Android 编程的新手,现在我想为 2 个视图创建一个 ViewFlipper 或 ViewSwitcher,它们将在 1 列和 2 列之间切换视图。问题是第一个视图没问题,但是当我使用 showNext() 或 showPrevious() 时,第二个视图显示为空白,我完全没有想到哪里出了问题。我尝试过 ViewFlipper 和 ViewSwitcher 的结果相同。
layout_devicelistmainswitcher.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="Grid/List View"
android:id="@+id/viewbutton"
android:background="@drawable/viewicon"
android:layout_gravity="right" />
<ViewFlipper
android:id="@+id/vflipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:numColumns="1"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grid"
android:scrollbars="none" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp">
<GridView
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grid_l"
android:scrollbars="none" />
</LinearLayout>
</ViewFlipper>
主要活动.java
public class activity_devicelist extends AppCompatActivity {
GridView grid;
Boolean viewtype = true;
ViewFlipper switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_devicelistmainswitcher);
Button button = (Button) findViewById(R.id.viewbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity_devicelist.this, "Button Clicked", Toast.LENGTH_SHORT).show();
switcher = (ViewFlipper) findViewById(R.id.vflipper);
switcher.showPrevious();
}
});
}
}