ViewPager 中的 ScrollView

ScrollView inside ViewPager

请帮助我。 我到处搜索如何在 ViewPager 中制作 ScrollView 但没有成功。 ViewPager 的孔布局应该是可滚动的。 重要提示:我只想要垂直的 ScrollView。

现在我想对您的回答表示感谢!

我的代码

"Main"

public class Rezepte2 extends Fragment {

// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from viewpager_main.xml
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.rezepte_fragment, container, false);

    // Generate sample data
    rezeptTitel = new String[] { "Nudeln","Fleisch" };

    naehrwerte = new String[] { "50kcal","70kcal" };

    zubereitung = new String[] { "ganz easy","voll easy" };

    rezeptImage = new int[] { R.drawable.ic_action_next_item, R.drawable.ic_action_next_item,
            R.drawable.ic_launcher, R.drawable.ic_action_next_item };

    // Locate the ViewPager in viewpager_main.xml
    viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
    // Pass results to ViewPagerAdapter Class
    adapter = new ViewPagerAdapter(getActivity(), rezeptTitel, naehrwerte, zubereitung, rezeptImage);
    // Binds the Adapter to the ViewPager
    viewPager.setAdapter(adapter);





    return layout;

}

适配器

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    String[] rezeptTitel;
    String[] naehrwerte;
    String[] zubereitung;
    int[] rezeptImages;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, String[] rezeptTitel, String[] naehrwerte,
                            String[] zubereitung, int[] rezeptImages) {
        this.context = context;
        this.rezeptTitel = rezeptTitel;
        this.naehrwerte = naehrwerte;
        this.zubereitung = zubereitung;
        this.rezeptImages = rezeptImages;
    }




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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables
        TextView txtrezeptTitel;
        TextView txtnaehrwerte;
        TextView txtzubereitung;
        ImageView imagerezept;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.rezepte_fragment_basic, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        txtrezeptTitel = (TextView) itemView.findViewById(R.id.rezeptTitel);
        txtnaehrwerte = (TextView) itemView.findViewById(R.id.naehrwerte);
        txtzubereitung = (TextView) itemView.findViewById(R.id.zubereitung);

        // Capture position and set to the TextViews
        txtrezeptTitel.setText(rezeptTitel[position]);
        txtnaehrwerte.setText(naehrwerte[position]);
        txtzubereitung.setText(zubereitung[position]);

        // Locate the ImageView in viewpager_item.xml
        imagerezept = (ImageView) itemView.findViewById(R.id.image_rezepte_basic);
        // Capture position and set to the ImageView
        imagerezept.setImageResource(rezeptImages[position]);

        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

滑动查看xml `

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    </LinearLayout>`  

ViewPager 片段xml

`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/rezeptTitel"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4">
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/ic_launcher"
            android:id="@+id/image_rezepte_basic"/>
        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/naehrwerte" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/zubereitung" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text" />


    </LinearLayout>
    </LinearLayout>

</RelativeLayout>´

也许您只是没有正确滚动 - 模拟器并没有真正的触摸屏。你必须在用另一根手指平移的同时触摸并按住它的一部分