【发布时间】:2021-06-11 07:57:33
【问题描述】:
我有一个片段,我需要在点击按钮时显示进度条,并在成功完成任务后移除进度条。
这是我的片段类的 onCreateView 方法
private RelativeLayout view;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container,
@Nullable Bundle savedInstanceState) {
RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.fragment_note, container, false);
view = root;
FloatingActionButton add = root.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage(context);
displayProgressBar(true);
}
});
hideKeyboard();
return root;
}
这是我用来显示和隐藏进度条的方法。但是当我试图隐藏进度条时它不起作用。
private void displayProgressBar(boolean visible) {
RelativeLayout layout = new RelativeLayout(getContext());
ProgressBar progressBar = new ProgressBar(MainActivity.getActivity(),null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
if (visible) {
layout.addView(progressBar, params);
MainActivity.getActivity().setContentView(layout);
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.INVISIBLE);
MainActivity.getActivity().setContentView(view);
}
}
我已经尝试在我的片段的 xml 中添加 progressBar,但它对我来说并不是很有效。 这是片段 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout android:id="@+id/note_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme"
android:orientation="vertical">
<EditText android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="@dimen/fab_margin_medium"
android:background="@android:color/transparent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Double tap to type"
android:gravity="top"
android:textCursorDrawable="@drawable/color_cursor"
android:inputType="textMultiLine"
android:id="@+id/taskText" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin_small"
android:background="@color/colorPrimaryLight"
android:backgroundTint="@color/colorPrimaryLight"
android:outlineSpotShadowColor="@color/colorPrimaryLight"
android:outlineAmbientShadowColor="@color/colorPrimaryLight"
app:borderWidth="0dp"
android:src="@drawable/plus" />
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</RelativeLayout>
我是 Android 新手。任何帮助表示赞赏。 提前致谢。
编辑: 我尝试通过以下方式重置片段视图:
MainActivity.getActivity().setContentView(view);
它给了我以下错误: 指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。]
编辑2: 主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
setContentView(R.layout.main_activity);
setBottomNavigation();
savedState = savedInstanceState;
}
public void setBottomNavigation() {
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_files)
.build();
NavController navController = Navigation.findNavController(MainActivity.this,
R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
navView.setVisibility(View.VISIBLE);
}
和 MainActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
我会选择 Binding Adapter 来实现这一点。见here
标签: android android-layout android-fragments