infinum / mjolnirrecyclerview Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] This library is no longer maintained and it will not receive any more updates.
License: Apache License 2.0
[DEPRECATED] This library is no longer maintained and it will not receive any more updates.
License: Apache License 2.0
How to implement custom onCreateViewHolder. The situation is I want to create a header that have an image with 2 buttons be added as a header to the listview. How to fetch the layout in onCreateHeaderViewHolder
?
@Override
protected MjolnirViewHolder onCreateHeaderViewHolder() {
return new HeaderViewHolder();
}
// Header Holder
public class HeaderViewHolder extends MjolnirViewHolder<Client> {
public HeaderViewHolder(View itemView) {
super(itemView);
}
@Override
protected void bind(Client item, int position, List<Object> payloads) {
}
}
Add method overloading for array adapter methods so that the consumer can decide whether the notifyDataSetChanged() should be called.
As stated in the title, the library throws an IllegalStateException.
This can be reproduced by passing an empty list to the adapter and then calling the add method:
add(item, 0)
The problem is the library validates if the index is greater or equal to items.size()
. Since the list is empty and the index is 0 this validates to true.
There is private hasHeader()
method in MjolnirRecyclerAdapter
. It would be great if you can make it protected
. It would be useful, for example, when you need to call notifyItemChanged method and you need to calculate the exact position of the item. In that case you'll need to know whether or not header is added.
Currently when you are using one empty view and then set new empty view (with empty list), old empty view stays visible and then you have both old and new empty view visible.
Probably would be better if library hide old empty view when setting new one.
Empty view is never shown, and it seems to disrupt headers aswell.
Test case:
If I only have a header, and no items in the lists, the entire screen is blank (neither header nor empty view can be seen)
EventHeaderView header = EventHeaderView_.build(this);
header.setEvent(eventId);
mAdapter.addHeader(header, true);
EmptyView empty = EmptyView_.build(this);
empty.setText(getString(R.string.HOME_FEED_EMPTY_LIST_TEXT));
recycler.setEmptyView(empty);
If only the empty view is set (no header, no extra items), it's again completely blank and nothing is showing.
EventHeaderView header = EventHeaderView_.build(this);
header.setEvent(eventId);
//mAdapter.addHeader(header, true);
EmptyView empty = EmptyView_.build(this);
empty.setText(getString(R.string.HOME_FEED_EMPTY_LIST_TEXT));
recycler.setEmptyView(empty);
If we only have the header and no empty view, it behaves as expected, only the header shows.
EventHeaderView header = EventHeaderView_.build(this);
header.setEvent(eventId);
mAdapter.addHeader(header, true);
EmptyView empty = EmptyView_.build(this);
empty.setText(getString(R.string.HOME_FEED_EMPTY_LIST_TEXT));
//recycler.setEmptyView(empty);
And to eliminate the possibility that my empty view is faulty, I tried this:
And the result is that the empty view shows properly as the header.
EventHeaderView header = EventHeaderView_.build(this);
header.setEvent(eventId);
//mAdapter.addHeader(header, true);
EmptyView empty = EmptyView_.build(this);
empty.setText(getString(R.string.HOME_FEED_EMPTY_LIST_TEXT));
mAdapter.addHeader(empty, true);
The expected behaviour when both header and empty views are set, and there are no items present, both should show (empty view below header).
Empty view should ONLY show when extra items are absent.
Footer view is in xml defined with match_parent
width. However when adding it to adapter it somehow gets to wrap_content and positions progress to left.
this is footer view xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/default_background"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progress"
style="?android:attr/android:progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/small_padding"/>
</LinearLayout>
this is how it is rendered: http://take.ms/HJpqX
Same view on ListView footer works properly
Currently, there is no support for multiple item ViewHolders in MjolnirRecyclerAdapter.
This could easily be solved in 2 different ways, which both give a lot of flexibility to the developers:
Add method with protected visibility to the end of onCreateViewHolder() method. By default, it's implementation will return null as a result. By overriding this method, developers can provide it's custom implementation with different ViewHolders.
Override onCreateViewHolder() and provide custom implementation, but force developers to call super on that method (@callsuper annotation).
We should define how does Mjonir handle cases when we want to add header/footer, but adapter already has defined header and footer.
MjolnirAdapter currently has limitation that it doesn't support multiple headers and views - we can communicate this better to the developer by using one of the few possible solutions:
I would like to first address this issue and later add support for multiple headers/footers, as this will probably introduce some breaking changes.
@kjurkovic @ikocijan @reisub what's your opinion about this?
For section "1. Header & footer support" of the README.md file, "adapter.setHeader(View view)" is shown twice, when it should only be shown once, and "adapter.setFooter(View view)" be shown for the second entry.
There should be method to clear items from adapter
If you call addFooter(view, true), then later call removeFooter(), and again call addFooter(view, true) the old footer will still be there, I suspect it has something to do with the FooterViewHolder and HeaderViewHolder classes since they are only updated in onCreateViewHolder.
If you have a very small list, won't this pose a problem with the header/footer never actually updating their contents if you replace the view rather than modifying an existing one?
The library doesn't seem to take headers into account when removing items.
When I do adapter.remove(item);
It will remove the item before that if I have a header, and if I remove the very first item, it crashes with the following stacktrace:
11-04 14:20:05.422 10076-10076/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: se.thefarm.yourblock, PID: 10076
java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{49e503 position=0 id=-1, oldPos=-1, pLpos:-1}
at android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:692)
at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
at android.support.v7.widget.RecyclerView.addAnimatingView(RecyclerView.java:1178)
at android.support.v7.widget.RecyclerView.animateDisappearance(RecyclerView.java:3535)
at android.support.v7.widget.RecyclerView$4.processDisappeared(RecyclerView.java:456)
at android.support.v7.widget.ViewInfoStore.process(ViewInfoStore.java:246)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3385)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3135)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1549)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
If I use multiple view types such as
@Override
protected int getAdditionalItemViewType(int position) {
return get(position).getType() == Usage.Type.LABEL ? YEAR : ITEM;
}
and I add header with adapter.setHeader(View view);
, method getAdditionalItemViewType
will receive wrong positions for items.
In ArrayAdapter methods, we don't need to subtract footer.size() from index, only headers.size()
Looks like the adapter need to be initialised out of block Handler
.
adapter = new SimpleAdapter(EmptyViewActivity.this);
If RecyclerView has less items than it can maximum has visible (ie. RecyclerView can take 5 items on the screen, but adapter contains only 3 items) and it has set nextPageListener
then IllegalStateException
happens when adding next page without any delay.
Steps to reproduce:
nextPageListener
and add just few items in adapter (to take less than recycler size)onScrolledToNextPage
add few more items (I used addAll
method)Workaround is to wrap code that is in onScrolledToNextPage
into some runnable and post it on main handler.
Solution is to use main handler for posting task that would call nextPageListener.onScrolledToNextPage
inside of onBindViewHolder
method of MjolnirRecyclerAdapter
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.