How do you add dividers between only certain elements in a RecyclerView? - java

I'm aware of the DividerItemDecoration with regards to RecyclerViews although instead of a divider between every item I'm more interested in a divider whenever a certain attribute changes within my elements. For example, a list of transactions that hold the date by which they were posted - a divider would then display at each change in date in the list of transactions.
This is definitely possible but I'm just not sure to go about doing it. For example, in the Monzo application - it's the dates that separate the transaction lines.
Any help would be very much appreciated!

You would create a recycler view adapter with two viewholders. In the example of the Monzo app, you would create one ViewHolder for the purchase and the other for the date. Then in onCreateViewHolder you would inflate the correct type of viewholder for that item in that list.

One way you could do it is to add the divider to the ViewHolder layout, defaulting to View.INVISIBLE (or View.GONE if you want it to take up extra space when it's visible - like the divider is outside the list element instead of inside at the top edge)
Then in onBindViewHolder you could do a bit of logic to decide if the divider should be set to visible or not for that item - something that decides if it represents a date change, and make sure to check it's not the first item in the list too (don't want a random divider up there)

Related

Divider in Android recycler view

I am doing a small Android game where the user needs to assign two players to two teams.
My plan is to use a recycler view which lists all existing players (which are stored in a local database).
The items in the recycler view will be possible to reorder. The first two items in the recycler view will be assigned to team A and the next two items will be assigned to team B. The remaining players will not be considered in a team. The implementation of this logic will be fine, there are also enough examples in the web.
But in order to indicate this logic to the user, I would like to offer him some kind of boxes which indidcate the teams. Like in the following wireframe:
Does anybody have an advice how this should be done? Should I put a View above the recycler view?
Alternatively, I am also open for alternative approaches to assign exactly two players (uniquely) to two teams.
If you want use RecyclerView, you need to include divider in item view, or make divider as item itself.
You need to place these Divider backgrounds below recyclerview, calculate heights of these items and place it accordingly.

How can I separate the elements of one recyclerView from another when the number of elements causes scroll to be generated on the recycler?

Every time I am adding elements within a recyclerView, at the moment of generating scroll I need that no more elements are added inside that viewHolder and later it is added to another one. Thank you very much

Android - TextView in a Custom ListView loses its content while scrolling

I have came across these type of questions a lot, But none of the answers solved my problem.
I am developing a mobile shopping application model. I have a custom ListView with an ImageView, and EditText(quantity), 2 Buttons for increasing and decreasing the quantity, 3 TextViews one for item Name and other two for prices and I am dynamically loading a number of Buttons for NetWeights based on the requirement through Java code and I am implementing it using CustomAdapter(Class) extending BaseAdapter and getView().
Problem 1:
When I use the condition if(convertView==null), setTag(holder) and getTag(), the dynamically loaded Buttons are getting duplicated and gets doubled in number.
Problem 2:
While scrolling the custom ListView I am loosing my data on 2 of the TextViews while the other TextView(name) is fine because in that TextView I don't Change data on runtime. On the other two TextViews(prices) I change the data on runtime using ButtonClicks(increase and decrease) where at that time when I scroll the two TextViews, it looses its content. Actually I had this same problem with the EditText(displaying quantity), I solved it using the TextWatcher by getting the value and passing it in an array and again setting it. I used the same technique (i.e) the TextWatcher for the TextViews But that did'nt Help. I also tried removing the TextWatcher and setting the text with the array and adding TextWatcher. Still I am loosing my content in those TextViews
Any Suggestions??????
Problem 1:
While using ViewHolder (setTag(), getTag()) we will be reusing the views.
For example: Let's say we have 10 listview items and the screen can display 3 items at a time. Lets say we add 2 buttons to list item 1. First time when we load the listview we will see two buttons. Lets say now we scroll down and come back to the list item 1 again, as we are reusing the views, we already have 2 buttons and if we have the code to add buttons using getTag() then 2 more buttons will be added. I think this may be causing the issue of button duplication. To add little more information, list item 1, list item 4, list item 7 and list item 10 will all use same view, as our screen in this example can display just 3 items.
Problem 2:
One idea is to store all these text values in the arrayList.
For example, lets assume we have one textView inside each list item that can be incremented/ decremented. Set textWatcher on this textView and whenever text changes add it to the arrrayList using 'position' as the index. Inside getView method when you do getTag() try to set the textView using previously stored text (that was stored inside the arrayList)if exists.

Android: Grouping of ListView [duplicate]

I've a ListView with items containing information about places with a rating and the distance to the current location.
The items are sorted into groups:
Group 1: within 500m
Group 2: 500m - 1km
Group 3: 1km - 1.5km
...
Withing these groups the items are sorted by their rating.
Now I put out these items via my custom adapter (extension of BaseAdapter) into the ListView, which works perfectly.
However, what I'd like to do is to put a separator before the each first item of each group. This separator can be a TextView saying e.g. 500m - 1km followed by all the ListView items in that group.
Any idea on how to realize this?
Here is one implementation that does exactly what you describe.
That one is GPLv3, because it is derived from this implementation, which was GPLv3.
You can also use my MergeAdapter for this, which has the advantage of being Apache License 2.0. Just hand it an alternating set of header TextViews and Adapters containing each section's worth of content.
Or, you can peek at all of these and roll their behaviors into your existing Adapter class. The trick is to return your TextView headers at the right spot, and properly implement methods like getViewTypeCount(), getItemViewType(), areAllItemsEnabled(), and isEnabled().
Here is even nicer implementation, inspired from iOS section list view : http://code.google.com/p/android-section-list/. The nie thing about it is that the section header remains visible even if you scroll the section down.

android listviews: header and footer views

In my ListActivity, I need header and footer views (on the top and bottom of the list) to be used as previous page and next page buttons on my list, respectively, because I want to display only 20 items at a time.
I set my header and foot views by doing:
getListView().addHeaderView(myHeaderView);
getListView().addFooterView(myFooterView);
setListAdapter(adapter);
This works fine, but I need to dynamically remove and add these header and footer views, because some pages of my list may not have a next page button or a previous page button.
The problem is, I cannot call addHeaderView or addFooterView after I have called setListAdapter.
Is there a way around this?
Why not just collapse the header and footer to zero height, or gray out the buttons (even better).
And the best user experience, in my opinion, would be to dynamically load more items when needed (i.e. upon scroll), like the built-in Gmail app does.
Yes, this is a bug or oversight in the ListView component. You can work around this by writing your own WrapperListAdapter that handles adding and removing fixed list items, but I can tell you it's not entirely straightforward to do.
Alternatively — and much easier — you could add a fixed component above or below the ListView where you place the next and previous buttons.
How about reset the adapter at every time you need to add header view, like this way:
ListView.FixedViewInfo headerInfo = getListView().new FixedViewInfo();
headerInfo.isSelectable=false ;
headerInfo.view = feedInfoView;
headerInfos.add(headerInfo);
headerViewListAdapter = new HeaderViewListAdapter(headerInfos,null,adapter);
getListView().setAdapter(headerViewListAdapter);

Categories

Resources