从活动/片段获取/设置回收者视图项的值

时间:2018-01-16 08:16:52

标签: android adapter

我有一个Fragment,我需要从Fragment运行一个方法并更改recycleler视图项的值。 问题是我不知道如何从活动中访问回收站视图中的项目变量,因为项目由适配器而不是活动本身提供动力,因为活动/片段所做的唯一事情就是初始化Recyclerview

我的主要目标是更改片段中每个项目的textview。

有解决方法吗?

我想要运行的功能(片段中):

  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) {
           //setting the item textview each time the timer ticks.
        }

        public void onFinish() {
           // setting the item textview when the timer finishes.
        }
    }.start();
}

适配器:

 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;
    }

    public ItemUpcomingProductBinding getBinding() {
        return binding;
    }



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

对不起,很长的帖子。 我好几天都遇到这个问题了,我希望你们有一个解决方案。 感谢~~

2 个答案:

答案 0 :(得分:0)

将方法添加到您的UpcomingProductAdapter。

=== undefined

然后在你的片段中使用你的适配器来改变它。

public void updateProduct(int position, Product product) {
    this.products.set(position, product);
    notifyItemChanged(position);
}

在您的代码中,您可能有内存泄漏,因此请尝试阅读它。我也更喜欢使用 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) { //setting the item textview each time the timer ticks. adapter.updateProduct(somePosition, someProduct); } public void onFinish() { // setting the item textview when the timer finishes. } }.start(); } http://blog.trsquarelab.com/2016/01/data-binding-in-android-recyclerview.html

答案 1 :(得分:0)

  1. 将片段附加到活动时,可以存储片段的引用。然后在此引用上调用方法。
  2. 或者你可以这样做:
  3. function func(){ return y } (function() { var exLog = console.log; console.log = function(msg) { exLog.apply(this, arguments); alert(msg); } })() func()