Scrollview无法处理片段

时间:2018-04-07 18:50:36

标签: java android android-fragments android-scrollview

我有一个注册片段,其根布局为ScrollView,孩子为LinearLayout。我在LinearLayout内编辑了文本。但是,当我点击EditText并尝试滚动视图时,它无法正常工作。

要打开此片段,我必须单击登录片段的注册视图。

这是我的SignUpFragment.java

public class SignUpFragment extends Fragment {

    private EditText etFirstName, etLastName, etEmail, etPassword, etcity, etMobile;
    private String firstName, lastName, email, password, city, mobile;

    private Button proceed;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sign_up, container, false);
    }

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

        proceed.setOnClickListener(v -> {
            getStrings();
            if (checkFields()) {
                Bundle bundle = new Bundle();
                bundle.putString("firstName", firstName);
                bundle.putString("lastName", lastName);
                bundle.putString("email", email);
                bundle.putString("password", password);
                bundle.putString("mobile", mobile);
                bundle.putString("city", city);

                jumpToDeliverAddress(bundle);
            }
        });
    }

    private void jumpToDeliverAddress(Bundle bundle) {
        Fragment fragment = new DeliveryAddressFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    private boolean checkFields() {
        if (firstName.trim().isEmpty()) {
            etFirstName.setError("Please enter a valid first name");
            return false;
        }
        if (lastName.trim().isEmpty()) {
            etLastName.setError("Please enter a valid last name");
            return false;
        }
        if (email.trim().isEmpty() || (!isEmailValid())) {
            etEmail.setError("Please enter a valid email");
            return false;
        }
        if (password.trim().length() < 6) {
            etPassword.setError("Password must contain 6 digits");
            return false;
        }
        if (mobile.trim().length() != 10) {
            etMobile.setError("Please enter a valid mobile number");
            return false;
        }
        if (city.trim().isEmpty()) {
            etcity.setError("Please enter a valid city");
            return false;
        }

        return true;
    }

    private boolean isEmailValid() {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

    private void getStrings() {
        firstName = Constants.getString(etFirstName);
        lastName = Constants.getString(etLastName);
        email = Constants.getString(etEmail);
        password = Constants.getString(etPassword);
        city = Constants.getString(etcity);
        mobile = Constants.getString(etMobile);
    }

    private void findViews(View view) {
        etFirstName = view.findViewById(R.id.firstName);
        etLastName = view.findViewById(R.id.lastName);
        etEmail = view.findViewById(R.id.email);
        etPassword = view.findViewById(R.id.password_edittext);
        etcity = view.findViewById(R.id.city);
        etMobile = view.findViewById(R.id.mobile_number_edittext);
        proceed = view.findViewById(R.id.proceed);
    }
}

fragment_sign_up.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".fragments.SignUpFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/firstName"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="First name"
                android:inputType="textPersonName" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/lastName"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Last name"
                android:inputType="textPersonName" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Email"
                android:inputType="textEmailAddress" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/password_edittext"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Password"
                android:inputType="textPassword"
                android:maxLength="10" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/mobile_number_edittext"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Mobile number"
                android:inputType="number"
                android:maxLength="10" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/city"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="@string/city" />

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

        <Button
            android:id="@+id/proceed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="Proceed to add delivery address" />
    </LinearLayout>
</ScrollView>

请注意,目前,我的流程是: -

Mainactivity加载LoginFragment - &gt;点击注册 - &gt; signUpfragment打开。

如果我直接从MainActivity打开注册片段,滚动就可以了。

MainActivity.java

public class MainActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener, DrawerLocker {
public static SharedPreferences sharedPreferences;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
private Toolbar toolbar;
private VolleyCallback volleyCallback;

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

    drawerLayout = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.nav_view);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    initDrawer();

    if (checkToken())
        jumpToHome();
    else
        jumpToLogin();


}

private void jumpToLogin() {
    Fragment loginFragment = new LoginFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, loginFragment);
    fragmentTransaction.commit();

}

private void jumpToHome() {
    Fragment homeFragment = new HomeFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, homeFragment);
    fragmentTransaction.commit();

}

private boolean checkToken() {
    APICall apiCall = new APICall(volleyCallback, this);
    String token = apiCall.getTokenFromLocal(this);
    return !token.isEmpty();


}

private void initDrawer() {


    toggle = new ActionBarDrawerToggle(this,
            drawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close);


    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            drawerLayout.openDrawer(GravityCompat.START);
        }
    });


    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    drawerLayout = findViewById(R.id.drawer_layout);
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.nav_manage_address)
        jumpToManageAddress();
    else if (id == R.id.nav_book_order)
        jumpToBookOrder();

    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

private void jumpToBookOrder() {
    Fragment bookOrderFragment = new BookOrderFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, bookOrderFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

private void jumpToManageAddress() {
    Fragment manageAddressFragment = new NewAddressFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, manageAddressFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

@Override
public void setDrawerLocked(boolean enabled) {
//        if (enabled) {
//            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
////            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
////                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            toggle.setDrawerIndicatorEnabled(false);
//        } else {
//            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
////            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            toggle.setDrawerIndicatorEnabled(true);
//        }
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">
<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >


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

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


    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>


</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/nav_items">

    <RelativeLayout
        android:id="@+id/expandable_Frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </RelativeLayout>

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

</android.support.v4.widget.DrawerLayout>

LoginFragment.java

public class LoginFragment extends Fragment {

private Button loginButton;
private VolleyCallback volleyCallback;
private EditText mobileNumber, password;
private TextView signup;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        ((MainActivity) getActivity()).setDrawerLocked(true);

    return inflater.inflate(R.layout.fragment_login, container, false);
}

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

    mobileNumber = view.findViewById(R.id.mobile_number_edittext);
    loginButton = view.findViewById(R.id.login);
    password = view.findViewById(R.id.password_edittext);
    signup = view.findViewById(R.id.signup);

//        ((MainActivity) getActivity()).getSupportActionBar().setTitle("Login");

    signup.setOnClickListener(v -> jumpToSignup());

    loginButton.setOnClickListener(v -> {
        if (checkLength(mobileNumber, 10) && checkLength(password, 6)) {

            apiCallForLogin(Constants.getString(mobileNumber), Constants.getString(password));
        } else {
            if (!checkLength(mobileNumber, 10))
                mobileNumber.setError("Enter a valid mobile number");
            else
                password.setError("Password should contain atleast 6 digits");
        }
    });
}

private void jumpToSignup() {
    Fragment fragment = new SignUpFragment();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, fragment);
//        fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commit();
}

private void apiCallForLogin(String mobileNumber, String password) {
    APICall apiCall = new APICall(volleyCallback, getContext());
    apiCall.addParams("mobile", mobileNumber);
    apiCall.addParams("password", password);

}
}

fragment_login.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.LoginFragment">


<android.support.design.widget.TextInputLayout
    android:id="@+id/mobile_number_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/password_layout"
    android:layout_centerHorizontal="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/mobile_number_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enter_your_mobile_number"
        android:inputType="number"
        android:maxLength="10"
        android:minEms="10" />
</android.support.design.widget.TextInputLayout>


<android.support.design.widget.TextInputLayout
    android:id="@+id/password_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/password_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:inputType="textPassword"
        android:maxLength="10"
        android:minEms="10" />
</android.support.design.widget.TextInputLayout>


<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/password_layout"
    android:layout_centerHorizontal="true"
    android:text="Login" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="52dp"
    android:text="@string/new_user_signup_here"
    android:textColor="@color/black"
    android:textSize="15sp"
    android:id="@+id/signup"
    />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果没有看到MainActivity代码,很难理解您遇到的确切问题。但是,基于一些常见问题,我建议在这种情况下使用NestedScrollView

但是,您可以考虑使用额外的ScrollView包裹LinearLayout。所以最终的布局将如下所示。

<LinearLayout> 
    <ScrollView>
        <LinearLayout>
            <!-- Other views --> 
        <LinearLayout/>
    <ScrollView/>
<LinearLayout/>