簡體   English   中英

在另一個片段中顯示viewpager中的片段

[英]Show fragment inside viewpager from another fragment

您好,我正在使用Viewpager和標簽內的3個片段(片段A,B,C)開發一個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;
   }

}
}

要關閉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

像這樣在您的片段中創建此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();
}

希望對您有所幫助:)

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。 還要將Fragment設置為目標,例如myFragmentDialog.setTargetFragment(this, 0) 然后在對話框中,使用getTargetFragment()獲取目標片段對象,並將其轉換為您創建的接口。 現在您可以使用((MyInterface)getTargetFragment()).methodToPassData(data) 有關更多信息: 鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM