首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ViewPager中的ScrollView,自动滚动到中间

ViewPager中的ScrollView,自动滚动到中间
EN

Stack Overflow用户
提问于 2013-04-24 10:22:12
回答 6查看 17.5K关注 0票数 23

我有一个包含相同片段的多个实例的ViewPager,这个片段包含一篇文章。文章视图层次结构非常简单,只有一个标题、一个横幅图像、一个副标题和一个正文;除了标题之外的所有内容都被包装在滚动视图中。

的问题是,当你滑动到一个新的页面时,片段会在顶部显示视图,然后它会立即滚动到容器的中间。(事实上,它会滚动到id为article_content)的TextView的开头

我已经在问题的底部张贴了布局。

现在,通过一个简单的FragmentStatePagerAdapter实现来设置ViewPager,下面是代码:

class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    Bundle args;
    int count;

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
        this.count = 8;
    }

    @Override
    public Fragment getItem(int position) {
        ArticleFragment mPrimaryFragment = new ArticleFragment();
        args = new Bundle();
        args.putString(ArticleFragment.KEY_ARTICLE_URL, mCurArticleLink);
        mPrimaryFragment.setArguments(args);
        return mPrimaryFragment;            
    }

    @Override
    public int getCount() {
        return count;
    }   
}

片段本身也非常简单。首先,我在onCreate期间检查是否缓存了文章,即对onCreateView的I调用

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.apk_article_view, null);

    mTitle = (TextView) view.findViewById(R.id.article_title);
    mBanner = (ImageView) view.findViewById(R.id.article_banner);
    mSubTitle = (TextView) view.findViewById(R.id.article_subtitle);
    mContent = (TextView) view.findViewById(R.id.article_content);

    if (isArticleCached) {
        Constants.logMessage("Article is cached, loading from database");
        setApkArticleContent();
    }
    else {
        Constants.logMessage("Article isn't cached, downloading");
        HtmlHelper.setApkArticleContent(mContext, mUrl, mTitle, mSubTitle, mContent, mBanner);
        setRefreshing(true);
    }

    return view;
}

值得注意的是,setApkArticleContent是一组简单的文本,没有什么花哨的东西:

private void setApkArticleContent() {
    mTitle.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.TITLE))));
    mSubTitle.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.SUBTITLE))));
    mContent.setText(Html.fromHtml(mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.BODY))));
    UrlImageViewHelper.setUrlDrawable(mBanner, mCursor.getString(mCursor.getColumnIndex(DbOpenHelper.BANNER)));     
}

此外,请知道,我以前没有一个分页程序,片段只加载到一个空的活动,它的工作,没有滚动到滚动视图的中间。

我真的不确定是什么触发了滚动,是的,我知道我可以通过编程将其设置为在加载后滚动回顶部,但话又说回来,当片段加载时会有两次滚动移动,这对用户来说非常明显。

你们知道为什么它会表现成这样吗?我怎么才能阻止那个无意的卷轴呢?

谢谢,

以下是ArticleFragment的布局:

<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/article_title"
    style="@style/headerTextBoldNoUnderline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center_vertical"
    android:text="" />

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        >

        <ImageView
            android:id="@+id/article_banner"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp" />

        <TextView
            android:id="@+id/article_subtitle"
            style="@style/HeaderTextItalicNoUnderline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left|center_vertical" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="?android:attr/dividerVertical" />

        <TextView
            android:id="@+id/article_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:gravity="left|center_vertical"
            android:padding="8dp"
            android:textColor="?android:attr/textColorSecondary"
            android:textIsSelectable="true"
            android:textSize="16sp" />
    </LinearLayout>
</ScrollView>

</LinearLayout>
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-04-24 11:23:09

这可能是由android:textIsSelectable引起的。您可以尝试将以下内容添加到ScrollView:

android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
票数 32
EN

Stack Overflow用户

发布于 2013-09-10 20:33:38

根据以下问题,将此属性值android:descendantFocusability="blocksDescendants"添加到ScrollView的子级:How to prevent a scrollview from scrolling to a webview after data is loaded?

票数 10
EN

Stack Overflow用户

发布于 2013-07-09 06:29:43

我也有这个问题。我通过将android:focusable="true"android:focusableInTouchMode="true"设置为ScrollView的LinearLayout中的第一个元素解决了这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16182331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档