Overwrite MaterialDrawer's RecyclerView - android

According to this commit, it is possible to rewrite drawer's RecyclerView with a custom one but I can't figure out how to do it.
Can anyone explain to me how to do it properly?

You can provide any RecyclerView you want with withRecyclerView(). This will then be picked instead of the default implementation. Just provide a RecyclerView object (which you have retrieved via a LayoutInflater or created programmatically) set the LayoutManager the Animator and everything else you need.
If you are interested what happens in the default behavior follow this link:
https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1576
As of your issue. It is also possible to just retrieve the default generated RecyclerView after the Drawer was built, via getRecyclerView(), which will also allow you to adjust some things like paddings, and other things.
See here: https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L214
As also discussed here:
https://github.com/mikepenz/MaterialDrawer/issues/1290

Related

How to create multilevel recyclerview in Android kotlin?

I want to create 3 level RecyclerView like tree view in kotlin. Is there any tutorial and suggestions please let me know.
I already tried so many times with ExpandableListView and 3 RecyclerView, but didn't find any proper solution.
By a multi-level RecyclerView, do you mean a RecyclerView with paths to different lists that branch depending upon which item has been selected? If that's the case, I would honestly recommend using a single adapter to cycle through multiple lists depending upon user input.
If you have a root list containing two items, each of which opens up its own list with its own unique set of data, you can easily implement code that notifies the adapter of which item in the root list was selected. From there, the adapter can update and switch the view accordingly. This can be applied to series of lists ad nauseam if you so choose, though I can't say I would recommend this kind of method for incredibly complex webs of lists that interact with each other.
Like Ircover said in their comment, I don't think a tree is necessary in this situation either, if only because (1) as stated, it isn't really best practice to do so for the kind of application you're making, and (2) it may unnecessarily over-complicate whatever you're trying to achieve with these branching paths in the first place. If you're willing/able to provide more information about what you're trying to do here, that may help others help you more precisely than I can :)
Full disclosure here: the blog post linked above is not a direct match that will solve your problem - it pertains specifically to displaying different types of data sets (from data classes and what have you,) but employs code that shows how different sets of data can be switched between in a single RecyclerView. Even if it isn't a god-sent solution or is only halfway helpful in solving your problem, I think it can provide some useful information to you.
Maybe a slightly dated question but looking into the same concept and I located this page https://blog.usejournal.com/multi-level-expandable-recycler-view-e75cf1f4ac4b .
They have made a single adapter class to take care of all the navigation and so far seems to be the least complicated example of an expandable RecyclerView, though not in kotlin.

How do I implement staggered views in reading order?

Here is what I want to implement:
I'm trying to display definitions I'm getting from the web (right now they're coming from dummy data, but that's neither here nor there).
My first thought was to use a horizontal StaggeredGridView. There are two problems with this. The views are not laid out in the correct order and the span count is not set dynamically. Also, I found out that calling setSpanCount() in onLayoutChildren() causes an error at run time.
Of course, I could just have a LinearLayout and forego my attempt to recycle views. That would work in most cases, but if the user did happen upon a length definition, it would be very memory inefficient.
With that in mind, I resolved to subclass StaggeredGridLayoutManager. And this is where I need your help. Here are the resources I found in my own search:
Google's Implementation of a LinearLayoutManager
A Guide Implementing a Custom LayoutManager
Another Guide for Implementing a Custom LayoutManager
An Overview of a Few Principles to Follow when Implementing a Custom LayoutManager
I went through each one of these, but I cannot mentally separate out what parts are there for their specific use-cases and what parts are common to all custom implementations. If someone could be so kind as to point me in the direction of a resource or explain what all the methods I have to call in what order and for what purpose, I would be immensely grateful. Thank you!
Your first screenshot looks like a perfect case for Google's FlexboxLayout library. You can use the FlexboxLayoutManager with RecyclerView to get exactly what you showed.

WearableListView Header

I am trying to create a WearableListView similar to the one in the default Settings app. I have a WearableListView that scrolls, and animates each element. All I need now is a header on top of the list that scrolls up when the list scrolls. This is the same behavior that the title 'Settings' has in the Settings app.
The only method I have found to do this, used here: How to Set the Layout for topEmptyRegion, implements a WearableListView.OnScrollListener(). Then uses the onAbsoluteScrollChange() method to set the displacement of the TextView used as a header. But, according to the Android Developer References, this method is deprecated. The documentation even states: BE ADVISED DO NOT USE THIS This might provide wrong values when the contents of a RecyclerView change.
I would like to know if anyone else has found a way to implement a header that behaves this way, one that is not deprecated. Possibly something that I missed in the Android Wear documentation?
EDIT:
I tried to implement this method anyway. As it turns out, WearableListView.setOnScrollListener() would not take a new WearableListView.OnScrollListener(). Instead it asked for its parent RecyclerView.OnScrollListener, which does not have a onAbsoluteScrollChange() method. It does have an onScrolled() method, which I tried to implement. As it turns out, this completely overrides the default scroll listener for WearableListView and makes it so the centered item can be off-center. Definitely not the effect I was looking for.
Since onScroll() is not deprecated, you may be able to accomplish the same by implementing that in your listener:
#Override
public void onScroll(int scroll) {
header.setY(header.getY() - scroll);
}
where header is the header component.
EDIT: Make sure you use WearableListView.addOnScrollListener not WearableListView.setOnScrollListener()

How to switch an android view based on a condition

I am creating an Android application that will mainly consists of ListViews in each activity. What I hope to achieve is a mechanism that will check to see if network connectivity is present and if so then then the data should be retrieved and supplied to the ListView. If there's no data present or the internet connection is unavailable then a seperate view should be loaded.
Which way is most efficient do to this. So far I have seen answers about using a ViewSwitcher or Viewflipper but I am not sure that's the right approach. Should I use fragments and then load a particular fragment based on the condition that was met? I just need some advice on how to accomplish this.
Have you considered just swapping the adapter? This question might be of some use to you.
I guess you might add some kind of factory method that will decide what adapter to instantiate depending on connectivity availability. Thus you will control both the data to be used and the appearance of list entries (I assume you will be using some custom extension of one of the existing adapters).
The best way is going to be to use the emptyView that's provided as part of the AdapterView base class. You just set the adapter as per usual, but if no data is retrieved (i.e. your adapter's data source is empty) you will show the empty view in place of the list. This empty view can be anything you want it to be.
As #Ivan says you should consider setting adapters in a dinamic way.
And about the fragments thing, only consider using independent fragments (or ListFragments) for this if you are going to re-use that fragments in other activities to avoid code duplication.

How to make a ListView with draggable items?

I was looking for a ListPreference in which the user can change the order of items from a list. The items would be draggable and can be re-ordered by the user.
I saw this in my custom ROM (and I'm almost sure I saw it in Cyanogenmod) for the QuickPanel. Here's a screenshot to get the idea through:
I know how I can make custom ListView items and set the icon to indicate that the items are draggable, but I don't know how to make them draggable, and change the order accordingly. As for saving them in the preferences, I found this which could be implemented easily.
PS: I know Cyanogenmod is open-source, but I couldn't find the source for this particular thing :( The closest I could get was this, which should be somewhere near the other screen...
Thanks in advance for any tip about this.
UPDATE: I ended up using the files from the accepted answer, with additions and modifications. I am listing them here for further reference.
Use a custom Adapter (ArrayAdapter in my case), to implement the visual feedback that this item is draggable, which is an ImageView near the TextView. This is optional.
Set a DragListener and RemoveListener to update the list accordingly. The ListView does not do that automatically. And it depends on the Adapter you are using.
There was a line that casted a View to a ViewGroup, it made some errors, so I removed the cast without any issue, it was not needed. (in the onInterceptTouchEvent method).
Change mRemoveMode = 1; in the constructor of TouchInterceptor, or one of: FLING = 0; SLIDE = 1; TRASH = 2;. I think for TRASH, a resource should be available too.
I actually took the file not from the answer's link but from the Cyanogenmod one, which I already had, but I guess these files were the same.
These are the actual files in the project (at r12, at the time of writing):
The Preference using this ListView
The ListActivity with the listeners and the Adapter
The actual ListView
I hope it helps somebody else :)
There is no built-in widget to do this, but you may want take a look at the custom implementation used by the AOSP music player to allow for re-ordering of songs in playlists.
TouchInterceptor.java
That's the class which is extending ListView and is responsible for handling all of the MotionEvents and re-ordering its items, as well as detecting swipes for deleting items. You can see it implemented into an activity here in the TrackBrowserActivity.java class.
It has three interfaces you should also be aware of: DragListener, DropListener, and RemoveListener. You can use these interfaces to provide it callbacks to fire for those events, so that you can update changes made to the list order to your SavedPreferences (since the ListView will not handle that for you).
You can easily extend or modify the TouchInterceptor class to add additional functionality since the low-level stuff is all there for you.

Categories

Resources