【问题标题】:Added fragment scrolled down添加的片段向下滚动
【发布时间】:2017-11-14 09:02:45
【问题描述】:

我有两个片段,第一个片段包含线性布局列表,整个片段处于滚动视图中,第二个片段被添加,第一个片段在从第一个片段中选择项目时隐藏。 问题是如果第一个片段向下滚动,则创建的第二个片段向下滚动。 我尝试了强制第二个片段滚动到 (0,0) 的方法,但失败了。

用于添加第二个片段的代码

public void setActionOnClick(String id) {
     CommentFragment frag = new CommentFragment();
     Bundle bundle = new Bundle();
     bundle.putString("id", id);
     bundle.putString("TAG", TAG_NEWS_STORY);
     ((MainActivity) getActivity()).setCurrentTag(TAG_NEWS_STORY);
     frag.setArguments(bundle);
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
     getActivity().getSupportFragmentManager().beginTransaction();
     fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, 
     android.R.anim.fade_out);
     fragmentTransaction.add(R.id.main_content, frag, TAG_NEWS_STORY);
     fragmentTransaction.addToBackStack(null);
     fragmentTransaction.commit();
}

在附加第二个片段时,第一个片段被隐藏。 我不想使用 fragmentTransaction.replace,因为我不想重新加载对 api 的调用。

【问题讨论】:

  • 您找到解决方案了吗?

标签: android android-fragments android-scrollview


【解决方案1】:

这是一个老问题,但是。可能您的片段place_hoder 在滚动视图中。 只需检查R.id.main_content 是否在滚动视图中。 您应该将 ScrollView 添加到 fragment-layout 而不是在其中实例化片段

【讨论】:

    【解决方案2】:

    在此之前:

    android.support.v4.app.FragmentTransaction fragmentTransaction = 
     getActivity().getSupportFragmentManager().beginTransaction();
    

    添加代码以删除旧片段:

    getSupportFragmentManager().beginTransaction().remove(yourOldFragment).commitAllowingStateLoss();
    

    【讨论】:

    • 我不想删除旧片段,而且我不适合我,它看起来也滚动了。
    猜你喜欢
    • 2012-05-12
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    相关资源
    最近更新 更多