java如何在查看最后和第一个片段时使后退/下一步按钮不可见
我正在尝试使用viewpager和fragments创建一个多页表单。表单需要有底部的点和点左右两侧的“后退”/“下一步”按钮,下一步按钮在最后一张幻灯片上更改为“完成”
我设法让底部的点和按钮起作用。然而,我不知道如何让后退按钮在第一个片段和最后一个片段的下一个按钮时不可见
我的代码:
主要活动。爪哇
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.viewpager.widget.ViewPager;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.view.View;
import com.google.安卓.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
getTabs();
}
public void getTabs(){
final ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
new Handler().post(new Runnable() {
@Override
public void run() {
viewPagerAdapter.addFragment(oneFragment.getInstance(),null); // this line can cause crashes
viewPagerAdapter.addFragment(twoFragment.getInstance(),null); // this line can cause crashes
viewPagerAdapter.addFragment(threeFragment.getInstance(),null); // this line can cause crashes
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
}
//Implement the listeners for the back and previous items
public void goBack(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem()-1);
}
public void goNext(View view) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
}
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:orientation="vertical"
tools:context=".MainActivity">
<安卓x.viewpager.widget.ViewPager
安卓:id="@+id/viewPager"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"/>
<安卓x.constraintlayout.widget.ConstraintLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true">
<Button
安卓:id="@+id/back"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
安卓:onClick="goBack"/>
<Button
安卓:id="@+id/next"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
安卓:onClick="goNext"/>
<com.google.安卓.material.tabs.TabLayout
安卓:id="@+id/tabLayout"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@drawable/tab_selector"
app:tabIndicatorHeight="0dp" />
</安卓x.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
ViewPagerAdapter。爪哇
import java.util.ArrayList;
import java.util.List;
import 安卓x.annotation.NonNull;
import 安卓x.annotation.Nullable;
import 安卓x.fragment.app.Fragment;
import 安卓x.fragment.app.FragmentManager;
import 安卓x.fragment.app.FragmentStatePagerAdapter;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragmentList = new ArrayList<>(); // this line can cause crashes
private List<String> stringList = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return stringList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment); // this line can cause crashes
stringList.add(title);
}
}
片段示例:oneFragment。布局为第一行的java
import 安卓.content.Context;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓x.annotation.NonNull;
import 安卓x.annotation.Nullable;
import 安卓x.fragment.app.Fragment;
public class OneFragment extends Fragment {
public static OneFragment getInstance(){
OneFragment oneFragment = new OneFragment();
return oneFragment;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.row_one, container,false);
return view;
}
}
# 1 楼答案
您可以将
addOnPageChangeListener
listener添加到ViewPager,并在onPageSelected()
回调中将所选/当前position
与第一个(位置=0)或最后一个位置进行比较另外,初始化FragmentPagerAdapter的正确方法是从
getItem()
返回新的片段实例。您应该永远不要将实际的片段引用传递给FragmentPagerAdapter,并且不应该将它们作为列表保存