【发布时间】:2013-04-17 10:24:56
【问题描述】:
我有一个 ViewPager,它包含同一个片段的多个实例,这个片段包含一篇文章。文章视图层次结构非常简单,一个标题、一个横幅图像、一个副标题和一个正文;除了标题之外的所有内容都包含在滚动视图中。
问题是,当你滑动到一个新页面时,fragment 会以 Views 在顶部呈现,然后它会立即滚动到容器的中间。 (事实上,它会滚动到带有 id: article_content 的 TextView 的开头)
我已在问题底部发布了布局。
现在,ViewPager 设置了 FragmentStatePagerAdapter 的简单实现,代码如下:
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;
}
}
Fragment 本身也很简单。首先,我在onCreate 期间检查是否有文章缓存,我调用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是一个简单的Texts集合,没什么花哨的:
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>
【问题讨论】:
-
你试过弄乱ScrollView上的
android:descendantFocusability吗? -
刚刚做了,我实际上尝试了所有三个值,三个值都一样,滚动视图仍然滚动到中间。
-
注意:您可能需要将
android:focusable=true和android:focusableInTouchMode=true用于android:descendantFocusability=beforeDescendants。我怀疑这些问题是基于包含android:textIsSelectable。也可以尝试删除它,作为控件。 -
嗯,我不明白为什么
android:textIsSelectable应该是问题的根源,毕竟之前所有的属性都在那里,当它是一个包含单个片段。但是,是的,确实工作了,它不再滚动到中间,谢谢! -
顺便说一句,您应该将其作为答案,我想我可以处理无法选择文本的问题。
标签: android android-viewpager android-scrollview