使用片段时列表不展开

时间:2014-04-01 06:40:27

标签: android android-fragments

我想在Fragment中使用Expandable Listview。

我获得了组名,但是当点击它时无法打开子元素。

public class SavedTabsFragment extends Fragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.saved_tab, null);
    ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.list);
    elv.setClickable(true);
    SavedTabsListAdapter  adapter= new SavedTabsListAdapter(getActivity());
    /*adapter.setInflater( inflater,
            getActivity());*/
    elv.setAdapter(adapter);
    return v;
}
 }

SavedTabsListAdapter

public class SavedTabsListAdapter extends BaseExpandableListAdapter {

private String[] groups = { "People Names", "Dog Names", "Cat Names",
        "Fish Names" };
private Context ctx;
private LayoutInflater inflater;
private String[][] children = { { "Arnold", "Barry", "Chuck", "David" },
        { "Ace", "Bandit", "Cha-Cha", "Deuce" }, { "Fluffy", "Snuggles" },
        { "Goldy", "Bubbles" } };

public SavedTabsListAdapter(Context ct) {
    ctx = ct;
    inflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getGroupCount() {
    return groups.length;
}

@Override
public int getChildrenCount(int i) {
    return children[i].length;
}

@Override
public Object getGroup(int i) {
    return groups[i];
}

@Override
public Object getChild(int i, int i1) {
    return children[i][i1];
}

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

@Override
public long getChildId(int i, int i1) {
    return i1;
}

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    view = inflater.inflate(R.layout.grouprow, null);
    CheckedTextView tv = (CheckedTextView) view
            .findViewById(R.id.textView1);
    tv.setText(getGroup(i).toString());
    return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view,
        ViewGroup viewGroup) {

    view = inflater.inflate(R.layout.childrow, null);
    TextView tv = (TextView) view.findViewById(R.id.textView1);
    tv.setText(getChild(i, i1).toString());
    return view;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

尝试使用以下适配器代码。它现在正在工作。

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

public class SavedTabsListAdapter extends BaseExpandableListAdapter {

    private String[] groups = { "People Names", "Dog Names", "Cat Names",
            "Fish Names" };
    private Context ctx;
    private LayoutInflater inflater;
    private String[][] children = { { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" }, { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" } };

    public SavedTabsListAdapter(Context ct) {
        ctx = ct;
        inflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int i) {
        return children[i].length;
    }

    @Override
    public Object getGroup(int i) {
        return groups[i];
    }

    @Override
    public Object getChild(int i, int i1) {
        return children[i][i1];
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.grouprow, null);
        CheckedTextView tv = (CheckedTextView) view
                .findViewById(R.id.textView1);
        tv.setText(getGroup(i).toString());
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view,
            ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.childrow, null);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(getChild(i, i1).toString());
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

}