我正在开发一个测验应用程序,其中我有一个HomeFragment包含一个带有DrawerLayout的NavigationView。我已经创建了一个单独的类“NavigationHelpline”用于绑定布局,并在HomeFragment上实现了OnClickListener。但是,onClick不起作用,即使我添加了日志,我也看不到任何日志。
每当我点击四个帮助热线布局中的一个时,它不会显示日志,而是关闭导航抽屉。
下面是HomeFragment代码:
public class HomeFragment extends Fragment implements View.OnClickListener{
private DrawerLayout drawerLayout;
private NavigationView navigationViewRight;
private NavigationHelpline navigationHelpline;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
drawerLayout = binding.drawerLayout;
navigationViewRight = binding.navViewRight;
//To remove default behaviour of navigation view
navigationViewRight.setNavigationItemSelectedListener(null);
navigationHelpline = new NavigationHelpline(navigationViewRight);
// Set OnClickListener for Helpline buttons
navigationHelpline.setHelplineButtonsOnClickListener(this);
return root;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_audience_poll:
// Handle audience poll button click
Toast.makeText(getContext(), "btn_audience_poll clicked", Toast.LENGTH_SHORT).show();
Log.d("NavView", "btn_audience_poll clicked");
break;
case R.id.btn_fifty_fifty:
Toast.makeText(getContext(), "btn_fifty clicked", Toast.LENGTH_SHORT).show();
Log.d("NavView", "btn_fifty clicked");
break;
case R.id.btn_change_question:
// Handle change question button click
Toast.makeText(getContext(), "btn_change_question clicked", Toast.LENGTH_SHORT).show();
Log.d("NavView", "change question clicked");
break;
case R.id.btn_ask_the_expert:
// Handle ask the expert button click
Toast.makeText(getContext(), "btn_ask the expert clicked", Toast.LENGTH_SHORT).show();
Log.d("NavView", "btn_ask the expert clicked");
break;
}
}
}
下面是来自home_fragment.xml和nav_layout.xml文件的相关代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end"
tools:context=".ui.home.HomeFragment">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_layout" />
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
这里是nav_laout.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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:id="@+id/nav_view_right_layout">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/btn_audience_poll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<ImageView
android:id="@+id/ivAudiencePoll"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/audience"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Audience Poll"
android:textStyle="bold"
android:textSize="19sp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/btn_fifty_fifty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<ImageView
android:id="@+id/ivFiftyFifty"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/fifty"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Fifty Fifty"
android:textStyle="bold"
android:textSize="19sp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/btn_change_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<ImageView
android:id="@+id/ivChangeQuestion"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/change_ques"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Flip The Question"
android:textStyle="bold"
android:textSize="19sp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/btn_ask_the_expert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<ImageView
android:id="@+id/ivAskTheExpert"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/ask_the_expert"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:padding="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Ask The Expert"
android:textStyle="bold"
android:textSize="19sp"
android:layout_gravity="center"/>
</LinearLayout>
<TextView
android:id="@+id/btn_back_to_home"
android:layout_width="match_parent"
android:layout_height="25sp"
android:text="Back to Question"
android:textSize="16sp"
android:layout_marginTop="10sp"
android:textColor="@color/white"
android:textStyle="bold"
android:background="@drawable/elliptical_border"
android:gravity="center"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
下面是NavigationHelpline类
public class NavigationHelpline {
private NavigationView navigationView;
private LinearLayout audiencePollBtn, fiftyFiftyBtn, flipQuestionBtn, askTheExpertBtn;
private TextView btnBackToHome;
public NavigationHelpline(NavigationView navigationView) {
this.navigationView = navigationView;
View includedLayout = navigationView.findViewById(R.id.nav_view_right_layout);
audiencePollBtn = includedLayout.findViewById(R.id.btn_audience_poll);
fiftyFiftyBtn = includedLayout.findViewById(R.id.btn_fifty_fifty);
flipQuestionBtn = includedLayout.findViewById(R.id.btn_change_question);
askTheExpertBtn = includedLayout.findViewById(R.id.btn_ask_the_expert);
btnBackToHome = includedLayout.findViewById(R.id.btn_back_to_home);
}
public void setHelplineButtonsOnClickListener(View.OnClickListener listener) {
audiencePollBtn.setOnClickListener(listener);
fiftyFiftyBtn.setOnClickListener(listener);
flipQuestionBtn.setOnClickListener(listener);
askTheExpertBtn.setOnClickListener(listener);
btnBackToHome.setOnClickListener(listener);
}
}
我已经尝试添加日志来调试该问题,但看不到任何日志。我还尝试删除NavigationView的默认行为,并为帮助热线按钮设置OnClickListener。但onClick方法仍无法按预期工作。
有人能帮助我理解为什么onClick方法不起作用以及如何修复它吗?
1条答案
按热度按时间ycl3bljg1#
我认为当你使用include标签在home_fragment.xml中添加新的布局时,实现点击监听器是不可能的,因为导航视图的默认行为是关闭导航视图内或外的任何视图上的导航视图抽屉。所以不要使用布局,而是使用带有项目的菜单,然后将每个项目设置为audience_poll,fifty_fifty,ask_the_expert,flip_the_question。