如何使用上一个和下一个按钮切换片段?

时间:2019-02-18 11:13:12

标签: android android-fragments

我有3个片段类。当我处于具有两个按钮(即“下一个”和“上一个”)的第一片段类时,“上一个”按钮应保持不可单击状态,然后在单击“下一个”按钮时,片段类应切换到第二个片段类,然后类似地单击“下一个”按钮,它应该切换到第3个片段类,但“下一个”按钮保持不可点击状态。

我的问题是单击该按钮后所有按钮均无法正常工作。当我进入首页时,“下一个”按钮不会切换到第二个片段类,也不会“上一个”按钮保持不可点击状态。

现在检查一下,我将按钮的文本颜色更改为白色,以使其感觉按钮不可单击。

 public class HomeFormViewActivity extends AppCompatActivity implements 
 FirstPage.OnFragmentInteractionListener, 
 SecondPage.OnFragmentInteractionListener, 
 ThirdPage.OnFragmentInteractionListener {

Button button1, button2;
public FrameLayout frameLayout;

public FirstPage firstPage;
public SecondPage secondPage;
public ThirdPage thirdPage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_form_view);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    button1 = findViewById(R.id.pre_button);
    button2 = findViewById(R.id.nxt_button);

    frameLayout = findViewById(R.id.frame_layout);

    firstPage = new FirstPage();
    secondPage = new SecondPage();
    thirdPage = new ThirdPage();

    setFragment(firstPage); // To display first fragment

    if (currentFragment() instanceof FirstPage){

        Toast.makeText(this, "You are in first page", Toast.LENGTH_SHORT).show();
        button1.setTextColor(getResources().getColor(R.color.LightGrey));
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setFragment(secondPage);
            }
        });
    }


     if (currentFragment() instanceof SecondPage){

        Toast.makeText(this, "You are in second page", Toast.LENGTH_SHORT).show();
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setFragment(thirdPage);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setFragment(firstPage);
            }
        });
    }

  if (currentFragment() instanceof ThirdPage){

        Toast.makeText(this, "You are in third page", Toast.LENGTH_SHORT).show();
        button2.setTextColor(getResources().getColor(R.color.LightGrey));
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setFragment(secondPage);
            }
        });
    }

}

public Fragment currentFragment() {

    return getSupportFragmentManager().findFragmentById(R.id.frame_layout);
}

private void setFragment(Fragment fragment) {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame_layout, fragment);
    fragmentTransaction.commit();
}

@Override
public void onFragmentInteraction(Uri uri) {

}

1 个答案:

答案 0 :(得分:2)

您的实例检查方法在错误的位置。在onCreate中调用setFragment时,需要花费一些时间来替换framelayout中的片段。其次,您每次替换片段时都不会运行检查条件。另外,您必须第一次添加片段,然后调用replace。

按钮点击应该是这样的:

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (currentFragment() instanceof FirstPage){
//Some code here
                   }else if (currentFragment() instanceof SecondPage){
//Some code here
    }
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (currentFragment() instanceof FirstPage){
//Some code here
                   }else if (currentFragment() instanceof SecondPage){
//Some code here

    }
            }
        });

我不擅长格式化。希望这有道理。