单击导航项时,我无法切换片段。我没有使用菜单项填充导航抽屉。
activity_dashboard layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/app_bar_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#23282e"
android:fitsSystemWindows="true"
android:visibility="gone"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@mipmap/login"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:src="@mipmap/nav_user_pic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sunil Nepali"
android:textColor="#fff"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Student"
android:textColor="#fff"
android:textSize="10dp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008b8b"
android:paddingBottom="11dp"
android:paddingLeft="20dp"
android:paddingTop="11dp"
android:text="INSTITUTION"
android:textColor="#fff" />
<LinearLayout
android:id="@+id/dashboard_layout"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/dashboard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Dashboard"
android:textColor="#fff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#23282e" />
<LinearLayout
android:id="@+id/calender_layout"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/calander" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Calendar"
android:textColor="#fff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#23282e" />
<LinearLayout
android:id="@+id/view_fee"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/fee" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Fee"
android:textColor="#fff" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008b8b"
android:paddingBottom="11dp"
android:paddingLeft="20dp"
android:paddingTop="11dp"
android:text="ACADEMICS"
android:textColor="#fff" />
<LinearLayout
android:id="@+id/class_schedule"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/class_schedule" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Class Schedule"
android:textColor="#fff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#23282e" />
<LinearLayout
android:id="@+id/progress_report"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/progess_report" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Progress Report"
android:textColor="#fff" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008b8b"
android:paddingBottom="11dp"
android:paddingLeft="20dp"
android:paddingTop="11dp"
android:text="ACCOUNT"
android:textColor="#fff" />
<LinearLayout
android:id="@+id/view_profile"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/view_profile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="View Profile"
android:textColor="#fff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#23282e" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/settings" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Settings"
android:textColor="#fff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#23282e" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#2e353d"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="3dp"
android:src="@mipmap/logout" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Log Out"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
家庭课程
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
LinearLayout dashboard, calendar, classSchedule, progressReport, profile, fee;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
calendar = (LinearLayout) findViewById(R.id.calender_layout);
classSchedule = (LinearLayout) findViewById(R.id.class_schedule);
profile = (LinearLayout) findViewById(R.id.view_profile);
fee = (LinearLayout) findViewById(R.id.view_fee);
progressReport = (LinearLayout) findViewById(R.id.progress_report);
// calendar.setOnClickListener(this);
// classSchedule.setOnClickListener(this);
// profile.setOnClickListener(this);
// fee.setOnClickListener(this);
// progressReport.setOnClickListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setVisibility(View.VISIBLE);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container, new Dashboard())
.commit();
if (id == R.id.dashboard_layout) {
fragmentManager.beginTransaction()
.replace(R.id.container, new Dashboard())
.commit();
} else if (id == R.id.view_fee) {
fragmentManager.beginTransaction()
.replace(R.id.container, new Fee())
.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我的信息中心片段
public class Dashboard extends Fragment {
public Dashboard() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
}
如何解决这个问题?
答案 0 :(得分:1)
如果您没有使用菜单项,则需要直接定义textview或Layour,无论您在菜单抽屉中定义了什么,然后单击该特定项目,如下所示。
这是您的一个线性布局ID是view_fee。 处理该特定视图的单击事件并打开片段
var ModalController = function($scope) { ... }
答案 1 :(得分:1)
你需要使用文本视图点击而不是使用menuItems你没有在menu.xml中添加任何菜单项它不会真正起作用你需要尝试这样,
public class Home extends AppCompatActivity implements View.OnClickListener {
LinearLayout calendar, classSchedule, progressReport, profile, fee;
android.support.v4.app.FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
calendar = (LinearLayout) findViewById(R.id.calender_layout);
classSchedule = (LinearLayout) findViewById(R.id.class_schedule);
profile = (LinearLayout) findViewById(R.id.view_profile);
fee = (LinearLayout) findViewById(R.id.view_fee);
progressReport = (LinearLayout) findViewById(R.id.progress_report);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.accept, R.string.appbar_scrolling_view_behavior);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setVisibility(View.VISIBLE);
TextView textView1 = (TextView) findViewById(R.id.cal);
textView1.setOnClickListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.cal) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Dashboard())
.commit();
}
}
}
您需要为要点击的所有textview设置ID
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Calendar"
android:id="@+id/cal"
android:textColor="#fff" />