我是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的另一個片段你解決了問題? –