Child dengan expandablelistview tidak sesuai

saya sudah beberapa kali membuat menu dengan ExpandableListView, namun tidak sesuai dengan harapan :

Kondisi 1 : jumlah Group = 2 jumlah masing-masing Child = 3 Hasil = Child tidak tampil semua, tapi cuma 2 child yang tampil dari masing-masing group

Kondisi 2 : jumlah Group = 3 jumlah Child pada Group 1 = 2 jumlah Child pada Group 2 = 3 jumlah Child pada Group 3 = 3 Hasil : ketika di klik Group 1, langsung keluar sendiri aplikasinya

Harapan Saya bisa membuat menu dengan struktur seperti berikut (misal) : Binatang -- kucing -- anjing -- burung Pemrograman -- java -- android -- php -- c# -- basic -- css -- html Elektronik -- TV -- Speaker

mohon bantuannya atau link mengenai atau koreksinya hal tersebut.

source code saya lampirkan

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

6 Jawaban:

MainActivity.java

<pre> package com.project.ardi.ebpkd;

import android.app.ProgressDialog; import android.content.res.Configuration; import android.support.annotation.Nullable; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;

import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.RadioButton;

import com.project.ardi.ebpkd.Adapter.CustomExpandableListAdapter; import com.project.ardi.ebpkd.Fragments.Fragments_PBB.ViewPenetapanPBBFragment; import com.project.ardi.ebpkd.Fragments.HomeFragment; import com.project.ardi.ebpkd.GlobalVar.GlobalKonstanta; import com.project.ardi.ebpkd.Helper.FragmentNavigationManager; import com.project.ardi.ebpkd.Interface.NavigationManager;

import java.util.ArrayList;

import java.util.HashMap; import java.util.List;

public class MainActivity extends AppCompatActivity {

private DrawerLayout mdDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
private ExpandableListView expandableListView;
private ExpandableListAdapter adapter;
private List&amp;lt;String&amp;gt; lstTitle; //group
private HashMap&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; lstChild;
private NavigationManager navigationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mdDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();
    expandableListView = (ExpandableListView)findViewById(R.id.navList);
    navigationManager = FragmentNavigationManager.getmInstance(this);

    View listHeaderView = getLayoutInflater().inflate(R.layout.nav_header, null, false);
    expandableListView.addHeaderView(listHeaderView);

    initItems();
    addDrawersItem();

    setupDrawer();

    if(savedInstanceState == null){
        selectFirstItemAsDefault();
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setTitle(GlobalKonstanta.title_app);
}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void FirstLoadFragment(){
    HomeFragment fragment = new HomeFragment();
    loadFragmentBaru(fragment);
}

private void selectFirstItemAsDefault() {
    if(navigationManager != null)
    {
        String firstItem = lstTitle.get(0);
        navigationManager.showFragment(firstItem);
        getSupportActionBar().setTitle(firstItem);
    }
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mdDrawerLayout, R.string.open, R.string.close){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mdDrawerLayout.setDrawerListener(mDrawerToggle);
}

private void addDrawersItem() {
    adapter = new CustomExpandableListAdapter(this, lstTitle, lstChild);
    expandableListView.setAdapter(adapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            getSupportActionBar().setTitle(lstTitle.get(groupPosition).toString());
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            getSupportActionBar().setTitle(GlobalKonstanta.title_app);
        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            String selectedItem = ((List)(lstChild.get(lstTitle.get(groupPosition)))).get(childPosition).toString();
            getSupportActionBar().setTitle(selectedItem);

            if(groupPosition&amp;lt;lstTitle.size()){
                if(groupPosition==0){
                    if(childPosition==0){
                        navigationManager.showFragment(selectedItem);
                    } else{
                        navigationManager.showFragment(selectedItem);
                    }
                }else if (groupPosition==1) {
                    if (childPosition == 0) {
                        ViewPenetapanPBBFragment fragment = new ViewPenetapanPBBFragment();
                        loadFragmentBaru(fragment);
                    } else {
                        navigationManager.showFragment(selectedItem);
                    }
                }
                else{
                    navigationManager.showFragment(selectedItem);
                }
            }else{
                throw new IllegalArgumentException(&quot;Tidak didukung fragment&quot;);
            }
            mdDrawerLayout.closeDrawer(GravityCompat.START);
            return false;
        }
    });
}

private void loadFragmentBaru(android.support.v4.app.Fragment fg){
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container ,fg);
    fragmentTransaction.commit();
}

private void initItems() {
    lstTitle = new ArrayList&amp;lt;&amp;gt;();
    lstChild = new HashMap&amp;lt;String,List&amp;lt;String&amp;gt;&amp;gt;();

    lstTitle.add(&quot;Group1&quot;);
    lstTitle.add(&quot;PBB-P2&quot;);

    List&amp;lt;String&amp;gt; list1 = new ArrayList&amp;lt;&amp;gt;();
    list1.add(&quot;Child_1_FromGroup1&quot;);
    list1.add(&quot;Child_2_FromGroup1&quot;);

    List&amp;lt;String&amp;gt; list2 = new ArrayList&amp;lt;&amp;gt;();
    list2.add(&quot;Cari&quot;);
    list2.add(&quot;Test&quot;);

    lstChild.put(lstTitle.get(0), list1);
    lstChild.put(lstTitle.get(1), list2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(mDrawerToggle.onOptionsItemSelected(item))
        return true;

    return  super.onOptionsItemSelected(item);
}

}

</pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

activity_main.xml

<pre> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"&gt;

&amp;lt;LinearLayout
    android:orientation=&quot;vertical&quot;
    android:id=&quot;@+id/activity_controller&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&amp;gt;

    &amp;lt;FrameLayout
        android:id=&quot;@+id/container&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&amp;gt;&amp;lt;/FrameLayout&amp;gt;
&amp;lt;/LinearLayout&amp;gt;

&amp;lt;ExpandableListView
    android:id=&quot;@+id/navList&quot;
    android:layout_width=&quot;235dp&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_gravity=&quot;left|start&quot;
    android:background=&quot;@android:color/background_light&quot;
    android:divider=&quot;@color/dividerColor&quot;
    android:dividerHeight=&quot;0.5dp&quot;
    android:indicatorLeft=&quot;?android:attr/expandableListPreferredItemIndicatorLeft&quot;&amp;gt;&amp;lt;/ExpandableListView&amp;gt;

&lt;/android.support.v4.widget.DrawerLayout&gt; </pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

nav_header.xml

<pre> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/main_header_selector" &gt;

&amp;lt;LinearLayout
    android:orientation=&quot;horizontal&quot;
    android:weightSum=&quot;1&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_margin=&quot;10dp&quot;
    &amp;gt;

    &amp;lt;ImageView
        android:id=&quot;@+id/avatar&quot;
        android:src=&quot;@drawable/ic_account_circle_white_24dp&quot;
        android:layout_width=&quot;70dp&quot;
        android:layout_height=&quot;70dp&quot; /&amp;gt;
    &amp;lt;TextView
        android:id=&quot;@+id/balance&quot;
        android:gravity=&quot;right|end&quot;
        android:layout_weight=&quot;1&quot;
        android:paddingTop=&quot;10dp&quot;
        android:text=&quot;Balance : 1000&quot;
        android:textColor=&quot;@android:color/white&quot;
        android:textSize=&quot;16dp&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot; /&amp;gt;

&amp;lt;/LinearLayout&amp;gt;

&amp;lt;TextView
    android:id=&quot;@+id/name&quot;
    android:layout_weight=&quot;1&quot;
    android:paddingLeft=&quot;10dp&quot;
    android:layout_marginBottom=&quot;10dp&quot;
    android:gravity=&quot;left|start&quot;
    android:text=&quot;ARDINAL&quot;
    android:textColor=&quot;@android:color/white&quot;
    android:textSize=&quot;16dp&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot; /&amp;gt;

&lt;/LinearLayout&gt; </pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

CustomExpandableListAdapter.java

<pre> package com.project.ardi.ebpkd.Adapter;

import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView;

import com.project.ardi.ebpkd.R;

import java.util.HashMap; import java.util.List;

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private  List&amp;lt;String&amp;gt; listTitle;
private HashMap&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; listItem;

public CustomExpandableListAdapter(Context context, List&amp;lt;String&amp;gt; listTitle, HashMap&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; listItem) {
    this.context = context;
    this.listTitle = listTitle;
    this.listItem = listItem;
    System.out.println(&quot;CustomExpandableListAdapter  listTitle size : &quot;+this.listTitle.size());
    System.out.println(&quot;CustomExpandableListAdapter  listItem size : &quot;+this.listItem.size());
}


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

@Override
public int getChildrenCount(int groupPosition) {
    return listItem.size();
}

@Override
public Object getGroup(int groupPosition) {
    return listTitle.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
     return listItem.get(listTitle.get(groupPosition)).get(childPosition);
}

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

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String title = (String)getGroup(groupPosition);
    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.list_group, null);
    }

    TextView txtTitle = (TextView)convertView.findViewById(R.id.listTitle);
    txtTitle.setTypeface(null, Typeface.BOLD);
    txtTitle.setText(title);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    String title = (String)getChild(groupPosition,childPosition);
    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
    }

    TextView txtChild = (TextView)convertView.findViewById(R.id.expandabledListItem);
    txtChild.setText(title);

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

</pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

list_group.xml

<pre> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt;

&amp;lt;TextView
    android:id=&quot;@+id/listTitle&quot;
    android:paddingBottom=&quot;10dp&quot;
    android:paddingTop=&quot;10dp&quot;
    android:paddingLeft=&quot;?android:attr/expandableListPreferredItemPaddingLeft&quot;
    android:layout_width=&quot;0dp&quot;
    android:layout_weight=&quot;1&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:textColor=&quot;@color/list_group_title&quot;
    /&amp;gt;

&lt;/LinearLayout&gt; </pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

list_item.xml

<pre> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt;

&amp;lt;TextView
    android:id=&quot;@+id/expandabledListItem&quot;
    android:paddingBottom=&quot;10dp&quot;
    android:paddingTop=&quot;10dp&quot;
    android:paddingLeft=&quot;?android:attr/expandableListPreferredItemPaddingLeft&quot;
    android:paddingRight=&quot;?android:attr/expandableListPreferredItemPaddingLeft&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:textColor=&quot;@color/list_item_title&quot;
    /&amp;gt;

&lt;/LinearLayout&gt; </pre>

avatar ardinal
@ardinal

7 Kontribusi 0 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban