片段按钮内的动作侦听器不起作用

时间:2019-11-06 18:18:03

标签: android android-layout android-fragments layout-inflater

我正在使用“主页”,“仪表板”和“通知”选项卡进行底部选项卡式活动。现在,默认情况下,android studio以片段的形式提供这些选项卡。我在fragment_home.xml中有一个按钮,但当前视图设置为activity_main.xml。fragment类中的button.onClick Listener似乎不起作用。该如何解决?

主类...

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    View cView = getLayoutInflater().inflate(R.layout.cutom_toolbar, null);
    actionBar.setCustomView(cView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()){
        case R.id.menu1:
            Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
            break;
        case R.id.menu2:
            Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.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"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:background="@drawable/image"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"
    grid:alignmentMode="alignBounds"
    grid:columnCount="2"
    grid:rowOrderPreserved="false"
    grid:useDefaultMargins="true">

    <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="@drawable/image1"
        app:itemIconTint="@color/color"
        app:itemTextColor="@color/color"
        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>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid2="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.gridlayout.widget.GridLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        grid2:alignmentMode="alignBounds"
        grid2:columnCount="2"
        grid2:rowOrderPreserved="false">

        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:text="TextView"
            grid2:layout_column="0"
            grid2:layout_row="0">

            <LinearLayout
                android:id="@+id/l1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical"
                android:text="Lighting"
                android:textSize="21sp"
                android:textStyle="italic"
                grid2:layout_column="0"
                grid2:layout_gravity="fill"
                grid2:layout_row="0">

                <Button
                    android:id="@+id/imageView2"
                    android:layout_width="80dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:background="@drawable/light2"
                    grid2:layout_gravity="left" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="All Off"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>

    </androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

片段类...

public class Fragment1 extends Fragment implements View.OnClickListener{

public Button button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    button = view.findViewById(R.id.imageView2);
    button.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View view) {
    button.setVisibility(View.GONE);
}
}

2 个答案:

答案 0 :(得分:0)

不清楚。如果我说对了, 在您的片段中声明一个全局校验。

public Button button;

将其初始化为onViewCreated()

您的主要活动

if(fragment.button != null){
   //fire action
}

答案 1 :(得分:0)

fragment_home.xml中声明的view元素将需要与Fragment代码而非活动进行交互。

使用NavHost意味着您的大多数代码将位于单个片段中,而不位于托管片段的活动中。

如有必要,您可以使用界面在两者之间进行通信。 您还可以使用共享的ViewModel进行通信-使片段中的onClick侦听器在MainActivity所预订的ViewModel中设置一个Observable。