小编the*_*bol的帖子

在Fragment中使用smoothScrollTo

我有以下情况:

我正在使用导航抽屉让用户轻松地在不同主题之间导航.

你可以在这里找到一张图片:

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

单击标题时,如General,只使用片段和布局文件替换主内容视图.但是当用户点击子标题(如Gameplay)时,布局会发生变化,并且应该向下滚动到布局中的特定视图.

所以在我的片段类中,我使用的是"onViewCreated"方法和由ScrollView提供的smoothScrollTo方法.ScrollView和RelativeLayout都不为null,并设置为正确的id,在"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();
        }
     }
Run Code Online (Sandbox Code Playgroud)

问题是它没有执行"smoothScrollTo"方法,而吐司被执行.

这里我调用片段(布尔滚动用于控制smoothScrollTo方法):

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();
    // …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-scroll navigation-drawer

4
推荐指数
1
解决办法
3813
查看次数