使用TabLayout的片段内的ScrollView不滚动,为什么?

时间:2016-02-02 15:21:11

标签: java android xml android-fragments scrollview

我是Android开发中的一个菜鸟,我在密码学的入门课程中,所以我决定结合两个并制作这个应用程序。我使用TabLayout为每个密码方法(RSA和XOR)创建一个选项卡,并使用一个选项卡指示和帮助。一切都运行良好,直到我在帮助选项卡中添加了一个scrollView与说明。 scrollView不滚动,我不知道为什么,我一直在网上寻求帮助,并做每个可行的解决方案,如更改宽度和高度ScrollView属性,我卡住了,你能帮我吗? Screenshots

-----编辑3/02/16 -----

**我的main_activity.xml **

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="com.example.usuario.chatparritos.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            app:tabMode="fixed"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            tools:context="net.voidynullness.android.tabitytabs.TabLayoutActivity"
                                           android:background="#ffffff"/>

    </android.support.design.widget.AppBarLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_info"/>

</android.support.design.widget.CoordinatorLayout>

----- //编辑3/02/16 -----

这是我的tab_fragment_3.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:focusableInTouchMode="true"
            android:focusable="true"
            android:scrollbars="vertical"
            android:id="@id/scrollView">

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:background="#FFFFFF"
                  android:layout_marginLeft="20dp"
                  android:layout_marginRight="20dp"
                  android:gravity="left">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_chatparritos"
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/body_chatparritos"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="9dp"
                android:background="@drawable/blue_cornered_textview"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/link_khan"
                android:layout_marginTop="-19dp"
                android:textAppearance="@style/TextAppearance.Design.Hint"
                android:textSize="15dp"
                android:linksClickable="true"
                android:id="@+id/linkAcademy"
                android:layout_gravity="center_horizontal"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_xor"
                android:textSize="20dp"
                android:layout_marginTop="25dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/body_xor"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="9dp"
                android:background="@drawable/blue_cornered_textview"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/instructions_xor"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/yellow_cornered_textview"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_rsa"
                android:textSize="20dp"
                android:layout_marginTop="25dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/body_rsa"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="9dp"
                android:background="@drawable/blue_cornered_textview"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/link_rsa"
                android:layout_marginTop="-19dp"
                android:textAppearance="@style/TextAppearance.Design.Hint"
                android:textSize="15dp"
                android:linksClickable="true"
                android:id="@+id/linkRSA"
                android:layout_gravity="center_horizontal"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/instructions_rsa"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/yellow_cornered_textview"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_numeros"
                android:textSize="20dp"
                android:layout_marginTop="25dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/body_numeros"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="9dp"
                android:background="@drawable/blue_cornered_textview"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/nota"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                android:layout_marginTop="30dp"
                android:layout_marginBottom="30dp"
                android:background="@drawable/red_cornered_textview"/>

    </LinearLayout>
</ScrollView>

这是我的TabFragment3.java

public class TabFragment3 extends Fragment {
    TextView linkAcademy;
    TextView linkRsa;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_fragment_3, container, false);


        linkAcademy = (TextView) view.findViewById(R.id.linkAcademy);
        linkAcademy.setMovementMethod(LinkMovementMethod.getInstance());

        linkRsa = (TextView) view.findViewById(R.id.linkRSA);
        linkRsa.setMovementMethod(LinkMovementMethod.getInstance());


        return view;
    }

我的PagerAdapter.java

public class PagerAdapter extends FragmentStatePagerAdapter {

    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int numOfTabs) {
        super(fm);
        this.mNumOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TabFragment1 tab1 = new TabFragment1();
                return tab1;
            case 1:
                TabFragment2 tab2 = new TabFragment2();
                return tab2;
            case 2:
                TabFragment3 tab3 = new TabFragment3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }

以及我的MainActivity.java

public class MainActivity extends AppCompatActivity {





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("XOR"));
        tabLayout.addTab(tabLayout.newTab().setText("RSA"));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_help));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                               @Override
                                               public void onTabSelected(TabLayout.Tab tab) {
                                                   viewPager.setCurrentItem(tab.getPosition());
                                               }

                                               @Override
                                               public void onTabUnselected(TabLayout.Tab tab) {

                                               }

                                               @Override
                                               public void onTabReselected(TabLayout.Tab tab) {

                                               }
                                           });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, R.string.fab_info, Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

1 个答案:

答案 0 :(得分:9)

主要问题出在main_activity.xml。尝试复制我从this question and answers获得的这个布局,让我们看看会发生什么:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="24dp"
        app:fabSize="normal"
        app:borderWidth="0dp"
        app:elevation="6dp"
        android:src="@drawable/ic_done"/>

</android.support.design.widget.CoordinatorLayout>

在某些情况下,在ScrollView中使用CoordinatorLayout可能会导致屏幕无法滚动。所以请改用NestedScrollView