ScrollView inside ViewPager, scrolls to middle automatically

前端 未结 6 775
滥情空心
滥情空心 2021-02-01 18:06

I have a ViewPager that contains several instances of the same fragment, this fragment contains an article. The Article view hierarchy is quite simple, a Title, a Banner image,

相关标签:
6条回答
  • 2021-02-01 18:41

    ScrollView inside ViewPager scrolls automatically but does not scrolls in middle.. Check out this.

    pager.setOnPageChangeListener(new OnPageChangeListener() {
    
                    @Override
                    public void onPageSelected(int position) {
                        // TODO Auto-generated method stub
                        int x=iv.getLeft();
                        int y=iv.getTop();
                        scroll.scrollTo(x, y);
                    }
    

    pager is ViewPager's object and scroll is ScrollView and iv is any View say TextView or ImageView.

    When we change page in viewpager, scrollbar automatically scrolls but to the left. If you would have find solution to middle please let me know.. :)

    0 讨论(0)
  • 2021-02-01 18:43

    Add this attribute value android:descendantFocusability="blocksDescendants" to the child of ScrollView, according to this question: How to prevent a scrollview from scrolling to a webview after data is loaded?

    0 讨论(0)
  • 2021-02-01 18:57

    This is likely caused by android:textIsSelectable. You may try adding the following to the ScrollView:

    android:focusable="true"
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    
    0 讨论(0)
  • 2021-02-01 18:59

    I tried solution:

    android:focusable=true
    
    android:focusableInTouchMode=true
    
    android:descendantFocusability=beforeDescendants
    

    And i dont know why but I cant do this with my RelativeLayout which is parent view (this does not work).

    I had to wrap my TextView inside FrameLayout and now everything is ok. Maybe this helps somebody.

      <FrameLayout
            android:id="@+id/detail_description_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/detail_parameters_container"
            android:layout_centerInParent="true"
            android:descendantFocusability="beforeDescendants"
            android:focusable="true"
            android:focusableInTouchMode="true" >
    
            <TextView
                android:id="@+id/detail_descritpion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autoLink="all"
                android:padding="10dp"
                android:textIsSelectable="true" />
        </FrameLayout>
    
    0 讨论(0)
  • 2021-02-01 19:00

    I also had this problem. I solved it with setting android:focusable="true" and android:focusableInTouchMode="true" to the first element in the ScrollView's LinearLayout.

    0 讨论(0)
  • 2021-02-01 19:03

    i try to add

        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="beforeDescendants"
    

    in the root viewGroup . it's work's well. like below.

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="beforeDescendants"
        android:background="@color/normal_bg">
    
        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/rl_enter"
            android:scrollbars="none">
            </ScrollView>
        </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题