gpt4 book ai didi

android - 导航到其他 fragment 时如何在滚动后显示底部应用栏?

转载 作者:行者123 更新时间:2023-11-29 00:58:30 25 4
gpt4 key购买 nike

我在我的应用程序中实现了底部应用栏。我可以在滚动时隐藏底部应用栏,但是当我导航到其他 fragment 时它仍然隐藏?如何以编程方式使底部应用栏可见?

根据 Material 设计文档,我在底部应用栏中定义了以下属性。

app:hideOnScroll="true"

在 Fragment 中,我将滚动内容放在 NestedScrollView 中,如下所示:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Admin.AddUpdateCustomerFragment"
android:orientation="vertical"
android:transitionName="transtion_name_example"
>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayout">


</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>

AdminActivity.java

public class AdminActivity extends AppCompatActivity {
public BottomAppBar bottomAppBar;
public FloatingActionButton floatingActionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
getWindow().setEnterTransition(null);
floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
bottomAppBar = (BottomAppBar) findViewById(R.id.bar);
setSupportActionBar(bottomAppBar);
new CustomerFragment().setArguments(Bundle.EMPTY);

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new CustomerFragment())
.commit();
}

public boolean loadFragment(Fragment fragment, Bundle bundle) {
if (fragment != null) {
fragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();

return true;
}
return false;
}
}

CustomerFragment.java

public class CustomerFragment extends Fragment {
View rootView;

public CustomerFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_customer_fragment_admin, container, false);

((AdminActivity) getActivity()).bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_CENTER);
((AdminActivity) getActivity()).floatingActionButton.setImageResource(R.drawable.ic_add_black_24dp);
((AdminActivity) getActivity()).floatingActionButton.setColorFilter(Color.WHITE);

((AdminActivity) getActivity()).floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AdminActivity) getActivity()).loadFragment(new AddUpdateCustomerFragment(), Bundle.EMPTY);
}
});
return rootView;
}

AddUpdateCustomerFragment.java

public class AddUpdateCustomerFragment extends Fragment {
View rootView;

public AddUpdateCustomerFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_add_update_customer, container, false);

((AdminActivity) getActivity()).bottomAppBar.setNavigationIcon(null);
((AdminActivity) getActivity()).bottomAppBar.setFabAlignmentMode(FAB_ALIGNMENT_MODE_END);

btnBack = (ImageView) rootView.findViewById(R.id.back);

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AdminActivity) getActivity()).loadFragment(new CustomerFragment(), Bundle.EMPTY);
}
});
return rootView;
}
}

最佳答案

这是解决您的问题的方法。

OnScrollChangeListener 附加到您的 NestedScrollView 以切换您的 BottomNavBar

的可见性
scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

if (scrollY > oldScrollY) {
Log.i(TAG, "Scroll DOWN");
mBottomNavBar.setVisibility(View.GONE);
}
if (scrollY < oldScrollY) {
Log.i(TAG, "Scroll UP");
mBottomNavBar.setVisibility(View.VISIBLE);
}

if (scrollY == 0) {
Log.i(TAG, "TOP SCROLL");
}

if (scrollY == ( v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight() )) {
Log.i(TAG, "BOTTOM SCROLL");
}
}
});

在 fragment 的 onCreateView() 中,切换 BottomNavigationBarVisibility 并隐藏它。

@Override
protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//...
bottomNavBar.setVisibility(View.VISIBLE)
}

或者在启动新的 Fragment 时执行相同的操作:

 btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AdminActivity) getActivity()).loadFragment(new CustomerFragment(), Bundle.EMPTY);
mBottomNavBar.setVisibility(View.VISIBLE);
}
});

并且不要忘记删除 app:hideOnScroll="true"

关于android - 导航到其他 fragment 时如何在滚动后显示底部应用栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52822806/

25 4 0