Java 在使用底部导航的同时在屏幕部分实现滑动功能?

Java 在使用底部导航的同时在屏幕部分实现滑动功能?,java,android,xml,android-fragments,swipe,Java,Android,Xml,Android Fragments,Swipe,作为大学项目的一部分,我正在开发我的第一个Android应用程序。我对java比较陌生。我的应用程序包含一个底部导航,其中包含4个单独的页面。在这些页面中,我有一个标题部分,包含标题、联系按钮和注销按钮 在头部下方,我希望有一个中间部分,它本身包含3个包含信息的滑动面板。基本上,每页应该有3个面板,有4个页面(4组3个滑动面板) 这就是我遇到困难的地方。。我不确定如何嵌套碎片,以便只有中间部分可以滑动。以下是我迄今为止的截图: 以下是我的MainActivity.java类的代码: impor

作为大学项目的一部分,我正在开发我的第一个Android应用程序。我对java比较陌生。我的应用程序包含一个底部导航,其中包含4个单独的页面。在这些页面中,我有一个标题部分,包含标题、联系按钮和注销按钮

在头部下方,我希望有一个中间部分,它本身包含3个包含信息的滑动面板。基本上,每页应该有3个面板,有4个页面(4组3个滑动面板)

这就是我遇到困难的地方。。我不确定如何嵌套碎片,以便只有中间部分可以滑动。以下是我迄今为止的截图:

以下是我的MainActivity.java类的代码:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    private static final String SELECTED_ITEM = "arg_selected_item";

    private BottomNavigationView mBottomNav;
    private int mSelectedItem;

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

    mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
    mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            selectFragment(item);
            return true;
        }
    });

    MenuItem selectedItem;
    if (savedInstanceState != null) {
        mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
        selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
    } else {
        selectedItem = mBottomNav.getMenu().getItem(0);
    }
    selectFragment(selectedItem);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(SELECTED_ITEM, mSelectedItem);
    super.onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
    MenuItem homeItem = mBottomNav.getMenu().getItem(0);
    if (mSelectedItem != homeItem.getItemId()) {
        // select home item
        selectFragment(homeItem);
    } else {
        super.onBackPressed();
    }
}

private void selectFragment(MenuItem item) {
    Fragment frag = null;
    // init corresponding fragment
    switch (item.getItemId()) {
        case R.id.menu_home:
            frag = MenuFragment.newInstance(getString(R.string.text_home),
                    getString(R.string.test_home));
            break;
        case R.id.menu_notifications:
            //Call to a function that does the stuff here
            frag = MenuFragment.newInstance(getString(R.string.text_notifications),
                    getString(R.string.test_notifications));
            break;
        case R.id.menu_search:
            frag = MenuFragment.newInstance(getString(R.string.text_search),
                    getString(R.string.test_search));
            break;
        case R.id.menu_followed:
            frag = MenuFragment.newInstance(getString(R.string.text_follow),
                    getString(R.string.test_follow));
    }

    // update selected item
    mSelectedItem = item.getItemId();

    // uncheck the other items.
    for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
        MenuItem menuItem = mBottomNav.getMenu().getItem(i);
        menuItem.setChecked(menuItem.getItemId() == item.getItemId());
    }

    if (frag != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container, frag, frag.getTag());
        ft.commit();
    }
}
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MenuFragment extends Fragment {
private static final String ARG_TEXT = "arg_text";
private static final String ARG_TESTSTRING = "arg_testString";

private String mText;
private String mTest;

private View mContent;
private TextView mTextView;
private TextView mTestView;
private Button contactButton;
private Button logoutButton;

public static Fragment newInstance(String text, String testString) {
    Fragment frag = new MenuFragment();
    Bundle args = new Bundle();
    args.putString(ARG_TEXT, text);
    args.putString(ARG_TESTSTRING, testString);
    frag.setArguments(args);
    return frag;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_menu, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // retrieve text and color from bundle or savedInstanceState
    if (savedInstanceState == null) {
        Bundle args = getArguments();
        mText = args.getString(ARG_TEXT);
        mTest = args.getString(ARG_TESTSTRING);
    } else {
        mText = savedInstanceState.getString(ARG_TEXT);
        mTest = savedInstanceState.getString(ARG_TESTSTRING);
    }

    // initialize views
    mContent = view.findViewById(R.id.fragment_content);
    mTextView = (TextView) view.findViewById(R.id.text);
    mTestView = (TextView) view.findViewById(R.id.test);
    contactButton = (Button) view.findViewById(R.id.contactButton);
    logoutButton = (Button) view.findViewById(R.id.logoutbutton);

    // set text and background color
    mTextView.setText(mText);
    mTestView.setText(mTest);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString(ARG_TEXT, mText);
    outState.putString(ARG_TESTSTRING, mTest);
    super.onSaveInstanceState(outState);
}
}
以下是fragments_menu.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.socialivemusic.socialivetestproject.MainActivity">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#ff292929">
</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:layout_weight="0.06"
    android:background="#ff000000"
    android:textAlignment="center"
    design:menu="@menu/bottom_nav_items" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.socialivemusic.socialivetestproject.MenuFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_marginLeft="17dp"
    android:layout_marginRight="17dp"
    android:layout_height="150dp"
    android:layout_marginTop="15dp"
    android:background="#ff434343"
    android:weightSum="1">

    <Button
        android:text="CONTACT"
        android:textColor="#ffffff"
        android:layout_width="420dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:id="@+id/contactButton"
        android:layout_weight="0.33"
        android:background="@drawable/round_button"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:textColor="#969696"
        android:textSize="40sp"
        android:textStyle="bold"
        android:layout_weight="0.355"
    />
    <Button
        android:text="LOGOUT"
        android:textColor="#ffffff"
        android:layout_width="420dp"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:id="@+id/logoutbutton"
        android:layout_weight="0.33"
        android:background="@drawable/logout_button"
        android:textSize="20sp"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_marginLeft="17dp"
    android:layout_marginRight="17dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="185dp"
    android:background="#ff434343">
    <TextView
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

</RelativeLayout>

上述文件中的第二个线性布局应为滑动面板。项目中唯一的其他文件是处理底部导航栏本身布局的xml文件,因此与我遇到的麻烦无关


有人对如何处理这个问题有什么建议吗?如何开始这个项目的这一部分的帮助将是惊人的!提前谢谢

您应该看看ViewPager s。它们使在碎片之间滑动变得很容易:@DamianJäger非常感谢,我现在有了这种滑动功能。我想将其设置为在bottomNav的一个页面上有3个滑动面板,但在其他页面上有更多/更少的面板。但是,当我更改页面时,应用程序崩溃,我得到以下错误:PagerAdapter在没有调用PagerAdapter的情况下更改了适配器的内容#notifyDataSetChanged知道如何解决此问题吗?我目前有一个变量进入getCount(),用于计算帧数。如果没有任何代码,我无法帮助您