[英]How to change image in first fragment when I pressed back button from the second fragment
當我從第二個片段反壓時,我想在第一個片段中更改圖像(即@ + id / shower)(此片段已添加到viewpager),該片段被第一個片段的第二個孩子代替(相對布局,即@ + id / view_frame)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="@+id/frame_indicators"
android:layout_height="wrap_content"
cardview:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:paddingTop="@dimen/dp10"
android:paddingBottom="@dimen/dp10"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/shower"
android:layout_width="wrap_content"
android:gravity="center"
android:scaleType="centerCrop"
android:layout_gravity="center_horizontal"
android:src="@drawable/first_plan_indicator"
android:layout_height="wrap_content">
</ImageView>
<LinearLayout
android:layout_marginTop="@dimen/dp5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="@style/Widget.AppCompat.ButtonBar"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/navi_selector"
android:id="@+id/choose_plan_text"
android:text="Choose Plans"/>
<TextView
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/enter_details_text"
android:text="Enter Details"/>
<TextView
android:gravity="center"
android:id="@+id/pay_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/textcolor"
android:text="Payment"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="@+id/view_frame"
android:visibility="visible"
android:layout_below="@+id/frame_indicators"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/tabs_type"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="@dimen/dp10"
android:id="@+id/for_family"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="For Your Family"> </TextView>
<View
android:id="@+id/family_divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="@dimen/dp5"
android:layout_marginEnd="@dimen/dp5"
android:background="@color/navi_selector"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="@dimen/dp10"
android:textColor="@color/textcolor"
android:id="@+id/for_you"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="For You"> </TextView>
<View
android:id="@+id/your_divider"
android:visibility="gone"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="@dimen/dp5"
android:layout_marginEnd="@dimen/dp5"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/navi_selector"/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_marginTop="@dimen/dp5"
android:layout_below="@+id/tabs_type"
android:id="@+id/packageslist_recyclerview"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
和我的代碼在FIRST FRAGMET中,當我調用第二個片段(registerfragment)時,可以通過調用indicatorChangesNew()方法來更改圖像。
if (packagesList.getPackageID()==6)
{
planListFragmentNew.indicatorChangesNew(1);
}
else
{
planListFragmentNew.indicatorChangesNew(2);
}
FragmentManager fragmanger=mContext.getFragmentManager();
FragmentTransaction fragtrans;
fragtrans = fragmanger.beginTransaction();
Bundle b=new Bundle();
b.putDouble("planCost",packagesList.getPackageCost());
b.putString("planName",packagesList.getPackageName());
b.putString("paymode",packagesList.getPackageModeOfPay());
b.putInt("planId",packagesList.getPackageID());
b.putString("role",packagesList.getRolePatient());
RegisterFragment registerFragment=new RegisterFragment();
registerFragment.setArguments(b);
fragtrans.replace(R.id.view_frame, registerFragment);
fragtrans.addToBackStack(Constants.FRAG_TAG_REGISTER);
fragtrans.commit();
我的indicatorChangesNew()方法是
public void indicatorChangesNew(int checkValue)
{if (checkValue==1)
{ mChoosePlanText.setTextColor(getResources()
.getColor(R.color.textcolor));
mEnterPlanText.setTextColor(getResources()
.getColor(R.color.navi_selector));
mPayText.setTextColor(getResources()
.getColor(R.color.textcolor));
mShower.setImageDrawable(getResources()
.getDrawable(R.drawable.second_plan_indicator));
}
else if(checkValue==2)
{
mChoosePlanText.setTextColor(getResources().
getColor(R.color.textcolor));
mEnterPlanText.setTextColor(getResources()
.getColor(R.color.textcolor));
mPayText.setTextColor(getResources()
.getColor(R.color.navi_selector));
mShower.setImageDrawable(getResources()
.getDrawable(R.drawable.third_plan_indicator));
}
else{
mChoosePlanText.setTextColor(getResources()
.getColor(R.color.navi_selector));
mEnterPlanText.setTextColor(getResources()
.getColor(R.color.textcolor));
mPayText.setTextColor(getResources()
.getColor(R.color.textcolor));
mShower.setImageDrawable(getResources()
.getDrawable(R.drawable.first_plan_indicator));}
}
使用共享的首選項或單例類或僅使用靜態變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.