zoukankan      html  css  js  c++  java
  • android Fragment使用RecyclerView

    1、先对系统生成的Fragment改造成更直观的,比如这种

    public class DashboardFragment extends Fragment {
    
        private DashboardViewModel dashboardViewModel;
    
        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
            View view =inflater.inflate(R.layout.fragment_dashboard,container,false);
            return view;
            /*
            /*
            dashboardViewModel =
                    ViewModelProviders.of(this).get(DashboardViewModel.class);
            View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
            final TextView textView = root.findViewById(R.id.text_dashboard);
            dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
                @Override
                public void onChanged(@Nullable String s) {
                    textView.setText(s);
                }
            });
            return root;
             */
        }
    }

    2、把Activity里用RecyclerView的拷贝过来,稍微改造下即可,如果找不到R.layout. 里的布局,可重新打开工程等

    import com.example.mytest2.R;
    
    public class NotificationsFragment extends Fragment {
    
        //private NotificationsViewModel notificationsViewModel;
        private RecyclerView mRecyclerView;
        private ArticleAdapter mArticleAdapter;
        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
    
            View view =inflater.inflate(R.layout.fragment_notifications,container,false);
    
            mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());//(this);
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(llm);
            mArticleAdapter=new ArticleAdapter(/*this,*/llm);
            mRecyclerView.setAdapter(mArticleAdapter);
    
            return view;
    
            /*
            notificationsViewModel =
                    ViewModelProviders.of(this).get(NotificationsViewModel.class);
            View root = inflater.inflate(R.layout.fragment_notifications, container, false);
            final TextView textView = root.findViewById(R.id.text_notifications);
            notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
                @Override
                public void onChanged(@Nullable String s) {
                    textView.setText(s);
                }
            });
            return root;
    */
    
        }
    }
  • 相关阅读:
    parallel desktop ubuntu从18.04更新到20.04(包括安装Parallels Tools)
    一段奇怪的R代码
    ipynb(jupyter notebook)的git管理的比较好的方式
    对比jupyterlab和jupyter notebook
    dotfiles的管理
    macvim报出一些奇怪的错误: macvim只能从命令行启动
    CSS3学习笔记(三、选择器-续)
    CSS3学习笔记(二、选择器)
    CSS3学习笔记(一、CSS介绍、语法、引入方式)
    HTML学习笔记(四、body内常用标签)
  • 原文地址:https://www.cnblogs.com/cnchengv/p/14660401.html
Copyright © 2011-2022 走看看