Skip to content

Add method isInitiallyExpanded in MultiLevelListAdapter #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class MultiLevelListAdapter {

/**
* Gets list of object's sub-items.
*
* <p>
* Called only for expandable objects.
*
* @param object The object.
Expand All @@ -58,13 +58,25 @@ public abstract class MultiLevelListAdapter {
/**
* Gets view configured to display the object.
*
* @param object The object.
* @param object The object.
* @param convertView The view that can be reused if possible. Null value if not available.
* @param itemInfo The InfoItem object with information about item location in MultiLevelListView.
* @param itemInfo The InfoItem object with information about item location in MultiLevelListView.
* @return The view that reflects the object.
*/
protected abstract View getViewForObject(Object object, View convertView, ItemInfo itemInfo);

/**
* Indicates if object is expanded initially.
* You can override this method to specify different objects' behaviour.
* This method will NOT be called if {@link #isExpandable(Object)} returns false or {@link MultiLevelListView#isAlwaysExpanded()} returns true.
*
* @param object The object.
* @return true if object is expanded, false otherwise.
*/
protected boolean isInitiallyExpanded(Object object) {
return false;
}

/**
* Sets initial data items to be displayed in attached MultiLevelListView.
*
Expand Down Expand Up @@ -93,7 +105,7 @@ public void notifyDataSetChanged() {
/**
* Reloads data. Method is causing nodes recreation.
*/
void reloadData() {
public void reloadData() {
setDataItems(mSourceData);
}

Expand All @@ -110,7 +122,7 @@ private void checkState() {
* Creates list of nodes for data items provided to adapter.
*
* @param dataItems List of objects for which nodes have to be created.
* @param parent Node that is a parent for nodes created for data items.
* @param parent Node that is a parent for nodes created for data items.
* @return List with nodes.
*/
private List<Node> createNodeListFromDataItems(List<?> dataItems, Node parent) {
Expand All @@ -121,7 +133,7 @@ private List<Node> createNodeListFromDataItems(List<?> dataItems, Node parent) {

Node node = new Node(dataItem, parent);
node.setExpandable(isExpandable);
if (mView.isAlwaysExpanded() && isExpandable) {
if (isExpandable && (mView.isAlwaysExpanded() || isInitiallyExpanded(dataItem))) {
node.setSubNodes(createNodeListFromDataItems(getSubObjects(node.getObject()), node));
}
result.add(node);
Expand All @@ -145,7 +157,7 @@ private List<Node> createItemsForCurrentStat() {
* Adds recurrently nodes and their sub-nodes to provided list.
*
* @param result Output parameter with flat list of items.
* @param nodes Nodes list.
* @param nodes Nodes list.
*/
private void collectItems(List<Node> result, List<Node> nodes) {
if (nodes != null) {
Expand Down Expand Up @@ -205,10 +217,10 @@ void registerView(MultiLevelListView view) {

/**
* Extends node.
*
* <p>
* Adds sub-nodes to the node.
*
* @param node The node.
* @param node The node.
* @param nestTyp NestType value.
*/
void extendNode(Node node, NestType nestTyp) {
Expand All @@ -221,7 +233,7 @@ void extendNode(Node node, NestType nestTyp) {

/**
* Collapse node.
*
* <p>
* Clears node's sub-nodes.
*
* @param node The node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public class DataActivity extends Activity {
private MultiLevelListView mListView;
private Switch mMultipliedExpandingView;
private Switch mAlwaysExpandedView;
private Switch mInitiallyExpandedView;

private boolean mAlwaysExpandend;
private ListAdapter mListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -59,19 +61,21 @@ private void confViews() {
mListView = (MultiLevelListView) findViewById(R.id.listView);
mMultipliedExpandingView = (Switch) findViewById(R.id.multipledExpanding);
mAlwaysExpandedView = (Switch) findViewById(R.id.alwaysExpanded);
mInitiallyExpandedView = (Switch) findViewById(R.id.initiallyExpanded);

mMultipliedExpandingView.setOnCheckedChangeListener(mOnCheckedChangeListener);
mAlwaysExpandedView.setOnCheckedChangeListener(mOnCheckedChangeListener);
mInitiallyExpandedView.setOnCheckedChangeListener(mOnCheckedChangeListener);

setAlwaysExpanded(mAlwaysExpandedView.isChecked());
setMultipleExpanding(mMultipliedExpandingView.isChecked());

ListAdapter listAdapter = new ListAdapter();
mListAdapter = new ListAdapter();

mListView.setAdapter(listAdapter);
mListView.setAdapter(mListAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);

listAdapter.setDataItems(DataProvider.getInitialItems());
mListAdapter.setDataItems(DataProvider.getInitialItems());
}

private void setAlwaysExpanded(boolean alwaysExpanded) {
Expand All @@ -83,6 +87,12 @@ private void setMultipleExpanding(boolean multipleExpanding) {
mListView.setNestType(multipleExpanding ? NestType.MULTIPLE : NestType.SINGLE);
}

private void setInitiallyExpanded(boolean expanded) {
DataProvider.setGroup2InitiallyExpanded(expanded);
mListAdapter.reloadData();
}


private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {

@Override
Expand All @@ -95,6 +105,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
case R.id.alwaysExpanded:
setAlwaysExpanded(isChecked);
break;
case R.id.initiallyExpanded:
setInitiallyExpanded(isChecked);
break;
}
}
};
Expand Down Expand Up @@ -140,6 +153,11 @@ public boolean isExpandable(Object object) {
return DataProvider.isExpandable((BaseItem) object);
}

@Override
protected boolean isInitiallyExpanded(Object object) {
return DataProvider.isInitiallyExpanded((BaseItem) object);
}

@Override
public View getViewForObject(Object object, View convertView, ItemInfo itemInfo) {
ViewHolder viewHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ public class DataProvider {
* Following variables refer only to data generation process.
* For instance, if ITEMS_PER_LEVEL = 2 and MAX_LEVELS = 3,
* list should look like this:
* + 1
* | + 1.1
* | - - 1.1.1
* | - - 1.1.2
* | + 1.2
* | - - 1.2.1
* | - - 1.2.2
* | - - 1.2.3
* + 2
* | + 2.1
* | - - 2.1.1
* | - - 2.1.2
* | + 2.2
* | - - 2.2.1
* | - - 2.2.2
* + 1
* | + 1.1
* | - - 1.1.1
* | - - 1.1.2
* | + 1.2
* | - - 1.2.1
* | - - 1.2.2
* | - - 1.2.3
* + 2
* | + 2.1
* | - - 2.1.1
* | - - 2.1.2
* | + 2.2
* | - - 2.2.1
* | - - 2.2.2
*/
private static final int ITEMS_PER_LEVEL = 4;
private static final int MAX_LEVELS = 6;
private static boolean isGroup2InitiallyExpanded = false;

public static List<BaseItem> getInitialItems() {
return getSubItems(new GroupItem("root"));
Expand All @@ -56,8 +57,8 @@ public static List<BaseItem> getSubItems(BaseItem baseItem) {
throw new IllegalArgumentException("GroupItem required");
}

GroupItem groupItem = (GroupItem)baseItem;
if(groupItem.getLevel() >= MAX_LEVELS){
GroupItem groupItem = (GroupItem) baseItem;
if (groupItem.getLevel() >= MAX_LEVELS) {
return null;
}

Expand All @@ -70,7 +71,7 @@ public static List<BaseItem> getSubItems(BaseItem baseItem) {
BaseItem item;
if (i % 2 == 0 && nextLevel != MAX_LEVELS) {
item = new GroupItem("Group " + Integer.toString(++groupNr));
((GroupItem)item).setLevel(nextLevel);
((GroupItem) item).setLevel(nextLevel);
} else {
item = new Item("Item " + Integer.toString(++itemNr));
}
Expand All @@ -82,4 +83,12 @@ public static List<BaseItem> getSubItems(BaseItem baseItem) {
public static boolean isExpandable(BaseItem baseItem) {
return baseItem instanceof GroupItem;
}

public static boolean isInitiallyExpanded(BaseItem baseItem) {
return isGroup2InitiallyExpanded && baseItem.getName().startsWith("Group 2");
}

public static void setGroup2InitiallyExpanded(boolean expanded) {
isGroup2InitiallyExpanded = expanded;
}
}
11 changes: 11 additions & 0 deletions sample-app/src/main/res/layout/data_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
android:textSize="@dimen/font_default"
android:layout_marginBottom="@dimen/offset_default"/>

<Switch
android:id="@+id/initiallyExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/group_2_initially_expanded"
android:padding="@dimen/padding_default"
android:switchPadding="@dimen/padding_default"
android:gravity="right|center_vertical"
android:textSize="@dimen/font_default"
android:layout_marginBottom="@dimen/offset_default"/>

<pl.openrnd.multilevellistview.MultiLevelListView
android:id="@+id/listView"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions sample-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="app_name">MultiLevelListView Sample</string>
<string name="multiplied_expanding">Multiplied expanding</string>
<string name="always_expanded">Always expanded</string>
<string name="group_2_initially_expanded">Group 2 initially expanded</string>
</resources>