3

我正在尝试创建一个可扩展的列表视图。组的初始视图显示良好,唯一的问题是......当我单击它们时没有任何反应。没有扩展什么的。我已经遵循了我见过的大多数教程,但没有任何东西可以解决它!感谢您的帮助,这是我正在使用的代码:

特点适配器:

package com.me.app;

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

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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class FeaturesAdapter extends BaseExpandableListAdapter
{

private Context mContext;
private HashMap<String, List<String>> mFeatures = new HashMap<String, List<String>>();
private HashMap<Integer, String> mList = new HashMap<Integer, String>();

public FeaturesAdapter (Context con)
{
    mContext = con;
}

public boolean AddGroup(final String groupName, final List<String> features)
{
    final List<Feature> prev = mFeatures.put(groupName, features);

    if (prev != null)
        return false;

    mList.put(mFeatures.size() - 1, groupName);

    notifyDataSetChanged();
    return true;
}

@Override
public Object getChild(int groupPos, int childPos) 
{
    if (mList.containsKey(groupPos))
    {
        final String str = mList.get(groupPos);
        final List<Feature> data = mFeatures.get(str);

        return data.get(childPos);
    }

    return null;
}

@Override
public long getChildId(int groupPos, int childPos) 
{  
    return 0;
}

@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) 
{
    View rowView = convertView;
    FeatureView fView = null;
    int gPos = groupPos;
    int cPos = childPos;

    if (rowView == null)
    {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        rowView = inflater.inflate(R.layout.feature_row, null);

        fView = new FeatureView();
        fView.feature_name = (TextView) rowView.findViewById(R.id.feature_name);
        fView.feature_checked = (CheckBox) rowView.findViewById(R.id.feature_checked);

        rowView.setTag(fView);
    }
    else
        fView = (FeatureView) rowView.getTag();

    String str = mList.get(gPos);
    List<Feature> data = mFeatures.get(str);
    Feature feature = data.get(cPos);

    fView.feature_name.setText(feature.getName());
    fView.feature_checked.setChecked(feature.getChecked());
    int [] values = {gPos,cPos};
    fView.feature_checked.setTag(values);

    fView.feature_checked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
            doThings();             
        }
    });

    return rowView;
}

protected static class FeatureView {
    protected TextView feature_name;
    protected CheckBox feature_checked;
}

@Override
public int getChildrenCount(int groupPos) 
{
    if (mList.containsKey(groupPos))
        return mFeatures.get(mList.get(groupPos)).size();

    return 0;
}

@Override
public Object getGroup(int groupPos) 
{
    if (mList.containsKey(groupPos))
        return mFeatures.get(mList.get(groupPos));

    return null;
}

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

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

@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
{
    View rowView = convertView;
    FeatureView fView = null;
    int gPos = groupPos;

    if (rowView == null)
    {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        rowView = inflater.inflate(R.layout.group_row, null);

        fView = new FeatureView();
        fView.feature_name = (TextView) rowView.findViewById(R.id.group_name);
        fView.feature_checked = (CheckBox) rowView.findViewById(R.id.group_checked);

        rowView.setTag(fView);
    }
    else
        fView = (FeatureView) rowView.getTag();

    String str = mList.get(groupPos);
    List<Feature> data = mFeatures.get(str);

    fView.feature_name.setText(mList.get(groupPos));
    boolean flag = false;
    for (Feature feat : data)
    {
        if (feat.getChecked())
            flag = true;
    }
    fView.feature_checked.setChecked(flag);
    int[] values = {gPos};
    fView.feature_checked.setTag(values);

    fView.feature_checked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
            doOtherThings();
    });

    return rowView;
}

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

@Override
public boolean isChildSelectable(int groupPos, int childPos) 
{
    return false;
}
}

这是我在实际文件中的代码:

    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.feature_list);

    FeaturesAdapter adapter = new FeaturesAdapter(this);

    mBye.add("Cya");
    mBye.add("Bye");
    mBye.add("Adios");
    mBye.add("Goodbye");
    adapter.AddGroup("Bye", mBye);

    mStuff.add("Hi"));
    mStuff.add("Hey"));
    mStuff.add("Hello"));
    adapter.AddGroup("Salutations", mStuff);

    mSlang.add(new Feature("Yo"));
    adapter.AddGroup("Slang", mSlang);



    setListAdapter(adapter);
    ExpandableListView elv = getExpandableListView();
    elv.setTextFilterEnabled(true);
4

1 回答 1

6

我们有一个赢家。如果 ExpandableListView 中有可聚焦元素,则需要在其 xml 设置中设置 android:focusable="false"。哇哦。

于 2012-06-29T17:38:01.557 回答