如何在可扩展列表视图上

时间:2015-11-29 05:35:32

标签: android expandablelistview

我创建了一个可扩展的listView,我能够获得它的子位置,我想要实现的是当我点击子元素时我需要被重定向到另一个活动。

我使用的代码如下

Adapter.java

private Activity activity;
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public StudentAcademicMenuExpandableAdapter(Context context, List<String> listDataHeader,
                                            HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
    this.activity=activity;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

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

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.student_academic_menu_expandable_list, null);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(activity,childPosition,Toast.LENGTH_SHORT).show();
            }
        });
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);
    /*txtListChild.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(_context, (CharSequence) _listDataChild.get(childPosition).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });*/
    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.student_academic_menu_expandable_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

MainActivity.java

listDataHeader = new ArrayList<String>(listDataChild.keySet());
    listAdapter = new StudentAcademicMenuExpandableAdapter(this, listDataHeader, listDataChild);
    expListView.setAdapter(listAdapter);
    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
    {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " List Expanded.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener()
    {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " List Collapsed.",
                    Toast.LENGTH_SHORT).show();

        }
    });
    // preparing list data
    //listAdapter = new StudentAcademicMenuExpandableAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    //expListView.setAdapter(listAdapter);
    expListView.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;

        }
    });

1 个答案:

答案 0 :(得分:3)

为Adapter Class创建以下接口。

public static interface AdapterOnClickHandler {
    void onClick();
}

然后,将接口放在Adapter Class中。

private AdapterOnClickHandler handler;
public void setOnClickHandler(AdapterOnClickHandler handler) {
    this.handler = handler;
}

修改你的getChildView方法make onClick。

@Override
public View getChildView(int groupPosition, final int childPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {

   final String childText = (String) getChild(groupPosition, childPosition);
   if (convertView == null) {
       LayoutInflater infalInflater = (LayoutInflater) this._context
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView =    infalInflater.inflate(R.layout.student_academic_menu_expandable_list, null);

   }

   TextView txtListChild = (TextView) convertView
           .findViewById(R.id.lblListItem);
   txtListChild.setText(childText);

   //Add this code in your method.....
   convertView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               handler.onClick();
            }
       });
   return convertView;
}

在MainActivity.java中,您可以订阅AdapterClick,如下所示

listAdapter.setOnClickHandler(new Adapter.AdapterOnClickHandler() {
      @Override
      public void onClick(BranchModel branchModel) {
         //Do something on click.....                          
      }
});