我有一个可以提交的表单并将数据发送到API,因此当我收到请求的回调时,我想隐藏表单并显示感谢视图。 这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:animateLayoutChanges="true">
<LinearLayout
android:id="@+id/thank_you_layout_rate"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_check_circle_green_800_48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto_medium"
android:gravity="center"
android:text="@string/thank_you_for_rating"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/DarkBlue"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/form_rating"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="0.1"
android:fontFamily="@font/roboto_medium"
android:gravity="center"
android:text="@string/rate_our_app"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/DarkBlue"
android:textStyle="bold" />
<RatingBar
android:id="@+id/ratingbar_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:isIndicator="false"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="?android:attr/listDivider" />
<EditText
android:hint="@string/rate_our_app"
android:id="@+id/comment_rate"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:inputType="textMultiLine"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/DarkBlue"
/>
<Button
android:id="@+id/save_rate"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:layout_weight="0.1"
android:background="@color/DecathlonBlue"
android:fontFamily="@font/roboto_medium"
android:gravity="center"
android:text="@string/save"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/LightGrey"
android:textAllCaps="true"/>
</LinearLayout>
</LinearLayout>
这是我的片段:
class RateFragment : Fragment(), RateFragmentListener {
private lateinit var mLoginManager: LoginManager
private lateinit var loadingDialog: LoadingDialog
private lateinit var ratePresenter: RatePresenter
private lateinit var mView: View
private lateinit var rateNotAllowed: LinearLayout
private lateinit var formRating: LinearLayout
companion object {
fun newInstance(loginManager: LoginManager): Fragment {
val currentFragment = RateFragment()
currentFragment.mLoginManager = loginManager
return currentFragment
}
}
override fun onResume() {
this.stopLoadingDialog()
super.onResume()
}
/**
*
* @param inflater LayoutInflater
* @param container ViewGroup?
* @param savedInstanceState Bundle?
* @return View?
*/
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
mView = inflater.inflate(R.layout.rate_fragment, container, false)
val saveButton = mView.findViewById<Button>(R.id.save_rate)
val starBar = mView.findViewById<RatingBar>(R.id.ratingbar_rate)
val commentRate = mView.findViewById<EditText>(R.id.comment_rate)
rateNotAllowed = mView.findViewById(R.id.thank_you_layout_rate)
formRating = mView.findViewById(R.id.form_rating)
loadingDialog = LoadingDialog(context!!)
startLoadingDialog()
initPresenter(activity as Activity, this)
saveButton.setOnClickListener {
this.startLoadingDialog()
ratePresenter.sendRating(
commentRate.text.toString(),
starBar.rating
)
}
return mView
}
/**
*
* @param activity Activity
*/
private fun initPresenter(
activity: Activity,
rateFragment: RateFragment
) {
ratePresenter = RatePresenter(activity, mLoginManager, rateFragment)
}
/**
* Loading dialog, block the UI
*/
override fun stopLoadingDialog() {
loadingDialog.dismiss()
}
/**
* Loading dialog, block de UI
*/
override fun startLoadingDialog() {
loadingDialog.show()
}
override fun rateSent() {
stopLoadingDialog()
}
override fun rateError() {
activity!!.toast("error on sending the rating")
stopLoadingDialog()
}
override fun rateNotAllowed() {
formRating.visibility = View.GONE
rateNotAllowed.visibility = View.VISIBLE
}
}
但是只有当我更改视图并返回视图时,视图的更新才会自动完成(演示者内部有一个if,用于检查是否发送了音符)。
谢谢你,问候