【问题标题】:Show fragment inside viewpager from another fragment在另一个片段的视图页面中显示片段
【发布时间】:2014-04-02 04:16:08
【问题描述】:

您好,我正在使用 3 个片段(片段 A、B、C)在 viewpager 和选项卡中开发一个 Android 应用程序,viewpager 工作正常。片段 A 包含一个列表视图,当用户单击一个项目时,应用程序会打开一个片段对话框,其中包含有关所选项目的信息。此对话框有一个名为“添加到收藏夹”的按钮。现在我想在用户按下按钮时这样做:

  1. 关闭片段对话框
  2. 在视图分页器中显示片段 B
  3. 将信息从对话片段发送到片段 B

我该怎么做?

这是我的代码的一部分:

* MainFragmentActivity *(这工作正常)

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

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });


    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            FragmentA a = new FragmentA();
            Bundle args1 = new Bundle();
            args1.putInt(FragmentA.ARG_SECTION_NAME , position + 1);
            a.setArguments(args1);
            return a;

        case 1:
            FragmentB b= new FragmentB();
            Bundle args2 = new Bundle();
            args2.putInt(FragmentB.ARG_SECTION_NAME , position + 2);
            b.setArguments(args2);
            return b;

        case 2:
            FragmentC c= new FragmentC();
            Bundle args3 = new Bundle();
            args3.putInt(FragmentC.ARG_SECTION_NAME , position + 3);
            c.setArguments(args3);
            return c;

          default:
              return null;
        }
    }

这是片段对话框

* FragmentDialogView *

public class FragmentDialogView extends DialogFragment implements OnClickListener {

private static final int REAUTH_ACTIVITY_CODE = 0;
private String videoId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    Bundle mArgs = getArguments();

    View view = (View) inflater.inflate(R.layout.fragment_dialog_view, container, false);

    //Buttons
    Button button = (Button) view.findViewById(R.id.button_one);
    button.setOnClickListener(this);
    buttonDownload.setOnClickListener(this);

    return view;
}

@Override
public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REAUTH_ACTIVITY_CODE) {

    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_one:

            //Here it should show the fragment B inside the viewpager

        break;
    default:                
        break;
   }

}
}

【问题讨论】:

    标签: android android-fragments android-viewpager


    【解决方案1】:

    要关闭 Dialog,请在您的 DialogFragment 类中包含以下内容

    private Dialog dialog;
    
    @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            dialog = new Dialog(getActivity());
            return dialog;
        }
    
        @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_one:
    
                dismiss();
    
            break;
        default:                
            break;
       }
    
    }
    

    并创建一个界面

    创建以下Communicator.java

    public interface Communicator {
        public void respond(int i);
    }
    

    在你的 MainAcitvity

    中实现这个 Communicator

    然后像这样在你的fragment中创建这个Communicatorinstance

    public class FragmentDialogView extends DialogFragment implements OnClickListener {
    
        private Communicator com;
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            com = (Communicator) getActivity();
            btn.setOnClickListener(this);
            }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
            case R.id.btn:
                com.respond(1);
                break;
            }
        }
    

    每当你单击那个按钮 它会将int发送到驻留在 MainActivity

    如下所示

    @Override
    public void respond(int i) {
    
    
            // Receive a bundle here
            // and pass the corresponding information to the FragmentB
            // here i'm receving an int and pass it to the FragmentB as a String
    
            FragmentManager fm = getFragmentManager();
            FragmentB fragment = (FragmentB) fm.findFragmentByTag("FragmentB");
            fragment.fromMainActivity(""+i);
    
            // If the above the one doesn't work keep the instance as Static and then try
    
            viewPager.invalidate();
            pagerAdapter.notifyDataSetChanged();
            viewPager.setCurrentItem(1, true);
    
        // Inside the setCuttentItem() method 0 first tab
        // 1 second tab
        // 2 third tab and so on
        } 
    

    我在这里收到一个 int 。您可以使用捆绑包来传递相应的信息。这将更改 viewPager 以显示下一个选项卡

    并在 FragmentB 中保留任何简单的方法,如下所示

    public void fromMainActivity(String sample) {
        Toast.makeText(getActivity(), sample, duration).show();
    }
    

    我希望这会有所帮助 :) 编码愉快

    【讨论】:

    • 感谢 Sha 工作正常,如果尝试传递信息我需要创建另一个实例到 FragmentB?如果我在 OnActivityCreated 创建一个 Bundle,这将得到 Null,因为当应用程序启动此片段的捆绑包时没有任何值
    • 是的。如果它不起作用,您需要创建一个实例,尝试将其设置为static。您将从DialogFrament 仪式创建捆绑包。因此,如果您传递任何内容,它就不会为空。否则检查 null @deleon 的条件
    【解决方案2】:

    1.试试这个:getDialog().dismiss();

    2.正如我理解的正确,在你的片段中创建一个这样的方法,

    public static FirstFragment newInstance(String text){
        FirstFragment f= new FirstFragment();
        return f;
    }
    

    在您的按钮中调用它onClick(),例如FirstFragment.newInstance("Fragment, Instance 1");

    3.使用 DialogFragment 中的方法创建 Interface 可以调用以将您想要的任何数据传递回创建所述 DialogFragment 的 Fragment。还将您的片段设置为目标,例如myFragmentDialog.setTargetFragment(this, 0)。然后在对话框中,使用 getTargetFragment() 获取目标片段对象并转换为您创建的接口。现在您可以使用((MyInterface)getTargetFragment()).methodToPassData(data) 传递数据。 欲了解更多信息:link

    【讨论】:

    • 我做到了...看看我的第一堂课。我对视图寻呼机中的片段没有问题,我的问题是当用户按下对话框片段中的按钮时如何显示位置 B(片段)
    • 您是否尝试过建议的解决方案,在您的按钮 onClick() 中调用给定的方法?
    • 我已经将它用于类似的场景。我的意思是,当您按下按钮时,创建片段 B 的新实例。你有什么错误吗?
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多