使用smoothScrollTo在片段 [英] Use smoothScrollTo in Fragment

查看:217
本文介绍了使用smoothScrollTo在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我使用的是抽屉式导航使用户方便地在不同主题之间切换。

您可以在这里找到一个画面:

https://drive.google.com/file/d/ 0B2gMUEFwlGRfSHRzVlptYmFQTXc /编辑?USP =共享

在上点击标题,例如常规的唯一的主要内容视图被替换,通过使用片段和布局文件。
但是,当用户点击一个副标题,例如游戏,布局的变化,它应该向下滚动到布局中specifc视图。

所以在我的片段类我使用了onViewCreated方法和smoothScrollTo方法,通过滚动型提供。滚动视图和RelativeLayout的都是不为空,并设置为正确的ID,在onCreateView

设置

codesnippets:

  @覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    super.onViewCreated(查看,savedInstanceState);
    如果(getArguments()。getBoolean(ARG_BOOL)){
        滚动视图=(滚动型)getView()findViewById(scrollID)。
        的RelativeLayout =(RelativeLayout的)getView()findViewById(relativeID)。        ((滚动型)getView()。findViewById(scrollID))。smoothScrollTo(
                0,(getView()findViewById(relativeID)。)getLeft())。        Toast.makeText(getApplicationContext(),滚动,Toast.LENGTH_SHORT)。
                显示();
        }
     }

的问题是,它不执行smoothScrollTo方法,而烤面包被执行

下面我所说的片段(布尔滚动来控制smoothScrollTo法):

 私人无效选择信息(INT标识,布尔滚动){
    片段片段=新ContentFragment();
    捆绑ARGS =新包();
    args.putBoolean(ContentFragment.ARG_BOOL,滚动);
    args.putInt(ContentFragment.ARG_ITEM_ID,ID);
    fragment.setArguments(参数);    //将替换现有的片段插入片段
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame,片段)
            。承诺();
    //高亮显示所选项目,更新称号,并关​​上抽屉
    TextView中的TextView =(的TextView)findViewById(ID);
    。getActionBar()的setTitle(textView.getText());
    mDrawerLayout.closeDrawer(mDrawerList);
}

您的帮助感谢; - )

编辑:
解决方法:

 如果(getArguments()。getBoolean(ARG_BOOL)){
    getView()。findViewById(scrollID)。员额(新的Runnable(){
        @覆盖
        公共无效的run(){
            ((滚动型)getView()。findViewById(scrollID))。
                smoothScrollTo(0,(getView()findViewById(relativeID))共达());
        }
    });
}


解决方案

尝试调用<一个href=\"http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo(int,%20int)\"><$c$c>smoothScrollTo法呼吁视图:

 滚动型滚动视图=(滚动型)getView()findViewById(scrollID)。
scrollView.post(新的Runnable(){    @覆盖
    公共无效的run(){
        scrollView.smoothScrollTo(0,(getView()findViewById(relativeID))共达());
    }
});

I have following situation:

I'm using a Navigation Drawer to make the user easily navigate between different topics.

You can find a picture here:

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

When clicking on a heading, like General the only the main content view is replaced, by using a fragment and a layout file. But when the user clicks on a subheading, like Gameplay, the layout changes, AND it should scroll down to a specifc view in the layout.

So in my fragment class I'm using the "onViewCreated" method and the smoothScrollTo method, provided by a ScrollView. The ScrollView and the RelativeLayout are both not null, and set to the right id, set 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 is not executing the "smoothScrollTo" method, while the toast gets executed.

Here I call the fragment (boolean scroll 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);
} 

Thanks 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());
        }
    });
}

解决方案

Try calling smoothScrollTo method in calling post of the view:

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

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

这篇关于使用smoothScrollTo在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆