简体   繁体   中英

how to add function on child items in expandablelistview?

Good Day! i am new in android programming and java, so i did a copy paste of a source code in the internet then it worked but i have to do some edit for this.

i wanna ask how to add a function when i click on switch of every group that supposed to be on/off not to toast. i understand how to use onClickListener Somehow but i dont know where to put the function.

any answers or good tutorial link for this will be greatly appreciated THANK YOU!

MainActivity.java

package com.capstone.r.e.d.e_kit;
import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;

public class MainActivity extends ExpandableListActivity{

private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // this is not really  necessary as ExpandableListActivity contains an ExpandableList
    //setContentView(R.layout.main);


    ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)

    expandableList.setDividerHeight(2);
    expandableList.setGroupIndicator(null);
    expandableList.setClickable(true);

    setGroupParents();
    setChildData();

    MainActivityAdapter adapter = new MainActivityAdapter(parentItems, childItems);

    adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    expandableList.setAdapter(adapter);
    expandableList.setOnChildClickListener(this);
}

public void setGroupParents() {
    parentItems.add("Flash Light");
    parentItems.add("Whistle");
    parentItems.add("Infographics");
    parentItems.add("Battery Saving Mode");
}

public void setChildData() {

    // Flash Light
    ArrayList<String> child = new ArrayList<String>();
    child.add("Switch");
    childItems.add(child);


    // Whistle
    child = new ArrayList<String>();
    child.add("Switch");    
    childItems.add(child);

    // Information
    child = new ArrayList<String>();
    child.add("Online News");
    child.add("Do's and Dont's");
    childItems.add(child);


    // Battery Saving Mode
    child = new ArrayList<String>();
    child.add("Switch");
    childItems.add(child);
}

}

MainActivityAdapter.java

package com.capstone.r.e.d.e_kit;


import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivityAdapter extends BaseExpandableListAdapter {

private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;


public MainActivityAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
    this.parentItems = parents;
    this.childtems = childern;
}

public void setInflater(LayoutInflater inflater, Activity activity) {
    this.inflater = inflater;
    this.activity = activity;
}


@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    child = (ArrayList<String>) childtems.get(groupPosition);

    TextView textView = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.group, null);
    }

    textView = (TextView) convertView.findViewById(R.id.textView1);
    textView.setText(child.get(childPosition));

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(activity, child.get(childPosition),
                    Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.row, null);
    }

    ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);

    return convertView;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) childtems.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

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

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

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



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

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

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

        <ExpandableListView
            android:id="@+id/list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:groupIndicator="@null" />   

</LinearLayout>

row.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:background="#339966"
/>

group.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textIsSelectable="true"
        android:textColor="#000000"
        android:textSize="14sp"
         />
</LinearLayout>


</LinearLayout>

Try to set setOnGroupClickListener :

adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    expandableList.setAdapter(adapter);
    expandableList.setOnChildClickListener(this);
expandableList.setOnGroupClickListener(this); //Implement inteface ExpandableListView.OnGroupClickListener 

Documatation: http://developer.android.com/reference/android/widget/ExpandableListView.html#setOnGroupClickListener(android.widget.ExpandableListView.OnGroupClickListener)

Tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html#expandablelistview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM