【发布时间】:2013-12-10 22:57:49
【问题描述】:
背景:我为 Android 4 (SDK 15) 开发并使用本机操作栏。在我的一项活动中,我有一个以滚动视图作为根元素的片段。片段填充内容。我没有为活动设置布局,我只添加片段。一切正常。
问题:当我将标签添加到操作栏时,滚动视图不再起作用。当我点击屏幕时会显示滚动条,但没有滚动。
我有第二个标签的第二个片段。它是一个 WebView,可以完美滚动。
有没有人经历过或者有任何想法?
标签是用这个代码添加的:
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set the name of the current entity as the title of the action bar.
final TextView textView = (TextView) getLayoutInflater().inflate(
R.layout.action_bar_text_view, null);
textView.setText(getGuideContext().getSelectedEntity().getName(
getGuidePreferences().getNameLang()));
actionBar.setCustomView(textView);
// Show the actionbar now that the text view is set up.
actionBar.show();
// Set the tabs.
final Entity entity = getGuideContext().getSelectedEntity();
final DetailsTabListener factsListener = new DetailsTabListener(
DetailsMode.FACTS, entity, details);
actionBar.addTab(actionBar.newTab()
.setText(getString(R.string.details_tab_facts))
.setTabListener(factsListener));
final DetailsTabListener webListener = new DetailsTabListener(
DetailsMode.WEB, entity, details);
actionBar.addTab(actionBar.newTab()
.setText(getString(R.string.details_tab_web))
.setTabListener(webListener));
这是 DetailsTabListener:
public class DetailsTabListener implements TabListener {
/** The current details mode. */
private final DetailsMode detailsMode;
/** A reference to the details of the current entity. */
private final WeakReference<Details> detailsRef;
/** A reference to the current entity. */
private final WeakReference<Entity> entityRef;
/** Holds a reference to the fragment this listener last created. */
private WeakReference<Fragment> fragmentRef;
/**
* The constructor.
*
* @param detailsMode
* the current details mode, i.e. the type of fragment this
* listener is listening to, must not be null
* @param entity
* the currently selected entity, must not be null
* @param details
* details for the current entity, must not be null
*/
public DetailsTabListener(final DetailsMode detailsMode,
final Entity entity, final Details details) {
if ((detailsMode == null) || (entity == null) || (details == null)) {
throw new IllegalArgumentException(
"None of the parameters detailsMode, entity or details can be null.");
}
this.detailsMode = detailsMode;
this.entityRef = new WeakReference<Entity>(entity);
this.detailsRef = new WeakReference<Details>(details);
}
/** {@inheritDoc} */
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
if (fragmentRef == null) {
return;
}
// Scroll to top.
final Fragment fragment = fragmentRef.get();
if (fragment instanceof ScrollToTopInterface) {
((ScrollToTopInterface) fragment).scrollToTop();
}
}
/** {@inheritDoc} */
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Sets a fragment that was created earlier if there is one.
Fragment fragment = null;
if (fragmentRef != null) {
fragment = fragmentRef.get();
}
// Creates and adds a new fragment if there is none, or attach the old
// one if it exists.
if (fragment == null) {
fragment = DetailsFragmentFactory.createFragment(detailsMode,
entityRef.get(), detailsRef.get());
fragmentRef = new WeakReference<Fragment>(fragment);
ft.add(android.R.id.content, fragment, "");
} else {
ft.attach(fragment);
}
}
/** {@inheritDoc} */
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// Detach the fragment if there is one.
if (fragmentRef != null) {
ft.detach(fragmentRef.get());
}
}
}
这是片段的布局:
<ScrollView
android:id="@+id/details_facts_scroll_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/details_facts_margin_top_bottom"
android:layout_marginTop="@dimen/details_facts_margin_top_bottom"
android:layout_width="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:baselineAligned="false"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/details_facts_header_margin_top"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_weight="0.5"
android:text="@string/details_length"
style="@style/details_fact_header"
/>
<TextView
android:layout_weight="0.5"
android:text="@string/details_weight"
style="@style/details_fact_header"
/>
</LinearLayout>
<LinearLayout
android:baselineAligned="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/details_length"
android:layout_weight="0.5"
style="@style/details_fact"
/>
<TextView
android:id="@+id/details_weight"
android:layout_weight="0.5"
style="@style/details_fact"
/>
</LinearLayout>
<LinearLayout
android:baselineAligned="false"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/details_facts_header_margin_top"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_weight="0.5"
android:text="@string/details_wingspan"
style="@style/details_fact_header"
/>
<TextView
android:layout_weight="0.5"
android:text="@string/details_age"
style="@style/details_fact_header"
/>
</LinearLayout>
<LinearLayout
android:baselineAligned="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/details_wingspan"
android:layout_weight="0.5"
style="@style/details_fact"
/>
<TextView
android:id="@+id/details_age"
android:layout_weight="0.5"
style="@style/details_fact"
/>
</LinearLayout>
<TextView
android:id="@+id/details_photograph_section_header"
android:text="@string/details_photograph_section_header"
style="@style/details_fact_section_header"
/>
<View
android:id="@+id/details_photograph_section_divider"
style="@style/divider"
/>
<ImageView
android:contentDescription="@string/details_image_content_description"
android:id="@+id/details_image"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/details_facts_margin_left_right"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/details_image_text"
style="@style/details_image_text"
/>
<TextView
android:text="@string/details_taxonomy_section_header"
style="@style/details_fact_section_header"
/>
<View
style="@style/divider"
/>
<TextView
android:layout_marginTop="@dimen/details_facts_header_margin_top"
android:text="@string/details_english"
style="@style/details_fact_header"
/>
<TextView
android:id="@+id/details_taxonomy_english"
style="@style/details_fact"
/>
<TextView
android:layout_marginTop="@dimen/details_facts_header_margin_top"
android:text="@string/details_latin"
style="@style/details_fact_header"
/>
<TextView
android:id="@+id/details_taxonomy_latin"
style="@style/details_fact"
/>
<TextView
android:layout_marginTop="@dimen/details_facts_header_margin_top"
android:text="@string/details_swedish"
style="@style/details_fact_header"
/>
<TextView
android:id="@+id/details_taxonomy_swedish"
style="@style/details_fact"
/>
</LinearLayout>
【问题讨论】:
-
如果您需要任何帮助,您需要发布一些代码。
-
感谢您的提示!我添加了一些代码。很难知道什么是有趣的。你有什么想念的吗?
-
您是否要在 tabhost 中设置滚动视图?
-
不,我正在使用操作栏中的选项卡,并且在添加到活动内容的片段中有一个滚动视图。我已经添加了将片段添加到内容的代码示例。
-
当我添加第二个包含 Web 视图的片段时,它会完美滚动。我已经用更多信息更新了这个问题。
标签: android tabs android-actionbar android-scrollview