ScrollView doesn't work in fragment

醉酒当歌 提交于 2019-12-25 14:33:12

问题


Does anyone know why the scroll does not work ? I trying to make my page scrollable and can swipe to another tab. It allows me to swipe but not scrollable. I'm use most of the answer in Stack Overflow but none of them is worked in my case.

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


<AbsoluteLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
xmlns:android="http://schemas.android.com/apk/res/android">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>



    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

<TextView
        android:layout_width="353dp"
        android:layout_height="91dp"
        android:text="Use the form below to change your information or update any others of your information"
        android:id="@+id/textView16"
        android:layout_x="9dp"
        android:layout_y="57dp"
        android:textSize="20dp" />


    <EditText
        android:layout_width="186dp"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:id="@+id/editText9"
        android:layout_x="17dp"
        android:layout_y="146dp" />

    <EditText
        android:layout_width="143dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText12"
        android:layout_x="226dp"
        android:layout_y="146dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Information"
        android:id="@+id/textView17"
        android:layout_x="20dp"
        android:layout_y="203dp" />

    <TextView
        android:layout_width="95dp"
        android:layout_height="37dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Weather"
        android:id="@+id/textView15"
        android:layout_x="20dp"
        android:layout_y="253dp" />

    <Spinner
        android:layout_width="174dp"
        android:layout_height="53dp"
        android:id="@+id/spinner5"
        android:layout_x="174dp"
        android:layout_y="238dp" />

</AbsoluteLayout>

回答1:


You have to add child views to linearLayout.

try something like this:

<LinearLayout 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:background="#FFFFFF"
android:orientation="vertical"
tools:context="com.example.samplestackoademo.MainActivity" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FFFFFF"
    android:fillViewport="true"
    android:orientation="vertical" >

    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView114"
            android:layout_width="218dp"
            android:layout_height="47dp"
            android:layout_x="12dp"
            android:layout_y="20dp"
            android:text="Update Page"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="353dp"
            android:layout_height="91dp"
            android:layout_x="9dp"
            android:layout_y="57dp"
            android:text="Use the form below to change your information or update any others of your information"
            android:textSize="20dp" />


        <EditText
            android:id="@+id/editText9"
            android:layout_width="186dp"
            android:layout_height="wrap_content"
            android:layout_x="17dp"
            android:layout_y="146dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

        <EditText
            android:id="@+id/editText12"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:layout_x="226dp"
            android:layout_y="146dp" />

        <TextView
            android:id="@+id/textView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="203dp"
            android:text="Information"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="95dp"
            android:layout_height="37dp"
            android:layout_x="20dp"
            android:layout_y="253dp"
            android:text="Weather"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner5"
            android:layout_width="174dp"
            android:layout_height="53dp"
            android:layout_x="174dp"
            android:layout_y="238dp" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="302dp"
            android:text="Status"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner7"
            android:layout_width="174dp"
            android:layout_height="53dp"
            android:layout_x="174dp"
            android:layout_y="295dp" />

        <TextView
            android:id="@+id/textView19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="357dp"
            android:text="Work Force"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="403dp"
            android:text="Subcontractors"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText17"
            android:layout_width="181dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="385dp" />

        <TextView
            android:id="@+id/textView23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="450dp"
            android:text="No. of Person(s)"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText18"
            android:layout_width="128dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="439dp"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="500dp"
            android:text="No.of hour(s)"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText19"
            android:layout_width="181dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="488dp" />

        <TextView
            android:id="@+id/textView25"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_x="20dp"
            android:layout_y="542dp"
            android:text="Work Details"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView26"
            android:layout_width="181dp"
            android:layout_height="54dp"
            android:layout_x="20dp"
            android:layout_y="589dp"
            android:text="Project/Service/Training"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner8"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="580dp" />

        <TextView
            android:id="@+id/textView27"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_x="20dp"
            android:layout_y="650dp"
            android:text="Work Description"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText20"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="650dp"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            android:id="@+id/textView28"
            android:layout_width="wrap_content"
            android:layout_height="61dp"
            android:layout_x="20dp"
            android:layout_y="706dp"
            android:text="Percentage "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText21"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="706dp" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="101dp"
            android:layout_height="58dp"
            android:layout_x="20dp"
            android:layout_y="760dp"
            android:text="Time In"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText22"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="753dp" />

        <TextView
            android:id="@+id/textView30"
            android:layout_width="wrap_content"
            android:layout_height="79dp"
            android:layout_x="20dp"
            android:layout_y="818dp"
            android:text="Time Out"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText23"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="819dp" />

        <TextView
            android:id="@+id/textView31"
            android:layout_width="181dp"
            android:layout_height="56dp"
            android:layout_x="20dp"
            android:layout_y="907dp"
            android:text="Project/Service/Training"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner9"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="913dp" />

        <TextView
            android:id="@+id/textView32"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_x="20dp"
            android:layout_y="994dp"
            android:text="Work Description"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText24"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="994dp"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="wrap_content"
            android:layout_height="65dp"
            android:layout_x="20dp"
            android:layout_y="1064dp"
            android:text="Percentage "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText25"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1064dp" />

        <TextView
            android:id="@+id/textView34"
            android:layout_width="101dp"
            android:layout_height="56dp"
            android:layout_x="20dp"
            android:layout_y="1143dp"
            android:text="Time In"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText26"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1143dp" />

        <TextView
            android:id="@+id/textView35"
            android:layout_width="wrap_content"
            android:layout_height="64dp"
            android:layout_x="20dp"
            android:layout_y="1204dp"
            android:text="Time Out"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText27"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1204dp" />

        <TextView
            android:id="@+id/textView36"
            android:layout_width="181dp"
            android:layout_height="67dp"
            android:layout_x="20dp"
            android:layout_y="1302dp"
            android:text="Project/Service/Training"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner13"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1307dp" />

        <TextView
            android:id="@+id/textView37"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_x="20dp"
            android:layout_y="1392dp"
            android:text="Work Description"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText28"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1390dp"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            android:id="@+id/textView38"
            android:layout_width="wrap_content"
            android:layout_height="98dp"
            android:layout_x="20dp"
            android:layout_y="1458dp"
            android:text="Percentage "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText29"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1450dp" />

        <TextView
            android:id="@+id/textView39"
            android:layout_width="101dp"
            android:layout_height="55dp"
            android:layout_x="20dp"
            android:layout_y="1524dp"
            android:text="Time In"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText30"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1521dp" />

        <TextView
            android:id="@+id/textView40"
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_x="20dp"
            android:layout_y="1589dp"
            android:text="Time Out"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText31"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1583dp" />

        <TextView
            android:id="@+id/textView41"
            android:layout_width="181dp"
            android:layout_height="80dp"
            android:layout_x="20dp"
            android:layout_y="1668dp"
            android:text="Project/Service/Training"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner14"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1655dp" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="57dp"
            android:layout_x="20dp"
            android:layout_y="1746dp"
            android:text="Work Description"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText32"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1746dp"
            android:ems="10"
            android:inputType="textMultiLine" />

        <TextView
            android:id="@+id/textView42"
            android:layout_width="wrap_content"
            android:layout_height="65dp"
            android:layout_x="20dp"
            android:layout_y="1825dp"
            android:text="Percentage "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText33"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1822dp" />

        <TextView
            android:id="@+id/textView43"
            android:layout_width="101dp"
            android:layout_height="55dp"
            android:layout_x="20dp"
            android:layout_y="1890dp"
            android:text="Time In"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText34"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1883dp" />

        <TextView
            android:id="@+id/textView44"
            android:layout_width="wrap_content"
            android:layout_height="106dp"
            android:layout_x="20dp"
            android:layout_y="1960dp"
            android:text="Time Out"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText35"
            android:layout_width="176dp"
            android:layout_height="wrap_content"
            android:layout_x="174dp"
            android:layout_y="1966dp" />
    </AbsoluteLayout>
</ScrollView>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >
</android.support.v4.view.ViewPager>

 </LinearLayout>

Add fragment to View Pager.

add adapter to view pager in your onCreate() method of activity.

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

    // Intializing view from xml file
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    // adding adapter to view pager
    VideoViewPagerAdapter adapter = new VideoViewPagerAdapter(
            getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0, true);
}

FragmentPagerAdapter.java

public class VideoViewPagerAdapter extends FragmentPagerAdapter {

private String[] urls = { "HelloText--", "HellooText--", "HelloText--",
        "HellooText--" };

public VideoViewPagerAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
}

@Override
public int getCount() {
    return urls.length;
}

@Override
public Fragment getItem(int position) {
    // Create fragment object
    VideoPlayerFragment fragment = new VideoPlayerFragment();

    Bundle args = new Bundle();
    args.putString("STRING_URL", urls[position] + "" + position);

    fragment.setArguments(args);

    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {
    return "Page " + (position + 1);
}
}

viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/pager_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="10dp"
    android:text="Helloooo"
    android:textSize="18dp" />

 </LinearLayout>

MyFragment.java

public class VideoPlayerFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.viewpager_layout, container, false);
    TextView textView = (TextView) v.findViewById(R.id.pager_textView);
    String data = getArguments().getString("STRING_URL");
    textView.setText(data);
    return v;
}

}


来源:https://stackoverflow.com/questions/33863791/scrollview-doesnt-work-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!