将上个问题所涉及的页面和代码全部显示出来
时间: 2024-09-23 14:05:33 浏览: 38
品优购案例,一个购物页面全部代码
由于上一个问题涉及到的内容较多,我会尽量提供一个简化版的示例,展示如何使用`FragmentPagerAdapter`和`Viewpager`配合一个按钮来进行`Fragment`切换。假设你有一个MainActivity、一个BaseFragment作为基础类,以及FirstFragment和SecondFragment作为具体的`Fragment`实例:
**MainActivity.java**
```java
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String[] FRAGMENTS = {"FirstFragment", "SecondFragment"};
private int currentFragmentIndex = 0;
private ViewPager viewPager;
private Button switchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
switchButton = findViewById(R.id.switch_button);
setupViewPager(viewPager);
setupSwitchButton();
viewPager.setAdapter(adapter);
}
private void setupViewPager(ViewPager viewPager) {
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
for (String tag : FRAGMENTS) {
adapter.addFragment(tag);
}
viewPager.setAdapter(adapter);
}
private void setupSwitchButton() {
switchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentFragmentIndex = (currentFragmentIndex + 1) % FRAGMENTS.length;
viewPager.setCurrentItem(currentFragmentIndex, true); // 切换到新Fragment
}
});
}
private class FragmentAdapter extends FragmentStatePagerAdapter {
private List<String> fragments = new ArrayList<>();
public FragmentAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
public void addFragment(String tag) {
fragments.add(tag);
}
@Override
public Fragment getItem(int position) {
return BaseFragment.createInstance(FRAGMENTS[position]);
}
@Override
public int getCount() {
return fragments.size();
}
}
}
```
**activity_main.xml**
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="scrollable"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/switch_button"
app:layout_constraintTop_toBottomOf="@id/tab_layout"/>
<Button
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
在这个例子中,我们没有显示具体的Fragment代码,因为它们通常是继承自BaseFragment并包含各自的视图和业务逻辑。对于`Fragment`间的通信,可以使用`setArguments()`和`getArguments()`来传递数据。
阅读全文