Use smoothScrollTo in fragment

I have the following situation:

I use the navigation box so that the user can easily navigate between different topics.

You can find a photo here:

https://drive.google.com/file/d/0B2gMUEFwlGRfSHRzVlptYmFQTXc/edit?usp=sharing

When you click on a title, such as General , only the main view of the content is replaced using the fragment and layout file. But when the user clicks on a subtitle, for example, Gameplay , the layout changes, And he must scroll down to view the view in the layout.

So, in my fragment class, I use the onViewCreated method and the smoothScrollTo method provided by ScrollView. ScrollView and RelativeLayout are both not null and set to the correct identifier specified in "onCreateView"

CodeSnippets:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (getArguments().getBoolean(ARG_BOOL)) {
        scrollView = (ScrollView)getView().findViewById(scrollID);
        relativeLayout = (RelativeLayout)getView().findViewById(relativeID);

        ((ScrollView)getView().findViewById(scrollID)).smoothScrollTo(
                0, (getView().findViewById(relativeID)).getLeft());

        Toast.makeText(getApplicationContext(), "SCROLL",Toast.LENGTH_SHORT).                  
                show();
        }
     }

The problem is that it does not execute the "smoothScrollTo" method, and the toast is executed.

Here I call the fragment (boolean scrolling is used to control the smoothScrollTo method):

private void selectItem(int Id, boolean scroll) {
    Fragment fragment = new ContentFragment();
    Bundle args = new Bundle();
    args.putBoolean(ContentFragment.ARG_BOOL, scroll);
    args.putInt(ContentFragment.ARG_ITEM_ID, Id);
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();
    // Highlight the selected item, update the title, and close the drawer
    TextView textView = (TextView) findViewById(Id);
    getActionBar().setTitle(textView.getText());
    mDrawerLayout.closeDrawer(mDrawerList);
} 

Thank you for your help; -)

EDIT: SOLUTION:

if (getArguments().getBoolean(ARG_BOOL)) {
    getView().findViewById(scrollID).post(new Runnable() {
        @Override
        public void run() {
            ((ScrollView) getView().findViewById(scrollID)).
                smoothScrollTo(0, (getView().findViewById(relativeID)).getTop());
        }
    });
}
+4
source share
1 answer

Try calling the method smoothScrollTowhen calling the postview:

ScrollView scrollView = (ScrollView)getView().findViewById(scrollID);
scrollView.post(new Runnable() {

    @Override
    public void run() {
        scrollView.smoothScrollTo(0,(getView().findViewById(relativeID)).getTop());
    }
});
+14
source

Source: https://habr.com/ru/post/1526762/


All Articles