在Android中,ViewSwitcher本身不能直接嵌套多個。ViewSwitcher是一個容器,用于在兩個子視圖之間切換,它只支持兩個子視圖。如果你想要實現多個視圖之間的切換,可以使用嵌套的ViewFlipper或者使用ViewPager結合Fragment來實現。
<ViewFlipper android:id="@+id/viewFlipper" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="View 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="View 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="View 3" />
</ViewFlipper>
然后在Activity中設置切換動畫:
ViewFlipper viewFlipper = findViewById(R.id.viewFlipper);
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
首先,創建一個Fragment類,例如ViewPagerFragment
,并在其中添加一個子視圖:
public class ViewPagerFragment extends Fragment {
private static final String ARG_VIEW_TYPE = "view_type";
public static ViewPagerFragment newInstance(int viewType) {
ViewPagerFragment fragment = new ViewPagerFragment();
Bundle args = new Bundle();
args.putInt(ARG_VIEW_TYPE, viewType);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_pager, container, false);
switch (getArguments().getInt(ARG_VIEW_TYPE)) {
case 1:
view.setBackgroundColor(Color.RED);
break;
case 2:
view.setBackgroundColor(Color.GREEN);
break;
case 3:
view.setBackgroundColor(Color.BLUE);
break;
}
return view;
}
}
接下來,在Activity中設置ViewPager:
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在Activity中設置適配器:
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
setupViewPager();
}
private void setupViewPager() {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(ViewPagerFragment.newInstance(1), "View 1");
adapter.addFragment(ViewPagerFragment.newInstance(2), "View 2");
adapter.addFragment(ViewPagerFragment.newInstance(3), "View 3");
viewPager.setAdapter(adapter);
}
}
最后,創建一個適配器ViewPagerAdapter
,繼承自FragmentPagerAdapter
或FragmentStatePagerAdapter
:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
這樣,你就可以實現多個視圖之間的切換了。