Thursday, August 7, 2014

ExpandableListView With Swipe To Dismiss

When I first started writing my latest app, +task, a few months ago, I initially decided to go with an ExpandableListView, basically just a ListView with an extra layer for group and child.


It all looked good until I started thinking about the finishing touches some more, particularly being able to swipe to dismiss the tasks. Unfortunately, the ExpandableListView didn't seem to be too popular.

I started looking through Github and there were a ton of projects with Swipe functionality, most of which could be found in the Libraries For Developers app I mentioned previously. I started digging in a little, but from what I could tell, all the best ones didn't support ExpandableListView, only the standard ListView. The one that did seems like it might support ExpandableListView was Tim Roes SwipeToDismissUndoList. Unfortunately there was a couple of things that were beyond me at that point -

1 - I didn't know how to include libraries with Android Studio yet (I have since figured it out).

2 - Even if I had got the library included, I don't think I would have figured it out using his sample (again, I've improved since then).

So, figuring the swiping was important, I restyled my app and got rid of the ExpandableListView. Well, I wish I hadn't. I've finally figured out how to include a library and got his sample working.

The original Github project works for an AbsListView which is where all ListViews are derived from, including GridView, ExpandableListView and regular old ListView. It worked great, but when you implemented his OnDismiss callback, it would provide you with a single position, even though there were multiple levels, like this


Which obviously doesn't make any sense when there might be 50 items under Group1 that are just not visible right now.

Luckily someone over at Google already thought this one through and created the following methods on ExpandableListView


Like I said, genius.

So, finally, to wrap all this up. I forked my very first project in Github and made the changes required to automatically use the above methods for an ExpandableListView.

It's hosted here, and works exactly the same way as Tim Roes project above, except only exclusively for ExpandableListView widgets. There's a sample in the description that'll see you through any confusion from here. Enjoy.

Wednesday, August 6, 2014

Add A Project Library To Android Studio

Android studio relies on the gradle build system (which I don't know too much about), and one of the best things about this is the way it handles dependencies. If the project you're referencing is hosted on Maven Central, you can include it as simply as entering this line here -
compile 'de.timroes.android:EnhancedListView:X.Z.Y'
Unfortunately not all projects are hosted like this and occasionally you'll have to include the project the old fashioned way, as a library.

Android Studio's documentation has been a little light, but since it's only just made it to Beta, I'll give it a pass. Up to this point I've always been a little confused as to how to include a project, but having figured it out last night I thought I'd include a quick explanation here (to remind myself in a couple of weeks as well).

1 - Open Android Studio and go to File --> Project Structure


2 - Click the + button to add a new module


3 - Select Import Existing Project and next


4 - Enter the project location and click Finish


5 - Go to the root Settings.Gradle file and make sure the newly imported project is listed there. For some reason (I wish I knew more) make sure the newly added projects are listed above your application.


6 - Go to the your application build.gradle file and under the dependencies section add the following 
compile project(___insert name of project from settings.gradle here___) 


7 - Sync your gradle files by clicking the following button


OK. That should be it. I've tried it with about 5 different projects and it seems pretty reliable. Be aware, if you decide to remove the project afterwards, just go to project structure and click the minus button on the project. This doesn't delete the project, just the references, you'll have to manually delete the folder with the project files.

Good luck!