使用位置将活动移动到片段

时间:2015-07-29 05:17:31

标签: android android-fragments

我使用了两个图像按钮来执行onClick以移动到片段页面。活动的工作流程:GridActivity1-> HomeActivity-> ShortFragment和DetailFragment。

为此,我设置了一个移动HomeActivity的位置,然后调用侦听器方法来移动适当的片段。

GridActivity1.java:

  int position;

  case R.id.btn_tour:

                Intent i1=new Intent(GridActivity1.this,MainActivity.class);


                i1.putExtra("position", position);
                startActivity(i1);
                break;

            case R.id.btn_contact:

                Intent i2=new Intent(GridActivity1.this,MainActivity.class);

                i2.putExtra("position", position);
                startActivity(i2);
                break;

MainActivity.java:

public class MainActivity extends ActionBarActivity implements ArticleSelectedListener {

 int position;

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

 position= getIntent().getExtras().getInt("position");

  if(position==0){

           onArticleSelected(2, "Short Fragment");

       }
        if(position==1){

           onArticleSelected(10, "Detail Fragment");
       }

 @Override
    public void onArticleSelected(int position, String content)
    {
 if(position==2)
        {
            action_bar_hometext.setText(content);
            FragmentManager manager = getFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            ShortFragment newFragment = new ShortFragment();
            ft.replace(R.id.realtabcontent, newFragment);
       //     ft.addToBackStack(null);
            ft.commit();
        }

  if(position==10)
        {
            action_bar_hometext.setText(content);
            FragmentManager manager = getFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            DetailFragment newFragment = new DetailFragment();
            ft.replace(R.id.realtabcontent, newFragment);
      //      ft.addToBackStack(null);
            ft.commit();
        }

我唯一的问题是,对于ShortFragment它工作正常。我在Detail Fragment类中做过的事情,它没有用。

我不知道如何设置第二个片段的位置。

任何人都可以帮助我。谢谢。

3 个答案:

答案 0 :(得分:1)

检查GridActivity1.java位置变量。

做到这一点

案例R.id.btn_tour:

            Intent i1=new Intent(GridActivity1.this,MainActivity.class);


            i1.putExtra("position", 0);
            startActivity(i1);
            break;

        case R.id.btn_contact:

            Intent i2=new Intent(GridActivity1.this,MainActivity.class);

            i2.putExtra("position", 1);
            startActivity(i2);
            break;

答案 1 :(得分:0)

试试此代码

if(position==0){

       onArticleSelected(position, "Short Fragment");

   }
    if(position==1){

       onArticleSelected(position, "Detail Fragment");
   }

@Override
public void onArticleSelected(int n, String content)
{
if(n==0)
    {
        action_bar_hometext.setText(content);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ShortFragment newFragment = new ShortFragment();
        ft.replace(R.id.realtabcontent, newFragment);
   //     ft.addToBackStack(null);
        ft.commit();
    }

if(n==1)
    {
        action_bar_hometext.setText(content);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        DetailFragment newFragment = new DetailFragment();
        ft.replace(R.id.realtabcontent, newFragment);
  //      ft.addToBackStack(null);
        ft.commit();
    }

答案 2 :(得分:0)

您在GridActivity1中的位置始终设置为零,因为原因只有ShortFragment正在运行而不是DetailFragment。