如何将ScrollView添加到导航抽屉

时间:2019-11-16 03:44:31

标签: java android android-studio android-fragments 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>

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

screen 1

screen 2

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

error part 1

error part 2

error part 3/3

2 个答案:

答案 0 :(得分:1)

我认为您必须设置

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

答案 1 :(得分:1)

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

将滚动视图添加到单独的片段中,并合并到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>