7

我真的是 android 的新手,我会很感激我的课程工作的任何帮助。

我需要去做:

1)一个Activity中的两个ViewPager(不嵌套)

2)两个ViewPager(一个ViewPager嵌套在另一个ViewPager中)

我发现了类似的问题,但我无法使用它。ViewPager 内的 ViewPager

我添加了第一个 ViewPager,不知道下一步该做什么

LayoutInflater inflater = LayoutInflater.from(this); //this - context of my activity
List<View> pages = new ArrayList<View>();
View page = inflater.inflate(R.layout.activity_main, null);
//next I adding some buttons on page
pages.add(page);

page = inflater.inflate(R.layout.activity_main2, null);  //my second page
//some buttons
pages.add(page);

page = inflater.inflate(R.layout.activity_main3, null);  //my third page
//some buttons
pages.add(page);

SamplePagerAdapter pagerAdapter = new SamplePagerAdapter(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);          
setContentView(viewPager);

如果我添加 ViewPager2 并说 setContentView(viewPager2),我失去了我的 viewPager1。我花了很多时间来解决这两个问题,请大家帮忙...

//这是我需要做的草图。抱歉,我无法附上我的草图...

4

4 回答 4

16

I added an OnTouchListener to the interior ViewPager:

private OnTouchListener mSuppressInterceptListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(
                event.getAction() == MotionEvent.ACTION_DOWN &&
                v instanceof ViewGroup
        ) {
                ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
        }
        return false;
    }
};

This just detects ACTION_DOWN touch events on the inner ViewPager and prevents the outer one from intercepting it. Because it returns false, only the ACTION_DOWN event should be hit; all the other events will be ignored. You can add this listener to every element you want to "protect" from the outer ViewPager's scrolling, though obviously if you want to pick up any other touch behaviour on those elements you'll need to deal with them inside the touch listener and possibly implement a better listener.

于 2013-05-08T18:43:50.433 回答
1

我认为你应该重新考虑在另一个 viewpager 中有一个 viewpager。触摸事件会有很多问题,而且用户体验可能会令人困惑/棘手,我建议你重新考虑一下。

现在针对问题 1:

在活动的 xml 文件 (/layout/activity.xml) 中声明两个 viewpagers,例如:

<?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" >

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

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

</LinearLayout>

然后在您的活动的 onCreate 方法上膨胀 xml 并连接两个视图页面:

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

    mViewpager1 = (ViewPager) findViewById(R.id.viewpager1);
    mViewpager2 = (ViewPager) findViewById(R.id.viewpager2);
}

不要忘记声明这两个变量private ViewPager mViewpager1, mViewpager2;

然后你可以用 mViewpager1 和 mViewpager2 做任何你想做的事。还有一个提示,我建议您使用适配器来设置页面,而不是手动将它们一一添加到每个视图页面,这样操作起来会更干净,更好。

于 2014-08-07T13:39:38.557 回答
1

关于拥有嵌套 Viewpager 并禁用嵌套 Viewpager 滑动的解决方案(如果您想在嵌套中设置带有按钮的选定页面)

对于嵌套的 You SubClass ViewPager 并添加以下内容:

 public boolean onInterceptTouchEvent(MotionEvent event) {
        //if you want to send the Touches to this Viewpager (the nested) //change true to false
      return true;    
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages on this inner 

        //send swipe to father instead of child
        if (this.getParent()!=null &&  this.getParent() instanceof ViewPager) {
            ((ViewPager) this.getParent()).onTouchEvent(event);
        }
        return false;
    }

在布局中你把这个:

<com.mypackage.mycustomapp.myviews.MyViewPager
            android:id="@+id/myNestedviewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
于 2017-04-07T18:18:44.103 回答
0
  1. 将单个 viewPager 与ConcatAdapter
//sample usage
val strings = listOf("a", "b", "cde", "f", "g", "h")
viewPager.adapter = ConcatAdapter(
   strings.partition({ string -> string.length > 1 }, { string -> string.split("") })
     //{{a, b}, {c, d, e}, {f, g, h}}
      .map { StringAdapter(it) }.toList() 
)

/**
 * custom partition function as kotlin List extension
 */

fun <E> List<E>.partition(shouldFlatten: (e: E)->Boolean, flatten: (e: E)->List<E>) : List<List<E>>{
   val partitions = mutableListOf<List<E>>()
   var currentPartition = mutableListOf<E>()
   val listIterator = this.listIterator()
   while (listIterator.hasNext()) {
      val next = listIterator.next()
      if (shouldFlatten(next)) {
         if (currentPartition.isNotEmpty()) {
            partitions.add(currentPartition)
            currentPartition = mutableListOf()
         }
         partitions.add(flatten(next))
      } else {
         currentPartition.add(next)
      }
   }
   if (currentPartition.isNotEmpty()) {
      partitions.add(currentPartition)
   }
   return partitions
}
于 2021-02-27T20:30:26.033 回答