公共类navagation_activity扩展AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navagation_activity);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,R.id.nav_vishal)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navagation_activity, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}......................................................................................................
公共类HomeFragment扩展Fragment实现LifecycleOwner {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
final RecyclerView recyclerView = root.findViewById(R.id.recyclerview);
homeViewModel = new ViewModelProvider(getActivity()).get(HomeViewModel.class);
Observer<List<CourseItem>> userListUpdateObserver = null;
homeViewModel.getUserMutableLiveData().observe(getActivity(), userListUpdateObserver);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
userListUpdateObserver = new Observer<List<CourseItem>>() {
@Override
public void onChanged(List<CourseItem> courseItems) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
RecyclerViewListAdapter recyclerViewListAdapter = new RecyclerViewListAdapter(getContext(), courseItems);
recyclerView.setAdapter(recyclerViewListAdapter);
}
};
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
List<CourseItem> course;
@Override
public void onChanged(String s) {
/*RecyclerViewListAdapter recyclerViewListAdapter = new RecyclerViewListAdapter(getContext(), course);
recyclerView.setAdapter(recyclerViewListAdapter);*/
}
});
return root;
}
}.......................................................................................
公共类HomeViewModel扩展ViewModel {
private MutableLiveData<String> mText;
private MutableLiveData<List<CourseItem>> mrecyclerview;
List<CourseItem> course;
public HomeViewModel() {
mText = new MutableLiveData<>();
mrecyclerview = new MutableLiveData<>();
// call your Rest API in init method
init();
// mText.setValue("This is home fragment");
}
private void init() {
mrecyclerview.setValue(course);
vishal();
}
private void vishal() {
CourseItem courseItem = new CourseItem();
courseItem.setAmount("100");
courseItem.setCourseName("vishal is there");
course = new ArrayList<>();
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
course.add(courseItem);
}
public LiveData<String> getText() {
return mText;
}
public MutableLiveData<List<CourseItem>> getUserMutableLiveData() {
return mrecyclerview;
}
}
让我给你MVVM的基本概念。你可以通过
- ModelClass。kt -您的数据模型
- 服务类。Kt -写&在这里管理你的API调用。您的模型将在这里填充。
- 最后将Service类方法调用到Mainactivity。
注意:你的xml &API管理器将根据您的要求。