Java Scrollview在片段中不工作
我有一个注册片段,它的根布局是Java Scrollview在片段中不工作,java,android,android-fragments,android-scrollview,Java,Android,Android Fragments,Android Scrollview,我有一个注册片段,它的根布局是ScrollView,子布局是LinearLayout。我在LinearLayout中编辑了文本。但是,当我点击一个EditText并尝试滚动视图时,它不起作用 要打开这个片段,我必须单击登录片段上的注册视图 这是我的SignUpFragment.java public class SignUpFragment extends Fragment { private EditText etFirstName, etLastName, etEmail, etP
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);
}
}
请注意,目前我的流程如下:-
Mainactivity加载LoginFragment->单击注册->注册片段打开
如果我直接从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>
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>
如果不查看您的
main活动
代码,就很难理解您遇到的确切问题。但是,基于一些常见问题,我建议在本例中使用NestedScrollView
但是,您可以考虑使用额外的<代码>线性布局< /代码>包装<代码> ScLabVIEW < /代码>。因此,最终布局将如下所示
<LinearLayout>
<ScrollView>
<LinearLayout>
<!-- Other views -->
<LinearLayout/>
<ScrollView/>
<LinearLayout/>
我现在添加了mainactivity和loginFragment代码。
<?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>
<LinearLayout>
<ScrollView>
<LinearLayout>
<!-- Other views -->
<LinearLayout/>
<ScrollView/>
<LinearLayout/>