Giter Club home page Giter Club logo

Comments (26)

drakeet avatar drakeet commented on July 29, 2024 23

又简单写了下,所谓 "继承 MultiTypeAdapter 提供接收数据方法,在方法中进行 Diff":

public class XDiffMultiTypeAdapter extends MultiTypeAdapter {

    private List<X> oldItems;


    public XDiffMultiTypeAdapter(@NonNull List<X> items) {
        super(items);
    }


    public void setItems(List<X> newItems) {
        final XDiffCallback diffCallback = new XDiffCallback(oldItems, newItems);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        this.oldItems.clear();
        this.oldItems.addAll(newItems);
        diffResult.dispatchUpdatesTo(this);
    }


    static class XDiffCallback extends DiffUtil.Callback {

        private final List<X> oldList;
        private final List<X> newList;


        XDiffCallback(List<X> oldList, List<X> newList) {
            this.oldList = oldList;
            this.newList = newList;
        }


        @Override
        public int getOldListSize() {
            return oldList.size();
        }


        @Override
        public int getNewListSize() {
            return newList.size();
        }


        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldList.get(oldItemPosition).id == newList.get(newItemPosition).id;
        }


        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            final X oldItem = oldList.get(oldItemPosition);
            final X newItem = newList.get(newItemPosition);
            return Objects.equals(oldItem, newItem);
        }
    }
}

好了,我已经双方面用代码解释了 "可以在 Activity 中进行 Diff,或者继承 MultiTypeAdapter 提供接收数据方法,在方法中进行 Diff. " 这句话,最后有没有发现我这句话已经非常言简意赅。

注意:如果你要容纳多种 item types,应该使用 ? 替代我这里的 X.

from multitype.

drakeet avatar drakeet commented on July 29, 2024 4

@zhengjieAdriod 我不是大神。diff callback 中 id 不是必须的,你自己思考,我不能一直手把手告诉你每一步应该怎么做。

from multitype.

drakeet avatar drakeet commented on July 29, 2024 2

不知道那句话你 24 遍读完了没,刚简单写了下所谓 "在 Activity 中进行 Diff" 的示例,代码如下:

public class FlexActivity extends AppCompatActivity {

    List<X> oldItems;
    MultiTypeAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flex);
        /* Init your oldItems, adapter and recyclerView ... */
        /* Fetch your data from server and call onLoadData */
    }


    void onLoadData(List<X> newItems) {
        final XDiffCallback diffCallback = new XDiffCallback(oldItems, newItems);
        final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        this.oldItems.clear();
        this.oldItems.addAll(newItems);
        diffResult.dispatchUpdatesTo(adapter);
    }


    static class X {
        int id;
    }


    static class XDiffCallback extends DiffUtil.Callback {

        private final List<X> oldList;
        private final List<X> newList;


        XDiffCallback(List<X> oldList, List<X> newList) {
            this.oldList = oldList;
            this.newList = newList;
        }


        @Override
        public int getOldListSize() {
            return oldList.size();
        }


        @Override
        public int getNewListSize() {
            return newList.size();
        }


        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldList.get(oldItemPosition).id == newList.get(newItemPosition).id;
        }


        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            final X oldItem = oldList.get(oldItemPosition);
            final X newItem = newList.get(newItemPosition);
            return Objects.equals(oldItem, newItem);
        }
    }
}

看完这个代码后,你可以再读几遍那句话,看看是不是说得一点没错。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

请把你的代码和使用的 MultiType 版本号贴出来,并描述清楚问题,我可以帮你看看。

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

https://github.com/mcxtzhang/SupportDemos 我看了他这个文章之后,就在他的demo中添加MultiType 的功能。意思就是:在他这个单一的itemtype中新添加一个itemtype, 并使用他demo中的diff功能。 实现的过程中,需要用到三个参数的onBindViewHolder();问题就是这个方法,应该在哪里复写?我是在MultiTypeAdapter的子类中复写的。

from multitype.

frankfancode avatar frankfancode commented on July 29, 2024

我也在做类似的功能,把 数据存储到 ViewHolder 中,在 onBindViewHolder 时 比较一下 ViewHolder 里面保存的数据 和 item 里面传入的数据,如果相同就不刷新了。
我觉得这个diff 的功能还是比较常用的,尤其是用这个库的会有很多不同的item,估计会有很多复杂的 item,重复刷新可能滑动会比较卡。

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

@frankfancode 请问你是在哪里复写这个三个参数的onBindViewHolder ()方法的??

from multitype.

drakeet avatar drakeet commented on July 29, 2024

如果是为了 diff,不应该覆写 onBindViewHolder,onBindViewHolder 是用于绑定数据的而不是让人来做数据筛选或处理的。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

其实我这句话已经说得非常清楚了,不理解的话你可以读 24 遍后再想想:

可以在 Activity 中进行 Diff,或者继承 MultiTypeAdapter 提供接收数据方法,在方法中进行 Diff.

若 24 遍后还不理解,待会儿我给你写个示例。

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

这篇文章我在这儿看了两天了,只是会用了,还是不能通过他的灵活性进行功能添加。我再想想..

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

@drakeet 文章里所说的diff 就是v7包下的DiffUtil吗?

from multitype.

drakeet avatar drakeet commented on July 29, 2024

@zhengjieAdriod 没有限定,可以是 v7 DiffUtil,也可以是其他 DiffUtil 或你自己造的 DiffUtil,文章里说了,这不属于 MultiType 的范畴,MultiType 不会去管你的数据 diff,只接受数据,这个数据可以是全量数据,也可以是你 diff 后的数据。

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

万分感谢!!

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

确实非常言简意赅,对我这个新手不好理解。真是有幸膜拜到大神的代码。

XDiffCallback 并没有重写 public Object getChangePayload(int oldItemPosition, int newItemPosition) ;
该方法, 返回 一个 代表着新老item的改变内容的 payload对象。 该对象会在三个参数的onBindViewHolder ()中使用。以达到只改变item上的部分数据。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

@zhengjieAdriod 不客气 ^ ^
getChangePayload 这个方法你可以自行根据你的需要进行覆写。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

@frankfancode

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

例如哪些需要啊?

from multitype.

drakeet avatar drakeet commented on July 29, 2024

这方法的字面意思是:一个 item 中,具体是哪些字段数据产生了变化。
它被调用的时机是:areItemsTheSame() 返回 trueareContentsTheSame() 返回 false.

When areItemsTheSame(int, int) returns true for two items and areContentsTheSame(int, int) returns false for them, DiffUtil calls this method to get a payload about the change.

另外,这个方法可以不覆写,它的返回值是 @Nullable.

from multitype.

drakeet avatar drakeet commented on July 29, 2024

这已经不属于 MultiType 的范畴了,具体内容你应该自行学习使用。经过一番努力后实在不懂,我可以再帮忙看看。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

如果你想学习这个方法,我可以给你提供一篇非常好且详尽的文章:

https://medium.com/@nullthemall/diffutil-is-a-must-797502bc1149#.l3a13i5sq

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

好的, 感谢大神!

from multitype.

zhengjieAdriod avatar zhengjieAdriod commented on July 29, 2024

`大神,public boolean areItemsTheSame(int oldItemPosition, int newItemPosition)方法中要做类型判断吗?拿不到id这个字段的
class DiffCallBack02 extends DiffUtil.Callback {

private final List<T> oldList;
private final List<T> newList;


DiffCallBack02(List<T> oldList, List<T> newList) {
    this.oldList = oldList;
    this.newList = newList;
}


@Override
public int getOldListSize() {
    return oldList.size();
}


@Override
public int getNewListSize() {
    return newList.size();
}


@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
    T oldT = oldList.get(oldItemPosition);
    T newT = newList.get(newItemPosition);
    //这块要做bean类型的判断吗??
    return oldList.get(oldItemPosition).id == newList.get(newItemPosition).id;
}


@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
    final T oldItem = oldList.get(oldItemPosition);
    final T newItem = newList.get(newItemPosition);
    return Objects.equals(oldItem, newItem);
}

}`

from multitype.

frankfancode avatar frankfancode commented on July 29, 2024

@drakeet 收到,相当赞

from multitype.

zhangle1 avatar zhangle1 commented on July 29, 2024

刚想问的问题 发现有好多小伙伴多问了0.0

from multitype.

anson39f avatar anson39f commented on July 29, 2024

@drakeet 找不到这个网址:https://medium.com/@nullthemall/diffutil-is-a-must-797502bc1149#.l3a13i5sq

from multitype.

drakeet avatar drakeet commented on July 29, 2024

@anson39f 我这边是可以打开的,如果打不开,你需要科学上网。

from multitype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.