android - 使用 viewpager 在 View 中滚动子项

标签 android android-viewpager swipe

我有一个看起来像这样的菜单 http://flashspeaksactionscript.com/wp-content/uploads/2008/06/vert-menu18.jpgchild 视为图片中的“3”,我希望能够向左滑动“3”并看到能够看到另一个项目。

这是我的 xml 代码:

         <LinearLayout
            android:id="@+id/llayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp" >

            <Button
                android:id="@+id/Sum"
                style="@style/SummaryButtons"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="Item 1" />
            <Button
                android:id="@+id/nana"
                style="@style/SummaryButtons"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="Item 1" />
            <Button
                android:id="@+id/mama"
                style="@style/SummaryButtons"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="Item 1" />

        </LinearLayout>


        <LinearLayout
            android:id="@+id/example_get_by_id"
            android:layout_width

等这些是 children 。例如,我希望能够滑动浏览这些按钮,只是因为我不能使用 Horizo​​ntalScrollView,因为我的应用程序由 fragment 组成并且已经具有浏览 fragment 的滑动功能。这些 fragment 的滑动功能和 Horizo​​ntalScrollView 混淆了,这就是为什么我需要能够将 Viewpager 用于单个项目而不是整个页面。至少有一个例子吗?我找不到任何东西。 提前致谢。

最佳答案

看看这可能对你有用:

public class MainActivity extends Activity {

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
  ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
  myPager.setAdapter(adapter);
  myPager.setCurrentItem(0);
 }

 private int imageArra[] = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
   R.drawable.gallery_photo_3, R.drawable.gallery_photo_5,
   R.drawable.gallery_photo_6, R.drawable.gallery_photo_7,
   R.drawable.gallery_photo_8, R.drawable.ic_launcher };

}

和 ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];

public ViewPagerAdapter(Activity act, int[] imgArra) {
    imageArray = imgArra;
    activity = act;
}

public int getCount() {
    return imageArray.length;
}

public Object instantiateItem(View collection, int position) {
     ImageView view = new ImageView(activity);
      view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
      view.setScaleType(ScaleType.FIT_XY);
      view.setBackgroundResource(imageArray[position]);
      ((ViewPager) collection).addView(view, 0);
    return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" >

    </ImageView>
</android.support.v4.view.ViewPager>

</LinearLayout>

关于android - 使用 viewpager 在 View 中滚动子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19607228/

相关文章:

android - 在 sharedPref.getString 中设置默认值有什么意义?

android - 无法为未附加到 FragmentManager 的 Fragment 设置MaxLifecycle : ViewPager

android - OpenGLRenderer 试图缩小位图 - 当到达 ViewPager 的末尾时

javascript - 选项卡面板可在移动 View 中滑动吗?

ios - 如何在字符串数组中的 View Controller 内滑动?

android - 如何在点击推送通知时打开特定 Activity ?

android - react native - webview 在构建发布 apk 时不呈现本地 html

android - 如何在不从 FragmentManager 中删除 DialogFragment 的情况下关闭它

android - 为什么我的 TextView 不是我的 ViewPager fragment 中的 centerInParent?

javascript - JQmobi - 动态切换页面