片段不会从顶部开始

时间:2016-04-01 07:00:19

标签: android fragment

我的应用程序有几个带有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(){...}
}

1 个答案:

答案 0 :(得分:0)

焦点问题。您需要对滚动视图和

执行requestFocus()

setFocusable(false)

分段;