2

My application has a couple tabs of fragments with a viewpager and it does not start on top and I do not know why. Even when I scroll the fragment to the top, refreshing(going back and forth tabs) resets the fragment's start position right above the 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(){...}
}
Nongthonbam Tonthoi
  • 11,055
  • 6
  • 30
  • 59
Jisu Hong
  • 674
  • 1
  • 4
  • 21
  • Fragment does not start on top? what do you mean? As in fragment doesnt load the first time you open app or fragment loads but top part not seen. – Ragesh Ramesh Apr 01 '16 at 07:03
  • Post your view pager and tablayout xml – D Agrawal Apr 01 '16 at 07:08
  • 1
    top part not seen and after googling for three hours I found out the answer here: http://stackoverflow.com/questions/33584187/nestedscrollview-wont-start-from-top – Jisu Hong Apr 01 '16 at 07:08
  • Always do research and then post a question, saves your and others time. – D Agrawal Apr 01 '16 at 07:11
  • 2
    Thank you for your advice but I think everyone here researches before they post a question in this community. If I didnt find that link after three hours of searching, I think others will have the same trouble finding a good answer too. I just provided another approach to that link so that someone might see my question and get to the same destination as I did. Just trying to help myself and the others. Linking posts make others find the answer easier and I wish you have had an experience like that too.@DAgrawal – Jisu Hong Apr 01 '16 at 08:42

1 Answers1

0

It is a problem about focus. you need to do requestFocus() to your scrollview and

setFocusable(false)

to fragment;