简体   繁体   English

如何将 ScrollView 添加到导航抽屉

[英]How to add ScrollView to Navigation Drawer

I want to add ScrollView to my navigation drawer, I first tried to but the linear layout as a child under Scrollview, but when I opened my fragment, the navigation drawer shows under my fragment.我想将 ScrollView 添加到我的导航抽屉中,我首先尝试将线性布局作为 Scrollview 下的子项,但是当我打开我的片段时,导航抽屉显示在我的片段下。 I want to have my fragments untouched, whereas the navigation drawer does now show behind the fragments, and the navigation drawer has scrollview.我想让我的片段保持不变,而导航抽屉现在确实显示在片段后面,并且导航抽屉有滚动视图。

drawer code:抽屉代码:

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

example of scrolling I want for the main content:我想要的主要内容的滚动示例:

屏幕 1

屏幕 2

when the main content is scrollable, it shows under the first fragment当主要内容可滚动时,它显示在第一个片段下方

错误部分 1

错误第 2 部分

错误部分 3/3

I think you have to set我认为你必须设置

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

Had a lot of help and figured it out.得到了很多帮助并想通了。

Add scrollview to separate fragment and incorporate in the onCreate.将滚动视图添加到单独的片段并合并到 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM