【问题标题】:ScrollView inside a fragment with TabLayout is not scrolling?带有 TabLayout 的片段内的 ScrollView 不滚动?
【发布时间】:2018-08-12 12:27:31
【问题描述】:

我正在使用TabLayout 进行注册和登录。 signup 表单的高度会很长,但由于某些原因,我无法在 fragment 中添加 ScrollView

这是我正在做的事情:https://imgur.com/9p42i43

这是我的布局主要活动

<android.support.constraint.ConstraintLayout 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:background="@color/appBackground"
    tools:context=".LoginActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/secondaryColor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/logo_view"
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:background="@color/secondaryColor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar2" />

    <android.support.design.widget.TabLayout
        android:id="@+id/signin_signup_tab_lay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/secondaryColor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/logo_view"
        app:tabBackground="@drawable/curve_login"
        app:tabIndicatorColor="@color/yellow_btn"
        app:tabSelectedTextColor="@color/primaryColor"
        app:tabTextAppearance="@style/tabstyle"
        app:tabTextColor="#7d98c3">

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signin_signup_tab_lay"></android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

Java 主活动

package com.example.mobilestyx.fantasy_arena;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;

public class LoginActivity extends AppCompatActivity {

    TabLayout tabLayout;
    private int[] tabIcons = {
            R.drawable.curve_login,
            R.drawable.curve_login
    };

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


        tabLayout = (TabLayout) findViewById(R.id.signin_signup_tab_lay);
        tabLayout.addTab(tabLayout.newTab().setText("Signin"));
        tabLayout.addTab(tabLayout.newTab().setText("Signup"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //
        // setupTabIcons();

        final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(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) {

            }
        });

    }
    private void setupTabIcons() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);

        tabLayout.getTabAt(1).setCustomView(tabTwo);
    }}

这是我的片段布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/constraint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    tools:context=".SignupFragment">

    <!-- TODO: Update blank fragment layout -->


        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/login_fb_btn"
            android:fontFamily="@font/mavenpro_regular_font"
            android:text="Signup with Facebook"
            android:textColor="#ffffff"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:fontFamily="@font/mavenpro_regular_font"
            android:gravity="center"
            android:text="OR"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button3" />

        <android.support.v7.widget.CardView
            android:id="@+id/cardView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            app:cardCornerRadius="5dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2">

            <EditText
                android:id="@+id/signup_name_et"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/startborder_input"
                android:ems="10"
                android:fontFamily="@font/mavenpro_regular_font"
                android:hint="John Doe"
                android:inputType="textPersonName"
                android:paddingLeft="15dp" />
        </android.support.v7.widget.CardView>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="22dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="32dp"
            android:text="Full Name"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="32dp"
            android:text="Email"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView2" />

        <android.support.v7.widget.CardView
            android:id="@+id/cardView3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            app:cardCornerRadius="5dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3">

            <EditText
                android:id="@+id/signup_email_et"
                style="@style/MavenproEdittextStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/startborder_input"
                android:ems="10"
                android:hint="johndoe@mail.com"
                android:inputType="textEmailAddress"
                android:paddingLeft="15dp" />
        </android.support.v7.widget.CardView>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="32dp"
            android:text="Phone"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView3" />

        <android.support.v7.widget.CardView
            android:id="@+id/cardView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            app:cardCornerRadius="5dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4">

            <EditText
                android:id="@+id/signup_phone_et"
                style="@style/MavenproEdittextStyle"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/startborder_input"
                android:ems="10"
                android:hint="000-000-0000"
                android:inputType="number"
                android:paddingLeft="15dp"
                android:textColor="#000000" />
        </android.support.v7.widget.CardView>

        <TextView
            android:id="@+id/textView5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="32dp"
            android:text="Gender"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView4" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView5">

            <RadioButton
                android:id="@+id/signup_gender_m_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Male" />

            <RadioButton
                android:id="@+id/signup_gender_f_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Female" />
        </RadioGroup>

        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="32dp"
            android:text="DOB"
            android:textColor="#000000"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="28dp"
            android:layout_marginEnd="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView6">

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="date" />
        </android.support.v7.widget.CardView></android.support.constraint.ConstraintLayout>

Java 片段

package com.example.mobilestyx.fantasy_arena;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class SignupFragment extends Fragment {


    public SignupFragment() {
        // Required empty public constructor
    }



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


}

【问题讨论】:

  • ScrollView 的布局应该会崩溃。你确定你正在寻找正确的东西吗?
  • @MikeM。是的,伙计,它崩溃了,我正在做一些我忘记删除的事情。
  • 那么真正的问题是什么?崩溃了?或者一旦运行就无法滚动?
  • @Siddheshdamble 我在你的布局中找不到ScrollView 顺便说一句。请准确解释真正的问题。
  • 我的主要问题是我无法让滚动视图工作。

标签: android android-fragments scroll android-tablayout


【解决方案1】:

在使用约束布局时,尝试使用 NestedScrollView 进行滚动。

【讨论】:

    【解决方案2】:

    确保您的 ScrollView 对父级有约束,即每个边的 ConstraintLayout 视图。

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 2023-04-08
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多