我是android开发中的新成员。 我有一个活动和两个fragments.in第二个片段(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;
}
和我的片段适配器:
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创建回调监听:
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();
,没有任何反应! 您的意见是什么?
你想从活动中移动到片段吗? – Sasi
@sasikumar不,我想去活动 –
@sayreskabir的另一个片段你解决了问题? –