4

Viewの子に子を追加する方法を模索してきましたListExpandableListActivity、役に立ちませんでした。

を取得しExpandableListViewて呼び出すことはできますが、それはグループリストaddFooterView()に新しいものを追加するだけです。View(データはから来ていますCursor)。

どんな提案もありがたく受けました。

乾杯

ジェームズ

4

3 に答える 3

15

残念ながら、ExpandableListView.addChildFooterView()方法はありませんExpandableListView.addFooterView()。呼び出しているメソッドは継承ListView.addFooterView()されたもので、リスト全体の最後の項目としてビューを追加するだけです。グループ/子の動作ではオーバーライドされませんでした。

これを行う最も簡単な方法は、おそらく、データセットサイズ+ 1を返すExpandableListAdapterように実装に何かを組み込み、適切な位置のフッタービューを返すことです。また、特定のグループの最後の子を取得するときにフラグを立てるブールパラメーターを提供します。getChildrenCount()getChildView()getChildView()

于 2011-01-04T20:58:22.717 に答える
2

このようなカスタムアダプタを作成し、そのインスタンスをExpandableListViewに渡しました。

public class MyExpandableAdapter extends BaseExpandableListAdapter {
Context context;
List<ParentObject> parentObjects;

public MyExpandableAdapter(Context context, List<ParentObject> parentObjects)
{
    this.context = context;
    this.parentObjects = parentObjects;
}
@Override
public int getGroupCount() {
    return parentObjects.size();
}

 //Add 2 to childcount. The first row and the last row are used as header and footer to childview
@Override
public int getChildrenCount(int i) {
    return parentObjects.get(i).childObjects.size() +2;
}

@Override
public ParentObject getGroup(int i) {
    return parentObjects.get(i);
}

@Override
public ChildObject getChild(int i, int i2) {
    return parentObjects.get(i).childObjects.get(i2);
}

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

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

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    ParentObject currentParent = parentObjects.get(i);
    if(view ==null)
    {
        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.parent_row,null);
    }
    TextView txtMother = (TextView)view.findViewById(R.id.txtMother);
    TextView txtFather = (TextView)view.findViewById(R.id.txtFather);
    txtMother.setText(currentParent.mother);
    txtFather.setText(currentParent.father);
    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    ParentObject currentParent = getGroup(groupPosition);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //the first row is used as header
    if(childPosition ==0)
    {
        view = inflater.inflate(R.layout.child_header, null);
        TextView txtHeader = (TextView)view.findViewById(R.id.txtHeader);
        txtHeader.setText(currentParent.textToHeader);
    }

    //Here is the ListView of the ChildView
    if(childPosition>0 && childPosition<getChildrenCount(groupPosition)-1)
    {
        ChildObject currentChild = getChild(groupPosition,childPosition-1);
        view = inflater.inflate(R.layout.child_row,null);
        TextView txtChildName = (TextView)view.findViewById(R.id.txtChildName);
        TextView txtChildAge = (TextView)view.findViewById(R.id.txtChildAge);
        txtChildName.setText(currentChild.childName);
        txtChildAge.setText("Age: " + String.valueOf(currentChild.age));
    }
    //the last row is used as footer
    if(childPosition == getChildrenCount(groupPosition)-1)
    {
        view = inflater.inflate(R.layout.child_footer,null);
        TextView txtFooter = (TextView)view.findViewById(R.id.txtFooter);
        txtFooter.setText(currentParent.textToFooter);
    }
    return view;
}

@Override
public boolean isChildSelectable(int i, int i2) {
    return false;
}

}

これが問題の解決策ですhttp://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/。お役に立てば幸いです。

于 2015-06-07T03:17:01.013 に答える
-1

より簡単な解決策があります:

適用されたビューに「OnClickListener」を設定するだけです。

View view = inflater.inflate(R.layout.xxx, null);
view.setOnClickListener(new OnClickListener(){
 @Override
public void onClick(View v) {
    //do something
 }
});

それを解決した非常に簡単なこと!

于 2013-09-04T22:09:14.650 に答える