Coder Social home page Coder Social logo

Comments (14)

davideas avatar davideas commented on September 21, 2024

One moment, calling addNews() you create everytime a new header object which starts with the flag hidden=true, so at the next call of showAllHeaders it displays a new different header object (even if you think it is the same content category). You should keep the reference of the same header you already have in memory, the header object must be the same for all the items under that section.

However, I've improved a little bit the displaying of the header when adding a new item with header, maybe you can use 5.0.0-SNAPSHOT, read my comment release and check the code of addItem() & addItems(): #39 (comment)

So in the end you should call once setDisplayHeadersAtStartUp() which internally calls showAllHeaders(), do this when you create the Adapter/RecyclerView after you fill the items to the adapter.

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

To clear/remove all items you can use removeRange(positionStart, itemCount) or removeItemsOfType(Integer... viewTypes) the last method is available only in SNAPSHOT release and it will be also in the next beta release: beta5. it takes care to remove/hide also the header only if all items of that section are removed, so you don't need to remove the header type!
Or you can implement your own clear() method which internally calls removeRange(...)

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

@davideas about the first issue.

calling addNews() you create everytime a new header object

I want to do this, because everytime I call addNews, i'm adding a list of news of a type, and I want a different header all the time.
I don't need to reuse headers now. How can I fix this?

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

Ok I understood, I was doing the same in my project, you need to calculate where to insert the new item keeping in the calculation all the previous displayed header, it is a bit more complicated of what you think because you might later add an item at the end of the section or at the beginning of the section.

However if you insert a new news you should add the new small item at position = 0 or at the end + 1 (adapter.getItemCount() is ok). Adapter doesn't crash since it's logic understand the bounds and use addItem(item) instead of addItem(position, item).

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

Hey @davideas ! I changed a bit my logic and now.

  1. I'm creating all possible headers (header = type of news), and put it into an array.
  2. I'm fetching news from server
  3. I'm getting more responses, each is a list of news for a category type.
  4. I'm adding the items along with the correct headers.

How I create my adapter:

  mAdapter = new MyNewsFlexibleAdapter(new ArrayList<AbstractFlexibleItem>());
        mAdapter.setDisplayHeadersAtStartUp(true);//Show Headers at startUp!

And no header is shown.
If I do showAllHeaders() after each time I add new news I will have the same problem as above (multiple headers). And news may come any time, so I can't just do showAllHeaders one time after all news are fetched from server.
Is there a solution for this problem on beta 4?

Thank you for your support.

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

@BoldijarPaul, do you initialize the list after you create new ArrayList?
Remember that the headers are attached to the items, so initialize your list and only after pass the list to the Adapter constructor.

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

@davideas

 private List<AbstractFlexibleItem> mItems;

    public MyNewsFlexibleAdapter(List<AbstractFlexibleItem> items) {
        super(items);
        mItems = items;
    }

And how I initialize my adapter

 mAdapter = new MyNewsFlexibleAdapter(new ArrayList<AbstractFlexibleItem>());
        mAdapter.setDisplayHeadersAtStartUp(true);//Show Headers at startUp!

And where i'm adding news

public void addNews(List<NewsModel> news) {
        if (news.isEmpty()) {
            return;
        }
        int categoryId = news.get(0).categoryId;
        // add first item
        MyNewsBigItem newsBigItem = new MyNewsBigItem(news.get(0), mHeaders.get(categoryId));
        mItems.add(newsBigItem);
        // add next items
        news.remove(0);
        for (NewsModel newsModel : news) {
            MyNewsSmallItem newsSmallItem = new MyNewsSmallItem(newsModel, mHeaders.get(categoryId));
            mItems.add(newsSmallItem);
        }
        mItems.add(new MyNewsAdItem(mHeaders.get(categoryId), categoryId));
  // here calling notifyDataSetChanged() will add everything in the list, but not display headers
// calling updateDataSet(mItems) will add everything in the list , and duplicate headers
// not calling anything after won't add my items to list..
        notifyDataSetChanged();
    }

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

Where is addNews in the Adapter or in the Activity? also when is it called?

If it is in the Adapter, you should use addItem() as you wrote before. Also what is mItems? I mean is it yours? because i-m sure it should be set as private in my Adapter so you should not have one in the adapter but in your "database service".
Summarizing, you have to change version to 5.0.0-SNAPSHOT as I said previously, otherwise when adding items with addItem() will not show the headers using beta4.

notifyDataSetChanged() indeed shows what is already in the mItems of the Adapter without animations, if the headers were not present in mItems then it continues to Not display any header.

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

Basically you should have 1 list of news as database where you fetch news from the server and rely on it as original list and without headers because headers are linked inside each item.
And another list already present in the Adapter to synchronize with your original list.

Why 2? because if you filter or remove an item with undo, the adapter list is modified with removals and these deletion don't have to be your database list but only in the displayed list.

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

I'm now using the SNAPSHOT version and I have this issue

 public void addNews(List<NewsModel> news) {
        if (news.isEmpty()) {
            return;
        }
        //  header
        int categoryId = news.get(0).categoryId;
        List<AbstractFlexibleItem> items = new ArrayList<>();
        // add first item
        MyNewsBigItem newsBigItem = new MyNewsBigItem(news.get(0), mHeaders.get(categoryId));
        items.add(newsBigItem);
        // add next items
        news.remove(0);
        for (ProTvNewsModel newsModel : news) {
            MyNewsSmallItem newsSmallItem = new MyNewsSmallItem(newsModel, mHeaders.get(categoryId));
            items.add(newsSmallItem);
        }
        items.add(new MyNewsAdItem(mHeaders.get(categoryId), categoryId));
        addItems(getItemCount(), items);

    }

I'm getting out of memory error.
Throwing OutOfMemoryError "Failed to allocate a 44479330 byte allocation with 10427408 free bytes and 9MB until OOM"

java.lang.OutOfMemoryError: Failed to allocate a 44479330 byte allocation with 10427408 free bytes and 9MB until OOM 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at java.lang.StringFactory.newStringFromChars(Native Method) 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629) 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at java.lang.StringBuilder.toString(StringBuilder.java:663) 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:60) 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:44) 03-21 13:16:33.595 20700-20700/com.appsrise.protv E/AndroidRuntime: at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(R

addNews is in the adapter, and gets called in the fragment.

My constructor:

public MyNewsFlexibleAdapter(List<AbstractFlexibleItem> items) {
        super(items);
    }

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

About the OOM I don't know, please check the caused by and you will found the cause. Is the adapter involved in the stack trace? StringBuilder object is not used in my adapter library.

About the addNews(), if I understand correctly: You initialize the adapter with empty non null list and then you call addItems, it should be fine I think.

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

@davideas
I'm trying to make another project with as few lines of code as possible, maybe later it can be an example project.

https://github.com/BoldijarPaul/flexible-adapter-test

Currently i'm having 2 problems

  • headers are not being shown
  • adding items later, will add them to the last header instead of at the desired header. (check addItems in adapter)

from flexibleadapter.

davideas avatar davideas commented on September 21, 2024

@BoldijarPaul, I've analyzed your code, basically all items are sticky headers that's why you don't see the colored ones sticky. Yours NormalItem extends AbstractHeaderItem while it should extends AbstractSectionableItem for example...

public class NormalItem extends AbstractSectionableItem<NormalItem.ViewHolder, HeaderItem> {

    private int mId;

    public NormalItem(int id, HeaderItem headerItem) {
        mId = id;
        header = headerItem;
    }
    ...
}

Regarding your second point, addItems() indeed adds items to the end (you added getItemCount() as parameter). To add items to the correct position, that should be calculated so you can use addItem(position, item). You can use getGlobalPositionOf(header) to get the position of the header, but also: hasHeader, hasSameHeader, getHeaderOf, getHeaderStickyOn, getSectionableOf.
I agree that can be improved to add an item at the beginning or at the end of a section. I will think about it.

from flexibleadapter.

boldijar avatar boldijar commented on September 21, 2024

@davideas Thanks for your help!
Whenever the project will have a stable version, If you want, I could fork the repo, and add a separate example just for sticky headers. 👍 , just contact me.

from flexibleadapter.

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.