Implementing Expandable ListView in fragment in Android Studio

Home / Uncategorized / Implementing Expandable ListView in fragment in Android Studio

Question:
I am trying to put expandable listview inside fragment, so each item of the list can drop down and show more text below it. I tried multiple way but didn’t succeed. No error or warnings, but when I click on the fragment the Mobile App just quit.

The fragment is called projects. I want it contains a list of titles of the projects, then each of them can drop down to show the details. Code are here:
fragment_project.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class fragment_project extends Fragment {

ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;

List titles;
Map texts;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
return init(inflater.inflate(R.layout.content_projects, container, false));
}

private View init(View inflate) {
expandableListView = (ExpandableListView) inflate.findViewById(R.id.expandable_list_view);
fill_data();

expandableListAdapter = new fragment_project_Exp_List_Adapter(inflate, titles, texts);
expandableListView.setAdapter(expandableListAdapter);

return expandableListView;
}

public void fill_data() {

titles = new ArrayList<>();
texts = new HashMap<>();

String title1 = getString(R.string.title_android_project);
String title2 = getString(R.string.title_HTML5_project);

titles.add(title1);
titles.add(title2);

String text1 = getString(R.string.content_android_project);
String text2 = getString(R.string.content_HTML5_project);

texts.put(title1, text1);
texts.put(title2, text2);
}

}

fragment_project_Exp_List_Adapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class fragment_project_Exp_List_Adapter extends BaseExpandableListAdapter {

View inflate;
List titles;
Map texts;

public fragment_project_Exp_List_Adapter(View inflate, List titles, Map texts) {
this.inflate = inflate;
this.titles = titles;
this.texts = texts;
}

@Override
public int getGroupCount() {
return titles.size();
}

@Override
public int getChildrenCount(int i) {
return 1; // HashMap is string to string, not string to multiple strings, so children count for each group is always 1.
}

@Override
public Object getGroup(int i) {
return titles.get(i);
}

@Override
public Object getChild(int i, int i1) {
return texts.get(i1);
}

@Override
public long getGroupId(int i) {
return i;
}

@Override
public long getChildId(int i, int i1) {
return i1;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

String title = (String) getGroup(i);

if (view == null){

view = inflate.findViewById(R.id.project_titles);
}

return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

String text = (String) getChild(i, i1);

if (view == null){

view = inflate.findViewById(R.id.project_texts);
}

return view;
}

@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}

I feel like I need to do something in public View onCreateView inside "fragment_project.java" file but not sure how. Any help are always appreciated. Thank you!


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *