Giải pháp chuyển đổi số trọn gói cho doanh nghiệp Việt
Tên file: CarListFragment.java
Nơi lưu file: java/com/vantinviet/adayroionline/ui/car/list
Nội dung file:
//Tên gói
package com.vantinviet.adayroionline.ui.car.list;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import com.vantinviet.adayroionline.Config;
import com.vantinviet.adayroionline.R;
import com.vantinviet.adayroionline.binding.FragmentDataBindingComponent;
//import Fragment_Car_List
//import CarListAdapter
import com.vantinviet.adayroionline.databinding.FragmentCarListBinding;
import com.vantinviet.adayroionline.ui.car.list.adapter.CarListAdapter;
import com.vantinviet.adayroionline.ui.common.DataBoundListAdapter;
import com.vantinviet.adayroionline.ui.common.PSFragment;
import com.vantinviet.adayroionline.utils.AutoClearedValue;
import com.vantinviet.adayroionline.utils.PSDialogMsg;
import com.vantinviet.adayroionline.utils.Utils;
import com.vantinviet.adayroionline.viewmodel.car.CarViewModel;
import com.vantinviet.adayroionline.viewobject.Car;
import com.vantinviet.adayroionline.viewobject.common.Resource;
import com.vantinviet.adayroionline.viewobject.common.Status;
import java.util.List;
public class CarListFragment extends PSFragment implements DataBoundListAdapter.DiffUtilDispatchedInterface {
private final androidx.databinding.DataBindingComponent dataBindingComponent = new FragmentDataBindingComponent(this);
private CarViewModel carViewModel;
private PSDialogMsg psDialogMsg;
private List<Car> carList;
@VisibleForTesting
private AutoClearedValue<FragmentCarListBinding> fragmentCarListBindingAutoClearedValue;
private AutoClearedValue<CarListAdapter> carListAdapterAutoClearedValue;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//set car fragment sử dụng layout fragment_car_list
FragmentCarListBinding fragmentCarListBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_car_list, container, false, dataBindingComponent);
fragmentCarListBindingAutoClearedValue = new AutoClearedValue<>(this, fragmentCarListBinding);
return fragmentCarListBindingAutoClearedValue.get().getRoot();
}
//Khởi tạo các ui
@Override
protected void initUIAndActions() {
psDialogMsg = new PSDialogMsg(getActivity(), false);
//kiểm tra khi có sự kiện scroll của carListRecyclerView
fragmentCarListBindingAutoClearedValue.get().carListRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//Khi người dùng kéo xong
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager layoutManager = (LinearLayoutManager)
recyclerView.getLayoutManager();
if (layoutManager != null) {
int lastPosition = layoutManager
.findLastVisibleItemPosition();
if (lastPosition == carListAdapterAutoClearedValue.get().getItemCount() - 1) {
if (!fragmentCarListBindingAutoClearedValue.get().getLoadingMore() && !carViewModel.forceEndLoading) {
carViewModel.loadingDirection = Utils.LoadingDirection.bottom;
int limit = Config.LIST_CARS_COUNT;
carViewModel.start = carViewModel.start + limit;
carViewModel.setLoadingState(true);
carViewModel.setNextPageCarsObj(String.valueOf(Config.LIST_CARS_COUNT), String.valueOf(carViewModel.start));
}
}
}
}
});
fragmentCarListBindingAutoClearedValue.get().swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.view__primary_line));
fragmentCarListBindingAutoClearedValue.get().swipeRefresh.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.global__primary));
fragmentCarListBindingAutoClearedValue.get().swipeRefresh.setOnRefreshListener(() -> {
carViewModel.loadingDirection = Utils.LoadingDirection.top;
// reset carViewModel.offset
carViewModel.start = 0;
// reset carViewModel.forceEndLoading
carViewModel.forceEndLoading = false;
carViewModel.setCarsObj(String.valueOf(Config.LIST_CARS_COUNT), String.valueOf(carViewModel.start));
// update live data
});
fragmentCarListBindingAutoClearedValue.get().AddCarButton.setOnClickListener(v -> {
navigationController.navigateToCarFormActivity(getActivity(),"");
});
}
@Override
protected void initViewModels() {
carViewModel = ViewModelProviders.of(this, viewModelFactory).get(CarViewModel.class);
}
@Override
protected void initAdapters() {
CarListAdapter nvAdapter = new CarListAdapter(dataBindingComponent,car -> navigationController.navigateToCarDetailActivity/*Hiện tại hàm này chưa được xây dựng, chúng ta sẽ làm ở bước này nhấn vào đây để xem [Chú ý là bước này chúng ta sẽ làm sau nhé] */(CarListFragment.this.getActivity(), car.id), this);
this.carListAdapterAutoClearedValue = new AutoClearedValue<>(this, nvAdapter);
fragmentCarListBindingAutoClearedValue.get().carListRecyclerView.setAdapter(carListAdapterAutoClearedValue.get());
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
// this method is called
// when the item is moved.
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// this method is called when we swipe our item to right direction.
// on below line we are getting the item at a particular position.
Car car = carList.get(viewHolder.getAdapterPosition());
// below line is to get the position
// of the item at that position.
int position = viewHolder.getAdapterPosition();
psDialogMsg.showConfirmDialog("Bạn có chắc chăn muốn xóa không ?","Chắc chắn","Hủy");
psDialogMsg.show();
psDialogMsg.okButton.setOnClickListener(view -> {
psDialogMsg.cancel();
// this method is called when item is swiped.
// below line is to remove item from our array list.
carList.remove(viewHolder.getAdapterPosition());
// below line is to notify our item is removed from adapter.
carListAdapterAutoClearedValue.get().notifyItemRemoved(viewHolder.getAdapterPosition());
// below line is to display our snackbar with action.
Snackbar.make(fragmentCarListBindingAutoClearedValue.get().carListRecyclerView, car.carName, Snackbar.LENGTH_LONG).setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
// adding on click listener to our action of snack bar.
// below line is to add our item to array list with a position.
carList.add(position, car);
// below line is to notify item is
// added to our adapter class.
carListAdapterAutoClearedValue.get().notifyItemInserted(position);
}
}).show();
carViewModel.setDeleteCarObj(car.id);
});
psDialogMsg.cancelButton.setOnClickListener(view -> {
psDialogMsg.cancel();
carList.add(position, car);
carListAdapterAutoClearedValue.get().notifyItemInserted(position);
});
}
// at last we are adding this
// to our recycler view.
}).attachToRecyclerView(fragmentCarListBindingAutoClearedValue.get().carListRecyclerView);
}
//Khởi tạo dữ liệu
@Override
protected void initData() {
// bắt đầu lấy dữ liệu
carViewModel.setCarsObj(String.valueOf(Config.LIST_CARS_COUNT), String.valueOf(carViewModel.start));
//Quan sát khi có dữ liệu được lấy về
carViewModel.getCarsData().observe(this, result -> {
if (result != null) {
switch (result.status) {
//Nếu lấy dữ liệu về thành công
case SUCCESS:
carList=result.data;
replaceCarList(result.data);
carViewModel.setLoadingState(false);
break;
//nếu vẫn đang trong quá trình tải
case LOADING:
replaceCarList(result.data);
break;
//nếu phát sinh một lỗi bất kỳ
case ERROR:
carViewModel.setLoadingState(false);
break;
}
}
});
//Quan sát khi người dùng kéo scroll xuống dưới
carViewModel.getNextPageCarsData().observe(this, state -> {
if (state != null) {
if (state.status == Status.ERROR) {
carViewModel.setLoadingState(false);
carViewModel.forceEndLoading = true;
}
}
});
carViewModel.getLoadingState().observe(this, loadingState -> {
fragmentCarListBindingAutoClearedValue.get().setLoadingMore(carViewModel.isLoading);
if (loadingState != null && !loadingState) {
fragmentCarListBindingAutoClearedValue.get().swipeRefresh.setRefreshing(false);
}
});
LiveData<Resource<Boolean>> delete_car = carViewModel.getDeletedCar();
delete_car.observe(this, status_Resource -> {
if (status_Resource != null) {
psDialogMsg.showSuccessDialog(getString(R.string.str_delete_car_success),"Ok");
psDialogMsg.show();
}
});
}
//Thực hiện việc cập nhật lại dữ liệu car
private void replaceCarList(List<Car> carList) {
Utils.psLog("carList",carList.toString());
this.carListAdapterAutoClearedValue.get().replace(carList);
fragmentCarListBindingAutoClearedValue.get().executePendingBindings();
}
@Override
public void onDispatched() {
if (carViewModel.loadingDirection == Utils.LoadingDirection.top) {
if (fragmentCarListBindingAutoClearedValue.get().carListRecyclerView != null) {
LinearLayoutManager layoutManager = (LinearLayoutManager)
fragmentCarListBindingAutoClearedValue.get().carListRecyclerView.getLayoutManager();
if (layoutManager != null) {
layoutManager.scrollToPosition(0);
}
}
}
}
}
Chưa có câu hỏi nào cho bài này.