view.post没有执行run()

时间:2017-04-29 10:13:32

标签: android android-layout android-fragments android-activity android-scrollview

我有一个可以正常使用的应用程序,但是在没有人碰到它的一年之后,我决定做一个 回归,但是我遇到了一个问题。在android api 24上运行应用程序之后,应用程序只是没有执行带有run()的view.post(),在onCreateView()中 应用程序甚至没有崩溃,它正在工作,但是缺少了ui 我做了一些调试,它只是因为某种原因跳过它。我曾经认为这是一个线程问题,但现在我不再知道了。 这是班级:

public class CategoriesFragment extends Fragment {
final String dbname = "app";
final String tbname = "categories";
DynamicUI ui;
int rowLength = 3;
final int dbver = 1;
public CategoriesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    final ScrollView l = (ScrollView) inflater.inflate(R.layout.fragment_categories_grid, container, false);
    l.post(new Runnable() {
        @Override
        public void run() {
            ui.addUtilitiesToLayout((LinearLayout) getActivity().findViewById(R.id.moreLayout), false);
            ui.addCategoriesToLayout((LinearLayout) getActivity().findViewById(R.id.categLayout), rowLength);
            // code you want to run when view is visible for the first time
        }
    });
    ui = new DynamicUI(getActivity(),DatabaseHelper.RIGHTS);
    return inflater.inflate(R.layout.fragment_categories_grid, container, false);

  }
}
例如,在api 23上,代码运行完美,这两行工作正常:

ui.addUtilitiesToLayout((LinearLayout) getActivity().findViewById(R.id.moreLayout), false);
ui.addCategoriesToLayout((LinearLayout) getActivity().findViewById(R.id.categLayout), rowLength);

这就是我觉得奇怪的事情:

l.post(new Runnable() {

它只是跳过它。 它真的很烦人。 我真的非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

问题在于这一行

return inflater.inflate(R.layout.fragment_categories_grid, container, false);

应该是

return l;
每次调用时,

inflater.inflate都会返回一个新视图。您的onCreateView返回的内容与您在

上发布的内容不同