创建可扩展列表时出错

时间:2014-12-07 04:39:57

标签: android android-layout android-fragments

关注此tutorial

我遇到了一些我不确定如何解决的错误。在本教程中,它扩展了ExpandableListActivity,但我无法这样做,因为我使用Fragment所以我必须扩展它。

expandableList.setOnChildClickListener(this);

我也收到错误:

 adapter.setInflater((LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);

错误为LayoutInflater cannot be resolved to a variable

如果我在expandableList上设置一个新的监听器,我不确定这是否有效,因为监听器及其方法已经在MyExpandableAdapter

无论如何,我有点迷失在这里,如果有人能帮助我解决这个问题,那就太棒了!

感谢有人看到这个!

 public class MuscleGroupFragment extends Fragment   {

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

    public MuscleGroupFragment() {
    // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_muscle_group, container, false);

        expandableList = (ExpandableListView) rootView.findViewById(R.id.list);
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        setGroupParents();
        setChildData();

        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
        adapter.setInflater((LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);  //Getting an error on LayoutInflater

       expandableList.setAdapter(adapter);
       expandableList.setOnChildClickListener(this);  //Issue is this line



        return rootView;    
 }

这是适配器类

 public class MyExpandableAdapter extends BaseExpandableListAdapter{

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

public MyExpandableAdapter(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;
}

@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.muscle_group_parent, null);
        }

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

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                final String message = child.get(childPosition);
                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.muscle_group_children, null);
    }

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

   return convertView;

}

@Override
public void onGroupCollapsed(int groupPosition) {
    // TODO Auto-generated method stub
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    // TODO Auto-generated method stub
    super.onGroupExpanded(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {

    return ((ArrayList<String>) childtems.get(groupPosition)).size();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}


@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;
}

}

1 个答案:

答案 0 :(得分:2)

根据您的评论,问题就在这一行:

expandableList.setOnChildClickListener(this);  //Issue is this line

这里基本上你将可扩展列表视图的OnChildClickListener设置为&#34;这个&#34;即。 MuscleGroupFragment类对象,但此类未实现OnChildClickListener接口。要解决此问题,您必须实现此接口并提供此接口方法的实现:

ExpandableListView.OnChildClickListener

在您要引用的示例中,使用的是ExpandableListActivity,因此它已在内部实现。但是当您扩展Fragment类时,您必须实现OnChildClickListener。

希望这会对你有所帮助。