the*_*bol 4 android android-fragments android-scroll navigation-drawer
我有以下情况:
我正在使用导航抽屉让用户轻松地在不同主题之间导航.
你可以在这里找到一张图片:
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();
// Highlight the selected item, update the title, and close the drawer
TextView textView = (TextView) findViewById(Id);
getActionBar().setTitle(textView.getText());
mDrawerLayout.closeDrawer(mDrawerList);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助 ;-)
编辑: 解决方案:
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());
}
});
}
Run Code Online (Sandbox Code Playgroud)
str*_*ide 14
尝试调用视图时调用smoothScrollTo方法post:
ScrollView scrollView = (ScrollView)getView().findViewById(scrollID);
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0,(getView().findViewById(relativeID)).getTop());
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3813 次 |
| 最近记录: |