RecyclerView MVVM项目与CountDownTimer滚动滞后

时间:2018-01-15 04:59:25

标签: android mvvm android-recyclerview

我有一个带有GridLayout的RecyclerView,并且在每个项目中我都显示了一个CountDownTimer。 (所有数据均来自数据库) 但随着我越来越多地上下滚动,滚动滞后。我怀疑这是因为我正在为每个项目单独调用计时器功能,因此每个项目都会消耗大量内存,因为项目越来越多。

正如我上面提到的,我使用MVVM方式,因此在每个项目的视图模型中,我使用双向数据绑定返回String:

ITEM VIEWMODEL:

public class UpcomingProductViewModel {

private UpcomingFragment fragment;
private ItemUpcomingProductBinding binding;
public ObservableField<ProductData> productData = new ObservableField<>();
public ObservableField<String> timer = new ObservableField<>("");

public UpcomingProductViewModel(UpcomingFragment fragment, ItemUpcomingProductBinding binding, ProductData product) {
    this.fragment = fragment;
    this.binding = binding;
    this.productData.set(product);

    if (productData.get() != null) {
        countDown(productData.get().getStart_date());
    }
}

public void countDown(String date) {

    DateTime today = DateTime.now();
    DateTime nextMeeting = Utilities.convertDate(date);

    Duration duration = new Duration(today, nextMeeting);

    new CountDownTimer(duration.getMillis(), 1000) {

        public void onTick(long millisUntilFinished) {
            timer.set(Utilities.formatMillisForProduct(millisUntilFinished));
        }

        public void onFinish() {
            timer.set("0 days 00:00:00");
        }
    }.start();
}

ITEM XML:

<TextView
            android:id="@+id/countDownTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fontPath="fonts/futuramedium.ttf"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/general_14"
            android:text="@{viewModel.timer}"/>

上面的xml声明我通过使用ObservableField设置时间来使用双向数据绑定。 :

public ObservableField<String> timer = new ObservableField<>("");    

过去几天我一直在阅读很多关于我案子的文章,不幸的是,我们找不到答案了。主要问题是每个项目占用太多内存,因此滞后于整个回收站视图,并且滞后于滚动。

希望你们能够提供答案。 很抱歉长时间阅读。

ADAPTER:

public class UpcomingProductAdapter extends RecyclerView.Adapter<UpcomingProductAdapter.UpcomingProductViewHolder> {

public UpcomingFragment fragment;
public ArrayList<ProductData> products = new ArrayList<>();

public UpcomingProductAdapter(UpcomingFragment fragment) {
    this.fragment = fragment;
}

public void addProducts(ArrayList<ProductData> products) {
    this.products.addAll(products);
    notifyDataSetChanged();
}

public void clearProduct() {
    products.clear();
    notifyDataSetChanged();
}

@Override
public UpcomingProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ItemUpcomingProductBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(fragment.getContext()),
            R.layout.item_upcoming_product,
            parent,
            false
    );
    return new UpcomingProductViewHolder(binding);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public void onBindViewHolder(UpcomingProductViewHolder holder, int position) {
    holder.bindProduct(products.get(position), position);
}

@Override
public int getItemCount() {
    return products.size();
}

class UpcomingProductViewHolder extends RecyclerView.ViewHolder {

    ItemUpcomingProductBinding binding;

    UpcomingProductViewHolder(ItemUpcomingProductBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    void bindProduct(ProductData product, int position) {
        binding.setViewModel(new UpcomingProductViewModel(fragment, binding, product));
    }
}

0 个答案:

没有答案