背景:我为Android 4(SDK 15)开发并使用本机操作栏。在我的一个活动中,我有一个片段,其中scrollview作为根元素。片段填充内容。我没有为活动设置布局,我只添加了片段。一切正常。
问题:当我向操作栏添加标签时,滚动视图不再有效。当我点击屏幕但没有滚动时,滚动条会显示。
我有第二个标签用于第二个标签。它是一个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>
答案 0 :(得分:0)
我现在已经解决了这个问题。
似乎scrollview不知道选项卡,因此认为布局适合并且不需要滚动。如果我在布局的底部添加更多视图,我可以再次滚动。或者,如果我删除标签,则滚动有效(如问题中所述)。
我的解决方法是在布局的底部添加一个大空间视图。但是,如果有人知道为什么scrollview这样做我仍然感兴趣。我觉得这个问题有一个更优雅的解决方案。