Daftar dan Belajar Sekarang!

Child dengan expandablelistview tidak sesuai

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
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

Jawaban
foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
MainActivity.java
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<String> lstTitle; //group
    private HashMap<String, List<String>> 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<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("Tidak didukung fragment");
                }
                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<>();
        lstChild = new HashMap<String,List<String>>();

        lstTitle.add("Group1");
        lstTitle.add("PBB-P2");

        List<String> list1 = new ArrayList<>();
        list1.add("Child_1_FromGroup1");
        list1.add("Child_2_FromGroup1");

        List<String> list2 = new ArrayList<>();
        list2.add("Cari");
        list2.add("Test");

        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);
    }
}

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/activity_controller"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/navList"
        android:layout_width="235dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@android:color/background_light"
        android:divider="@color/dividerColor"
        android:dividerHeight="0.5dp"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"></ExpandableListView>

</android.support.v4.widget.DrawerLayout>

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        >

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

    </LinearLayout>

    <TextView
        android:id="@+id/name"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        android:layout_marginBottom="10dp"
        android:gravity="left|start"
        android:text="ARDINAL"
        android:textColor="@android:color/white"
        android:textSize="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
CustomExpandableListAdapter.java
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<String> listTitle;
    private HashMap<String, List<String>> listItem;

    public CustomExpandableListAdapter(Context context, List<String> listTitle, HashMap<String, List<String>> listItem) {
        this.context = context;
        this.listTitle = listTitle;
        this.listItem = listItem;
        System.out.println("CustomExpandableListAdapter  listTitle size : "+this.listTitle.size());
        System.out.println("CustomExpandableListAdapter  listItem size : "+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;
    }
}

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/listTitle"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textColor="@color/list_group_title"
        />

</LinearLayout>

foto ardinal
7 Kontribusi 0 Coto
3 bulan yang lalu
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/expandabledListItem"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/expandableListPreferredItemPaddingLeft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/list_item_title"
        />

</LinearLayout>


Login untuk diksusi di forum sekolah koding