如何将ScrollView添加到导航抽屉

我想将ScrollView添加到我的导航抽屉中,我首先尝试将线性布局作为滚动视图下的子项,但是当我打开片段时,导航抽屉显示在我的片段下。我想使片段保持不变,而导航抽屉现在确实显示在片段的后面,并且导航抽屉具有滚动视图。

抽屉代码:

public class drawer_home extends AppCompatactivity implements NavigationView.OnNavigationItemSelectedListener {


private DrawerLayout drawer;
Toolbar toolbar;

TextView mStartDate,mEndDate,mPlan,mDays;
FirebaseAuth firebaseAuth;
FirebaseUser user;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer_home);

    toolbar = findViewById(R.id.tb);
    mStartDate = findViewById(R.id.startDateTV);
    mEndDate = findViewById(R.id.endDateTV);
    mPlan = findViewById(R.id.planTV);
    mDays = findViewById(R.id.daysTV);

    setSupportactionBar(toolbar);
    /*TextView textView = (TextView)toolbar.findViewById(R.id.toolbartv);
    textView.setText("No Excusas");
    getSupportactionBar().setDisplayShowTitleEnabled(false);*/

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    actionbardrawertoggle toggle = new actionbardrawertoggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if(savedInstanceState == null) {

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new androidx.fragment.app.Fragment()).commit();
        navigationView.setCheckedItem(R.id.nav_homeMain);
    }
    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getcurrentUser();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Users");
    Query query = databaseReference.orderByChild("email").equalTo(user.getEmail());
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull Datasnapshot datasnapshot) {

            //checkc until requiered data get
            for (Datasnapshot ds : datasnapshot.getchildren()) {

                String startdate = "Fecha de Inicio = " + ds.child("Start Date").getvalue();
                String enddate = "Fecha de Salida = " + ds.child("End Date").getvalue();
                String plan = "Plan = " + ds.child("Plan").getvalue();
                String days = "Tu plan acaba en " + ds.child("Days Left").getvalue() +" dias ";


                mStartDate.setText(startdate);
                mEndDate.setText(enddate);
                mPlan.setText(plan);
                mDays.setText(days);


            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });

}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_homeMain:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new androidx.fragment.app.Fragment()).commit();
            break;
        case R.id.nav_qr:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new QRcodeFragment()).commit();
            break;
        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new UserProfile()).commit();
            break;
        case R.id.nav_order:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Ordersactivity()).commit();
            break;
        case R.id.nav_nutrition:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Nutritionactivity()).commit();
            break;
        case R.id.nav_chat:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Contactactivity()).commit();
            break;
        case R.id.nav_coach:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Coachesactivity()).commit();
            break;
        case R.id.nav_information:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new AboutUsactivity()).commit();
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onBackpressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackpressed();
    }


}

}

xml:

<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"
android:background="@drawable/homebcgself"
tools:context=".drawer_home"
tools:openDrawer="start">


<LinearLayout

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:popuptheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="@drawable/toolbar_bg"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.actionBar">

    <TextView
        android:id="@+id/toolbartv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="No Excusas"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#ffffff"
        android:textSize="40sp"
        android:textStyle="bold" />

    </androidx.appcompat.widget.Toolbar>


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/startDateTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="200dp"
                android:textColor="@color/colorWhite"
                android:textSize="30sp"
                android:textStyle="bold" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />


        <TextView
            android:id="@+id/endDateTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />


        <TextView
            android:id="@+id/planTV"
            android:layout_width="match_parent"
            android:textSize="30sp"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginStart="5dp"
            android:textColor="@color/colorWhite"/>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorWhite" />



    <TextView
        android:id="@+id/daysTV"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:textColor="@color/colorWhite"/>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />




</LinearLayout>




<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu" />


</androidx.drawerlayout.widget.DrawerLayout>

我想要的主要内容滚动示例:

如何将ScrollView添加到导航抽屉

如何将ScrollView添加到导航抽屉

主要内容可滚动时,显示在第一个片段下方

如何将ScrollView添加到导航抽屉

如何将ScrollView添加到导航抽屉

如何将ScrollView添加到导航抽屉

caojinwan 回答:如何将ScrollView添加到导航抽屉

我认为您必须设置

<com.google.android.material.navigation.NavigationView
    ...
    android:fitsSystemWindows="true"
    android:isScrollContainer="true"
    ... />
,

得到了很多帮助,并弄清楚了。

将滚动视图添加到单独的片段中,并合并到onCreate中。

'''

ScrollView

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">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/startDateTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="200dp"
        android:textColor="@color/colorWhite"
        android:textSize="30sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />

    <TextView
        android:id="@+id/endDateTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:textColor="@color/colorWhite"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />

    <TextView
        android:id="@+id/planTV"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:textColor="@color/colorWhite"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />

    <TextView
        android:id="@+id/daysTV"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginStart="5dp"
        android:textColor="@color/colorWhite"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorWhite" />

</LinearLayout>

本文链接:https://www.f2er.com/3091553.html

大家都在问