开发者社区> 问答> 正文

【Android】树形菜单、扩展下拉菜单BaseExpandableListAdapte..:报错

先看效果~

 

 

也就是BaseExpandableListAdapter、AbsListView类的使用,就不多说了..大牛留情...

 

就两个类。

ExpandLabel:

package com.yfz;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ExpandableListView;
import android.widget.ViewFlipper;
public class ExpandLabel extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 数据源
        //标题
        List<String> groupArray = new ArrayList<String>();
        //子标题
        List<List<String>> childArray = new ArrayList<List<String>>();
		groupArray.add("第一章");
		groupArray.add("第二章");
		List<String> tempArray01 = new ArrayList<String>();
		tempArray01.add("第1.1节");
		tempArray01.add("第1.2节");
		tempArray01.add("第1.3节");
		
		List<String> tempArray02 = new ArrayList<String>();
		tempArray02.add("第2.1节");
		tempArray02.add("第2.2节");
		tempArray02.add("第2.3节");
		
		childArray.add(tempArray01);
		childArray.add(tempArray02);
		
        ExpandableListView expandableListView = new ExpandableListView(this);
        
        ExpandableAdapter adapter = new ExpandableAdapter(this,groupArray,childArray);
        
		expandableListView.setAdapter(adapter);
		
		addContentView(expandableListView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }
}

 

 

树形菜单类,只要传两个List过来就可以直接使用了。

groupArray:表示一级菜单

childArray: 表示二级菜单

二级菜单相当于一个二维List,该List中存储的每个元素都是一个List。第一个元素对应一级菜单中第一个菜单的子项List,第二个元素对应一级菜单中第二个菜单的子项List,依次类推。

 

 

 

ExpandableAdapter:

package com.yfz;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableAdapter extends BaseExpandableListAdapter {
		
	private List<String> groupArray;
	private List<List<String>> childArray;
	private Activity activity;
	public ExpandableAdapter(Activity a,List<String> groupArray,List<List<String>> childArray) {
		activity = a;
		this.groupArray = groupArray;
		this.childArray = childArray;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childArray.get(groupPosition).get(childPosition);
	}
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String string = childArray.get(groupPosition).get(childPosition);
		return getGenericView(string);
	}
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childArray.get(groupPosition).size();
	}
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groupArray.get(groupPosition);
	}
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupArray.size();
	}
	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String string = groupArray.get(groupPosition);
		return getGenericView(string);
	}
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}
	
/****************************************以下为自定义方法*********************************************/	
	/**
	 * Children 's View
	 * @param string
	 * @return
	 */
	public TextView getGenericView(String string) {
		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
		TextView text = new TextView(activity);
		text.setLayoutParams(layoutParams);
		// Center the text vertically
		text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// Set the text starting position
		text.setPadding(36, 0, 0, 0);
		text.setText(string);
		return text;
	}
}

 

 


ExpandableListView的OnChildClickListener无效? ExpandableListView子元素无法点击?

 

不知道大家有没有发现,上面这样做之后, Child项时无法被点击的。 因此也会造成OnChildClickListener注册后无效。 即不管你怎么点击Child项,onChildClick始终不会被调用, 原因是什么呢?

 

很简单,是自定义adapter中的 isChildSelectable 方法造成。 这个方法标识子元素能否被点击。 返回false就无法被点击,要返回true才行。 


原文链接: http://blog.csdn.net/feng88724/article/details/6170121

展开
收起
kun坤 2020-06-20 11:23:58 1096 0
1 条回答
写回答
取消 提交回答
  • mark 不错。。

    2020-06-20 11:24:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载