【问题标题】:fragment does not start at top片段不是从顶部开始
【发布时间】:2016-04-01 07:00:18
【问题描述】:

我的应用程序有几个带有 viewpager 的片段选项卡,但它没有从顶部开始,我不知道为什么。即使我将片段滚动到顶部,刷新(来回切换选项卡)也会重置片段的起始位置,就在 recyclerview 的正上方。

fragment2.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:isScrollContainer="false"
android:layout_gravity="fill_vertical"
android:clipToPadding="false"
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="79dp"
            android:scaleType="fitXY"
            android:src="@drawable/sub4_visual"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="79dp"
            android:alpha="0.3"
            android:background="#000000"/>
    </RelativeLayout>
    <TextView
        android:id="@+id/frag2_desc"
        android:layout_margin="11dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="@color/color_333333"
        android:text=""/>
    <Spinner
        android:id="@+id/frag2_dropbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="ifContentScrolls"
        android:scrollbars="none"
        android:gravity="left"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_subfrag2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Fragment2.java

    public class Fragment2 extends Fragment{

...

    public static Frag_BoardContent[] fragsList;
    public static RecyclerView mRecyclerView;
    public static Frag234_Adapter4 board_adapter;


    public Fragment2() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View v = inflater.inflate(R.layout.fragment2, container, false);

        tv_frag2Desc = (TextView) v.findViewById(R.id.frag2_desc);


        spinner = (Spinner) v.findViewById(R.id.frag2_dropbox);
        ArrayList<String> categoryList = new ArrayList<>();
        if (connect) makeDynamicArray(categoryList);
        adapter = new ArrayAdapter<> (getActivity(), R.layout.activity_board_spinner,categoryList);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final AdapterView<?> parent, final View view, int position, long id) {
                            mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.context));
                            mRecyclerView.setHasFixedSize(true);
                            mRecyclerView.setNestedScrollingEnabled(false);
                            board_adapter = new Frag234_Adapter4(MainActivity.context, fragsList);
                            mRecyclerView.setAdapter(board_adapter);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_subfrag2);
        mRecyclerView.setAdapter(board_adapter);



        return v;
    }

    public ArrayList<String> makeDynamicArray(){...}
}

【问题讨论】:

  • 片段不是从顶部开始的?你什么意思?正如您第一次打开应用程序或片段加载时片段不加载但顶部未看到一样。
  • 发布您的视图寻呼机和tablayout xml
  • 顶部没有看到,谷歌搜索了三个小时后,我在这里找到了答案:stackoverflow.com/questions/33584187/…
  • 始终进行研究,然后发布问题,节省您和他人的时间。
  • 感谢您的建议,但我认为这里的每个人在在此社区中发布问题之前都会进行研究。如果我在搜索了三个小时后没有找到那个链接,我想其他人也会有同样的麻烦找到一个好的答案。我只是为该链接提供了另一种方法,以便有人可能会看到我的问题并到达与我相同的目的地。只是想帮助自己和他人。链接帖子让其他人更容易找到答案,我希望你也有这样的经历。@DAgrawal

标签: android fragment


【解决方案1】:

这是关于焦点的问题。你需要对你的滚动视图做requestFocus()

setFocusable(false)

分片;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多