码迷,mamicode.com
首页 > 移动开发 > 详细

android Fragment使用RecyclerView

时间:2021-04-15 12:40:48      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:getview   you   Owner   viewgroup   public   manager   rgba   set   etl   

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;
*/

    }
}

 

android Fragment使用RecyclerView

标签:getview   you   Owner   viewgroup   public   manager   rgba   set   etl   

原文地址:https://www.cnblogs.com/cnchengv/p/14660401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!