Comments (9)
I am considering allowing any Group (that would also mean any individidual Item) to be either shown or hidden. If a Group is hidden, all its children would be hidden too.
What would you prefer to see?
Group.setVisible(boolean visible)
or
Group.hide()
/ Group.show()
?
from groupie.
In my opinion, the best way is to create a custom group. I've done this in the past with the ToggleGroup
code below (name suggestions welcome). I planned on submitting it as a groupie enhancement at some point but haven't had time clean up the code or write tests for it (there may be bugs). The basic idea is that you can add multiple children to this group and then easily select which child is visible by calling toggleGroup.setVisible(int visibleIndex)
import com.genius.groupie.Group;
import com.genius.groupie.NestedGroup;
import java.util.ArrayList;
import java.util.List;
public class ToggleGroup extends NestedGroup {
private int visibleIndex = 0;
private final List<Group> children = new ArrayList<>();
public ToggleGroup(final Group firstGroup) {
super.add(firstGroup);
children.add(firstGroup);
notifyItemRangeInserted(0, firstGroup.getItemCount());
}
@Override
public void add(final Group group) {
super.add(group);
children.add(group);
}
@Override
public Group getGroup(final int position) {
if (position != 0) {
return null;
}
return children.get(visibleIndex);
}
@Override
public int getPosition(final Group group) {
if (children.contains(group)) {
return 0;
}
return -1;
}
@Override
public int getGroupCount() {
return 1;
}
public int getVisibleIndex() {
return visibleIndex;
}
public void setVisible(final int visibleIndex) {
if (this.visibleIndex == visibleIndex) {
return;
}
int oldSize = getItemCount();
this.visibleIndex = visibleIndex;
int newSize = getItemCount();
if (oldSize == newSize) {
notifyItemRangeChanged(0, newSize);
} else {
notifyItemRangeRemoved(0, oldSize);
notifyItemRangeInserted(0, newSize);
}
}
private boolean dispatchChildChanges(final Group group) {
return visibleIndex == children.indexOf(group);
}
@Override
public void onChanged(Group group) {
if (dispatchChildChanges(group)) {
super.onChanged(group);
}
}
@Override
public void onItemInserted(Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemInserted(group, position);
}
}
@Override
public void onItemChanged(Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemChanged(group, position);
}
}
@Override
public void onItemChanged(Group group, int position, Object payload) {
if (dispatchChildChanges(group)) {
super.onItemChanged(group, position, payload);
}
}
@Override
public void onItemRemoved(Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemRemoved(group, position);
}
}
@Override
public void onItemRangeChanged(Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeChanged(group, positionStart, itemCount);
}
}
@Override
public void onItemRangeInserted(Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeInserted(group, positionStart, itemCount);
}
}
@Override
public void onItemRangeRemoved(Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeRemoved(group, positionStart, itemCount);
}
}
@Override
public void onItemMoved(Group group, int fromPosition, int toPosition) {
if (dispatchChildChanges(group)) {
super.onItemMoved(group, fromPosition, toPosition);
}
}
}
from groupie.
Isn't what https://github.com/Genius/groupie/blob/master/groupie/src/main/java/com/genius/groupie/ExpandableGroup.java is for ?
from groupie.
There are several ways you could approach this, depending on your requirements and how you like to think about it!
- @oldergod ExpandableGroup is a little different than what OP is asking for. It shows or hides the items in a single group but it is also intended to have a "parent" Group which is always visible. Of course, that could be a group of 0 items. So you could easily make something like
public class DisappearingGroup extends ExpandableGroup {
public DisappearingGroup() {
super(new EmptyGroup());
}
public void toggleVisibility() {
onToggleExpanded();
}
}
It reads a little strange to me, but it is certainly easy.
-
@kjpublic01 's code above is more complex but would also satisfy your requirement.
-
You could also make a
SwitchingGroup
which extendsSection
, has references to your groups A and B, and simply adds one group and removes the other when you callswitch()
. -
If it's top level, you could just remove one group and add the other at the adapter level, honestly.
If there's a lot of interest in hiding / showing individual groups, I'm down to consider it more deeply and add something specifically for this purpose to the library. It might make sense to have ExpandableGroup
be simpler and itself simply contain a ToggleGroup
or DisappearingGroup
(names welcome)!
from groupie.
+1 for hiding / showing individual groups. I've encountered several situations in which this would come in handy in our app - for example showing a dismissable card at some index in a list. Sure, you could just add / remove the card item manually, but you'd have to calculate the index which quickly becomes a pain if you have other dismissable cards.
from groupie.
btw @chip2n — in the case of dismissable cards, simply hold a reference to the card item. When it is dismissed, call GroupAdapter.remove(cardItem)
. References are the best way to interact with Groupie. If you're messing with indices, something is wrong!
from groupie.
This would be a great addition. Especially for items. I've run into several cases where I end up putting a single item in a group similar to the ToggleGroup
I mentioned above.
Think I would prefer Group.setVisible(boolean visible)
. The hide() / show()
seem like it would turn into isVisible ? group.show() : group.hide()
.
from groupie.
@lisawray Agreed - in one of my apps I have "reserved spots" in which a card should be shown in some situations. Instead of using indices, you could use an empty "placeholder" group in which you add/remove the card as needed. But calling remove on the GroupAdapter itself wouldn't work in that case, since the card would lose it's position in the list when I add it later (unless you know the index).
Anyways, I agree with @kjpublic01 - it seems that Group.setVisible(boolean visible)
would avoid a small amount of boilerplate code (especially with a reactive architecture).
from groupie.
@chip2n—I get what you're saying now; yep I've used a Group to wrap individual items as a placeholder, but toggling visibility does seem like a missing feature in that case.
from groupie.
Related Issues (20)
- I'm trying to implement. But not some error... HOT 7
- [Crash] ViewPropertyAnimator.java line 1111 - Tmp detached view should be removed from RecyclerView before it can be recycled HOT 2
- [ANR] com.xwray.groupie HOT 1
- Failed to resolve: com.github.lisawray.groupie:groupie:2.9.0 HOT 9
- [Crash] Crashes have occurred as a result of the use of groupie HOT 6
- How not disable recycling for all/some items?
- Support for Jetpack-Compose? HOT 7
- Infinite horizontal scroll HOT 1
- How to switch from list layout to grid layout using groupie ? HOT 1
- androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.FrameLayout HOT 6
- Is it possible to make ui like Instagram Discover HOT 2
- Groupie Examples Need To Be Updated
- Updating specific section / group
- GroupAdapter item count is misrepresented if empty groups are present
- Vulnerability detected in dependency used for the latest version in Maven Repository: 2.9.0 HOT 1
- getItem(position).notifyChanged(payload)
- I can't remove more than one item in my GroupAdapter.
- Crash com.xwray.groupie.GroupAdapter.onViewDetachedFromWindow GroupAdapter.java, line 261 java.lang.NullPointerException: Attempt to invoke virtual method 'void h8.j.t(h8.i)' on a null object reference
- com.xwray.groupie.GroupAdapter.onViewRecycled java.lang.NullPointerException
- RecyclerView + Groupie , Big Data Will Scroll Slow HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from groupie.