自定义expandablelistview无法正常工作 [英] Custom expandablelistview is not working

查看:70
本文介绍了自定义expandablelistview无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我停止了自定义ExpandableListView,但点击它时它不会扩展。



activity_main.xml

I ceated custom ExpandableListView but it does not expand when I click on it.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <ExpandableListView 

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/exp_lv"

        ></ExpandableListView>
    
</RelativeLayout>







group_items.xml






group_items.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:layout_width="270sp"

        android:layout_height="30sp"

        android:layout_marginLeft="30sp"

        android:id="@+id/title"

        android:textIsSelectable="true"

        />
    
    <CheckBox 

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/chk"

        />
    
</LinearLayout>





child_items.xml





child_items.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:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/child"

        android:textIsSelectable="true"

        />
    <RelativeLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        >
        <TextView 

        android:layout_width="120sp"

        android:layout_height="wrap_removed"

        android:textIsSelectable="true"

        android:id="@+id/dd"

        android:textStyle="bold"

        />
        
        <TextView 

            android:layout_width="200sp"

            android:layout_height="wrap_content"

            android:layout_toRightOf="@+id/dd"

            android:textIsSelectable="true"

            android:id="@+id/date"

            />
    </RelativeLayout>

</LinearLayout>





group.java





group.java

package com.example.expandablelistview;

import java.util.ArrayList;

public class group {
	
	private String Name;
	private boolean isSelected;
	private ArrayList<child> Items;

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public ArrayList<child> getItems() {
		return Items;
	}

	public void setItems(ArrayList<child> items) {
		Items = items;
	}
}





child.java





child.java

package com.example.expandablelistview;

public class child {

	String child_title;
	String dd;
	String date;
	public String getChild_title() {
		return child_title;
	}
	public void setChild_title(String child_title) {
		this.child_title = child_title;
	}
	public String getDd() {
		return dd;
	}
	public void setDd(String dd) {
		this.dd = dd;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
}







MyBaseAdapter.java






MyBaseAdapter.java

package com.example.expandablelistview;

import java.util.ArrayList;

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

public class MyBaseAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<group> group_al;

public MyBaseAdapter(Context context,ArrayList<group> group_al) {
	this.context=context;
	this.group_al=group_al;
}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<child> chList = group_al.get(groupPosition).getItems();
		
        return chList.get(childPosition);
	}

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

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		 child ch = (child) getChild(groupPosition, childPosition);
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context
	                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.child_items, null);
	        }
	        TextView child = (TextView) convertView.findViewById(R.id.child);
	        TextView dd = (TextView) convertView.findViewById(R.id.dd);
	        TextView date= (TextView) convertView.findViewById(R.id.date);

	        child.setText(ch.getChild_title().toString());
	        dd.setText(ch.getChild_title().toString());
	        date.setText(ch.getChild_title().toString());

	        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		ArrayList<child> chList = group_al.get(groupPosition).getItems();
		
        return chList.size();
	}

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

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

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

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		group gr = (group) getGroup(groupPosition);
		long group_id = getGroupId(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_items, null);
        }
        
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(gr.getName());
        CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
        chk.setFocusable(false);
        
        return convertView;
	}

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

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		
		return false;
	}

}







MainActivity.java






MainActivity.java

package com.example.expandablelistview;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
	private MyBaseAdapter ExpAdapter;
    private ArrayList<group> ExpListItems;
    private ExpandableListView ExpandList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
        ExpListItems = SetStandardGroups();
        ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems);
        ExpandList.setAdapter(ExpAdapter);
	}

	private ArrayList<group> SetStandardGroups() {
		
		ArrayList<group> list = new ArrayList<group>();
		group gru= new group();
        gru.setName("Tax 1");
        
        ArrayList<child> ch_list= new ArrayList<child>();
        child ch = new child();
        ch.setChild_title("Information of tax 1");
        ch.setDd("Due Date:");
        ch.setDate("22/02/2016");
        
        ch_list.add(ch);
        
    gru.setItems(ch_list);
    list.add(gru);
    
		return list;
	}
}













Please guys help me.

Thanks in advance.




What I have tried:



When I run the application I can see the first row of ExpandableListView i.e. Tax1 with CheckBox beside it but when I click on the row then it does not expanded.







Please guys help me.
Thanks in advance.


What I have tried:

When I run the application I can see the first row of ExpandableListView i.e. Tax1 with CheckBox beside it but when I click on the row then it does not expanded.

推荐答案

Hello I think your code in the Oncreate Method is not complete. You have not set the click listener.

Hello I think your code in the Oncreate Method is not complete. You have not set the click listener.
@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
        ExpListItems = SetStandardGroups();
        ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems);
        ExpandList.setAdapter(ExpAdapter);
        ExpandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                return false;
            }
        });
        ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
               
                return false;
            }
        });

	}



Try something like that. And lets know whats happening.


Try something like that. And lets know whats happening.


这篇关于自定义expandablelistview无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆