大家好我在frament中编辑文本时遇到问题。重新打开此片段后,此elemtn保存文本。如果我执行此步骤,它可以重现
开放活动 - >打开FragmentA - >打开FragmentB(其中包含edittext) - > backPress(并再次打开FragmentA) - >打开FragmentB(再次)
在这个步骤之后,如果我在某些文本之前键入它再次填写它。我在代码中添加了这个:writeReviewText.setText("");
和xml:android:text=""
,但没有任何帮助。看起来片段的状态是保存。
这里是xml的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/app_bar_gradient"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/greyReview">
<ScrollView
android:id="@+id/scrollReview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/recomendRev"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_weight="1">
<ImageButton
android:id="@+id/leftIb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:src="@drawable/review_recomend_left_selector" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:alpha="0.5"
android:src="@drawable/good" />
<TextView
android:id="@+id/leftTv"
style="@style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:background="@android:color/transparent"
android:text="@string/recomend"
android:textColor="@color/fiolet" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="@+id/noRecomendRev"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_weight="1">
<ImageButton
android:id="@+id/rightIb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:src="@drawable/review_recomend_right_selector" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|bottom"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:alpha="0.5"
android:src="@drawable/bad" />
<TextView
android:id="@+id/rightTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:background="@android:color/transparent"
android:text="@string/dontRecomended"
android:textColor="@color/fiolet" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<EditText
android:id="@+id/writeReviewText"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="@android:color/white"
android:hint="@string/dummyReview"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:text=""
android:maxLines="4" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@color/darkGreyReview">
<include
android:id="@+id/photosDummy"
layout="@layout/add_photo_dummy"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<android.support.v7.widget.RecyclerView
android:id="@+id/photosRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:visibility="gone" />
</FrameLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
这是我的片段类的代码:
public class WriteReviewFragment extends Fragment implements Constants, View.OnClickListener, ExpandableListView.OnChildClickListener, CameraInterface {
MainActivity mActivity;
View self;
EditText writeReviewText;
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(TAG, "onCreateView");
self = inflater.inflate(R.layout.write_review_layout, container, false);
mActivity = ((MainActivity) getActivity());
font = DialogsHelper.getRLight(getActivity().getApplicationContext());
user = mActivity.getUser();
width = DialogsHelper.getDisplayWidth(mActivity);
currentlocale = getResources().getConfiguration().locale;
return self;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onViewCreated");
super.onViewCreated(view, savedInstanceState);
Toolbar tv = (Toolbar) view.findViewById(R.id.toolbar);
TextView tbTv = (TextView) tv.findViewById(R.id.tolbarTv);
TextView post = (TextView) tv.findViewById(R.id.post);
back = (ImageView) tv.findViewById(R.id.back);
post.setTypeface(font);
tbTv.setTypeface(font);
tags = (RecipientEditTextView) view.findViewById(R.id.chipsView);
tags.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
tags.setAdapter(new BaseRecipientAdapter(getActivity()));
tags.setTypeface(font);
musicExLv = (ExpandableListView) self.findViewById(R.id.expandableListView);
peopleAVGExLv = (ExpandableListView) self.findViewById(R.id.expandableListViewAVG);
peopleIm = (ImageView) self.findViewById(R.id.peopleIm);
musicIm = (ImageView) self.findViewById(R.id.musicIm);
photosDummy = (RelativeLayout) self.findViewById(R.id.photosDummy);
photosRv = (RecyclerView) self.findViewById(R.id.photosRv);
avgpeople = (TextView) self.findViewById(R.id.avgpeople);
musictype = (TextView) self.findViewById(R.id.musictype);
faceControl = (TextView) self.findViewById(R.id.faceControl);
dresCode = (TextView) self.findViewById(R.id.dresCode);
details = (TextView) self.findViewById(R.id.details);
tagsTv = (TextView) self.findViewById(R.id.tags);
rightTv = (TextView) self.findViewById(R.id.rightTv);
leftTv = (TextView) self.findViewById(R.id.leftTv);
writeReviewText = (EditText) self.findViewById(R.id.writeReviewText);
noRecomendRev = (ImageButton) self.findViewById(R.id.rightIb);
recomendRev = (ImageButton) self.findViewById(R.id.leftIb);
dresleftIb = (ImageButton) self.findViewById(R.id.dresleftIb);
dresRightIb = (ImageButton) self.findViewById(R.id.dresRightIb);
faceleftIb = (ImageButton) self.findViewById(R.id.faceleftIb);
faceRightIb = (ImageButton) self.findViewById(R.id.faceRightIb);
faceRightTv = (TextView) self.findViewById(R.id.faceRightTv);
faceleftTv = (TextView) self.findViewById(R.id.faceleftTv);
dresrightTv = (TextView) self.findViewById(R.id.dresrightTv);
dresleftTv = (TextView) self.findViewById(R.id.dresleftTv);
post.setOnClickListener(this);
back.setOnClickListener(this);
dresleftIb.setOnClickListener(this);
dresRightIb.setOnClickListener(this);
faceleftIb.setOnClickListener(this);
faceRightIb.setOnClickListener(this);
noRecomendRev.setOnClickListener(this);
recomendRev.setOnClickListener(this);
photosDummy.setOnClickListener(this);
peopleIm.setOnClickListener(this);
musicIm.setOnClickListener(this);
writeReviewText.setTypeface(font);
avgpeople.setTypeface(font);
musictype.setTypeface(font);
faceControl.setTypeface(font);
dresCode.setTypeface(font);
details.setTypeface(font);
tagsTv.setTypeface(font);
rightTv.setTypeface(font);
leftTv.setTypeface(font);
faceRightTv.setTypeface(font);
faceleftTv.setTypeface(font);
dresrightTv.setTypeface(font);
dresleftTv.setTypeface(font);
StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
photosRv.setLayoutManager(sglm);
//set same size as images preview
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, width / screenPart);
Log.e(TAG, "photosDummy" + (width / screenPart));
photosDummy.setLayoutParams(lp);
if (getArguments() != null) {
Bundle b = getArguments();
if (b.containsKey("foursquare_id")) {
foursquare_id = b.getString("foursquare_id").toString();
} else {
Toast.makeText(getActivity(), "FSQ Id is empty, you can`t post anything", Toast.LENGTH_SHORT).show();
}
}
//change state of arrow marker
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
musicExLv.setIndicatorBoundsRelative(width - GetPixelFromDips(30), width - GetPixelFromDips(10));
peopleAVGExLv.setIndicatorBoundsRelative(width - GetPixelFromDips(30), width - GetPixelFromDips(10));
} else {
musicExLv.setIndicatorBounds(width - GetPixelFromDips(30), width - GetPixelFromDips(10));
peopleAVGExLv.setIndicatorBounds(width - GetPixelFromDips(30), width - GetPixelFromDips(10));
}
prepareMusicList();
preparePeopleList();
writeReviewText.setText("");
writeReviewText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
reviewAdd.setReview_text(s.toString());
}
});
}
在这里编码我如何在活动中打开片段:
public void SetCurentFragment(final int index, int direction, boolean addToBackStack, int type, Bundle bundle) {
if (currentIndex != 5) {
previosIndex = currentIndex;
}
if (index == 1 || index == 2)
DeleteAllFragmentsBackstack();// need for detect if google map is initialize and already in backstack
Fragment fragment;
fragment = getSupportFragmentManager().findFragmentByTag(ReadReviewFragment.class.getClass().getSimpleName());
if (fragment == null) {
if (fragment == null) {
fragment = fragmentArray[index];
}
} else {
fragment = fragmentArray[index];
}
Log.e(TAG, "Open fragment is " + fragment);
if (fragment.getArguments() != null) {
fragment.getArguments().clear(); //for clear arguments if they saved from previous open of fragment(happens when open photos fragment)
}
if (bundle != null) {
if (fragment.getArguments() == null) {
fragment.setArguments(bundle);
} else {
fragment.getArguments().clear();
fragment.setArguments(bundle);
}
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
if (addToBackStack) {
transaction.addToBackStack(fragment.getTag());
} else {
transaction.addToBackStack(null);
}
transaction.commitAllowingStateLoss();
Log.e(TAG, "Transaction commit");
currentIndex = index;
if (index == 0 || index == 1 || index == 2) {
SetVisualTabs(index);
}
}
我正在调试我的代码,当我打开FragmentB时,bundle是空的。而fragmentArray的片段数组如:
public final Fragment[] fragmentArray = new Fragment[]{
new FragmentA(),//0
new FragmentB(),//1