ViewPager中的ScrollView,自动滚动到中间 [英] ScrollView inside ViewPager, scrolls to middle automatically

查看:58
本文介绍了ViewPager中的ScrollView,自动滚动到中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager,其中包含同一片段的多个实例,该片段包含一篇文章.文章视图的层次结构非常简单,包括标题,标题图像,字幕和正文.除标题外的所有内容都包装在滚动视图中.

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, a subtitle and a body; everything but the title is wrapped in a scrollview.

问题是,当您滑动到新页面时,该片段在顶部显示视图",然后立即滚动到容器的中间. (事实上​​,它滚动到ID为 article_content 的TextView的开头)

The problem is, when you swipe to a new page, the fragment is presented with the Views at the top, and then it immediately scrolls to the middle of the container. (As a matter of fact it scrolls to the beginning of the TextView with id: article_content)

我已将布局发布在问题的底部.

I have posted the layout at the bottom of the question.

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

Now, the ViewPager is set with a simple implementation of a FragmentStatePagerAdapter, here's the code:

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

The Fragment itself is pretty simple too. First, I check during onCreate to see if we have the article cached, the I call on onCreateView

@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是一组简单的文本,没有花哨:

It is worth noting that setApkArticleContent is a simple set of Texts, nothing fancy:

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)));     
}

另外,请知道,我以前没有寻呼机,该片段仅加载到一个空的活动中,并且工作时没有滚动到scrollview的中间 >.

Also, please know that I did not have a pager before, the fragment was only loaded to an empty activity, and it worked without scrolling to the middle of the scrollview.

我真的不确定是什么触发了滚动,是的,我知道我可以通过编程方式将其设置为在加载后滚动回顶部,但是同样,在加载片段时会进行两次滚动移动,对于用户而言,这将是非常明显的.

I am really not sure what is triggering the scroll, and yes, I know I can programatically set it to scroll back to the top after loading, but then again, that'd be two scroll movements when the fragment is loaded and it would be quite noticeable for the user.

你们有什么主意,为什么会这样?关于如何阻止意外滚动的任何想法?

Do you guys have any ideas why it would behave like this? Any ideas on how I can stop that unintentional scroll?

谢谢

下面是ArticleFragment的布局:

Below is the layout for the 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>

推荐答案

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

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"

这篇关于ViewPager中的ScrollView,自动滚动到中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆