如何在android代码的expandablelistview中添加包含Edittexts,textviews,buttons等的另一个布局?

时间:2012-12-26 06:02:40

标签: android expandablelistview

我是Android的新手,在我正在开发的应用程序中,我想添加一个包含注册页面的expandablelistview。 它可以是这样的,

   Add Personal Details +
       name(edittext)
       ph_no(edittext)
       email_id(edittext)
       Save Button


  Add Acocunt Details +
      Transaction_id(edittext)
      Transaction_type(edittext)
      Total_amount(edittext)
      Save Button

等...

怎么可能?

3 个答案:

答案 0 :(得分:0)

您必须为Expandablelistview创建自定义适配器。

答案 1 :(得分:0)

您可以使用ExpandableListView和customAdapter

此处的活动实现onChildClickListener onGroupCollapseListener和onGroupExpandListener

请注意,我是jus直接输入代码,可能有编译错误,请修复,如果有的话,请发表评论

   public class YourActivity extends Activity implements OnChildClickListener, OnGroupCollapseListener{

private ExpandableListView exList= null;
private ExpandableListAdapter mAdapter = null;

//add all unimplemented methods

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    placeHolder = (LinearLayout) findViewById(R.id.placeHolder);

    exList= (ExpandableListView) findViewById(R.id.wl_list);

    exList.setHorizontalFadingEdgeEnabled(true);
    exList.setFastScrollEnabled(true);
    exList.setHorizontalFadingEdgeEnabled(true);
    exList.setFadingEdgeLength(30);
    exList.setDividerHeight(1);
    exList.isFocusable();
    exList.setTextFilterEnabled(true);
    exList.setSelectionAfterHeaderView();
    exList.setSelected(true);

    exList.setOnChildClickListener(this);
    mAdapter = new ExpandableListAdapter(this);
    exList.setAdapter(mAdapter);

    exList.setOnGroupCollapseListener(this);
    exList.setOnGroupExpandListener(this);

    setContentView(exList); 

}


public void onGroupCollapse(int groupPosition) {

    // do whatever additional
    exList.collapseGroup(groupPosition);
}

public void onGroupExpand(int groupPosition) {

    // do whatever additional
    exList.expandGroup(groupPosition);
}

}

和自定义适配器一样,我只实现了getChildView()方法,请为getGroupView()方法做同样的完成

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
                private Activity activity;

        public ExpandableListAdapter(Activity activity) {
            this.activity = activity;
        }

        public View getCustomChildView(int groupPosition, int childPosition) {
            WORKLIST_HEADER wfHeader = null;
            //View view = null;
            //if (view == null) {
                // you can do this also, if you prefer filling up screens via xml, that is, inflate
                //view = inflater.inflate(R.layout.wl_list_row, null);
            //}


            //or jus create a linear layout manually and add views to it like below and return the linear layout, this should do
                TextView textView = null;
                LinearLayout wflayout = new LinearLayout(activity);    


                textView = new TextView(activity);
                textView.setText("name");
                textView.setTextAppearance(activity, R.style.boldText);

                wflayout.addView(textView);

                textView = new TextView(activity);
                textView.setText("email_id");
                textView.setTextAppearance(activity, R.style.boldText);



                wflayout.addView(textView);

// add more and button as well,

        //return linear layout
            return wflayout;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
                return getCustomChildView(groupPosition, childPosition);
        }


    // add otehr  unimplemented methods


    }

答案 2 :(得分:0)

您可以使用水平方向的linearlayout并为每个线性布局指定名称。这样您就可以扩展为在单个xml文件中使用可扩展列表视图。