从活动中调用一个片段

时间:2017-02-11 10:24:34

标签: android android-fragments callback

我是android开发的新手。 我有一个Activity和两个片段。在第二个片段(EditProfileFragment)中,我有一个名为UPDATE的联系信息更新按钮。 我想在点击更新按钮后,第一个片段(ProfileFragment)被替换为第二个片段(EditProfileFragment)。 在活动中我使用两个这样的片段:

private void initFragment() {
    fragments = new Vector<>();
    fragments.add(Fragment.instantiate(this, ProfileFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, EditProfileFragment.class.getName()));
    adapterFragment = new PagerAdapterFragment(this.getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.vpg_main_content);
    pager.setAdapter(adapterFragment);

}


private ProfileFragment getProfileFragment() {
    if (profileFragment == null) {
        profileFragment = (ProfileFragment) fragments.get(0);
    }
    return profileFragment;
}

private EditProfileFragment getEditProfileFragment() {
    if (editProfileFragment == null) {
        editProfileFragment = (EditProfileFragment) fragments.get(1);
    }
    return editProfileFragment;
}

我的Fragment适配器是:

public class PagerAdapterFragment extends FragmentPagerAdapter {
private final List<Fragment> fragments;
public PagerAdapterFragment(FragmentManager fm, 
List<Fragment> fragments)   {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}

这是我的更新声明:

       fragmentEditBtUpdate.setOnClickListener(
       new View.OnClickListener()   {
       @Override
       public void onClick(View v) {
       id = contact.get_id();
       name = fragmentEditEditTextName.getText().toString();
       family = fragmentEditEditTextFamily.getText().toString();
       phoneNumber = fragmentEditEditTextPhoneNumber.getText().toString();
       contact = new Contact(id, name, family, phoneNumber,
       mCurrentImagePath);
       rowUpdate =
       App.getInstanceImplementation().updateContact(contact);
            Log.i("==>", "btnUpdateContact: " + rowUpdate);
       editFragmentCallBack.setCurrentFragmentPosition();

        }
    });

我在EditProfileFragment中创建了callBack监听器:

    public interface EditFragmentCallBack 
    {
    Contact getContact();
    void finishProfile();
    void setCurrentFragmentPosition();
    }

并在活动中实施:

    @Override
public void setCurrentFragmentPosition() {
 getSupportFragmentManager().beginTransaction().replace(R.id.vpg_main_content,profileFragment);
}

这是活动布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/vpg_main_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

但是当我点击按钮并拨打editFragmentCallBack.setCurrentFragmentPosition();时,没有任何事情发生! 你有什么看法?

3 个答案:

答案 0 :(得分:0)

如果您使用beginTransaction()初始化了一项交易,则应按commit()

提交

所以使用这个

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
 fragmentTransaction.replace(R.id.vpg_main_content,profileFragment);
 fragmentTransaction.commit();

将其放入setCurrentFragmentPosition()并替换

getSupportFragmentManager().beginTransaction().replace(R.id.vpg_main_content,profileFragment);

修改

private void initFragment() {
    List<Fragments> fragments=new ArrayList<> //declare it as global
    fragments.add(new ProfileFragment.class);
    fragments.add(new EditProfileFragment.class);
    adapterFragment = new PagerAdapterFragment(this.getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.vpg_main_content);
    pager.setAdapter(adapterFragment);

}


private ProfileFragment getProfileFragment() {
    if (profileFragment == null) {
        profileFragment = (ProfileFragment) fragments.get(0);
    }
    return profileFragment;
}

private EditProfileFragment getEditProfileFragment() {
    if (editProfileFragment == null) {
        editProfileFragment = (EditProfileFragment) fragments.get(1);
    }
    return editProfileFragment;
}

参考this

答案 1 :(得分:0)

在你想要去片段

的Activity类中

活动类

 FragmentClass fragClass = FragmentClass.newInstance(You can send any data to fragment class);
 fragClass.show(getActivity().getFragmentManager(), "");

片段类

 public static FragmentClass newInstance(Get Any Data from Activity Class) {
    FragmentClass fragClass = new FragmentClass();

    return fragClass;
   }

答案 2 :(得分:0)

我对我的程序结构进行了一些更改和使用View Pager setCurrentItem()方法,我终于可以解决我的问题。这个链接对我很有帮助。 guides.codepath.com