在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);
}
}
这样,你就可以实现多个视图之间的切换了。