英文:
How to properly set layout visibility from another activity?
问题
public class MainActivity extends AppCompatActivity {
private static BottomNavigationView navView;
private LinearLayout createTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createTransaction = findViewById(R.id.create_transaction_layout);
navView = findViewById(R.id.nav_view);
addBadgeView();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
private void addBadgeView() {
navView = findViewById(R.id.nav_view);
navView.getOrCreateBadge(R.id.navigation_transactions); // Show badge
}
public void hideFAB() {
createTransaction.setVisibility(View.GONE);
}
}
private MainActivity mainActivity;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
walletsViewModel =
ViewModelProviders.of(this).get(WalletsViewModel.class);
View root = inflater.inflate(R.layout.fragment_wallets, container, false);
mainActivity.hideFAB();
return root;
}
英文:
I have a LinearLayout in my BottomNavgigation's MainActivity, and I'm trying to hide the layout from one of the fragments, but I keep getting a nullpointer exception, and i'm quite certain everything is set right
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@color/colorGrey"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<LinearLayout
android:id="@+id/create_transaction_layout" \\view I'm trying to set visibility on
android:layout_width="match_parent"
android:layout_height="@dimen/margin_padding_size_60"
android:layout_gravity="bottom"
android:layout_marginBottom="@dimen/margin_padding_size_24"
android:gravity="center"
android:visibility="visible">
<LinearLayout
android:id="@+id/send_receive_layout"
android:layout_width="wrap_content"
android:layout_height="@dimen/margin_padding_size_40"
android:background="@drawable/card_bg"
android:elevation="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/fab_send"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_bg"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:paddingStart="@dimen/margin_padding_size_16"
android:paddingEnd="@dimen/margin_padding_size_16">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_communication_call_made"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:text="Send"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
app:fontFamily="@font/source_sans_pro" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="24dp"
android:background="@color/colorPrimary" />
<LinearLayout
android:id="@+id/fab_receive"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_padding_size_40"
android:background="@drawable/card_bg"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:paddingStart="@dimen/margin_padding_size_16"
android:paddingEnd="@dimen/margin_padding_size_16">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_communication_call_received"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:text="Receive"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
app:fontFamily="@font/source_sans_pro" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static BottomNavigationView navView;
private LinearLayout createTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createTransaction = findViewById(R.id.create_transaction_layout);
navView = findViewById(R.id.nav_view);
addBadgeView();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
private void addBadgeView() {
navView = findViewById(R.id.nav_view);
navView.getOrCreateBadge(R.id.navigation_transactions); // Show badge
}
public void hideFAB() {
createTransaction.setVisibility(View.GONE);
}
}
and in my Fragment I try setting visibility like this
WalletFragment.java
private MainActivity mainActivity;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
walletsViewModel =
ViewModelProviders.of(this).get(WalletsViewModel.class);
View root = inflater.inflate(R.layout.fragment_wallets, container, false);
mainActivity.hideFAB();
return root;
}
please what could I have done wrong? as everything seems fine to me
答案1
得分: 1
在你的片段内部,你可以调用
((MainActivity) requiredActivity()).hideFAB()
英文:
Inside your fragment you can call
((MainActivity) requiredActivity()).hideFAB()
答案2
得分: 1
使用 getActivity
获取托管活动的实例,将其强制转换为其类,并然后调用公共方法。
( (MainActivity) getActivity() ).hideFAB()
英文:
Use getActivity to get the instance of hosting activity, cast it to its class and then call the public methods.
( (MainActivity) getActivity() ).hideFAB()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论