多列表视图?

时间:2015-09-11 09:42:38

标签: android listview android-listview

我试图弄清楚如何制作这个应用程序很长一段时间,我是一个新手所以是的,对你来说对我来说容易对你来说当然更难。

在说完之后我会向你解释我的问题:

我想制作一个列出 ListView 的应用,其中包含 NO 元素,但是,您可以通过 + ' + '添加。按钮。

行为

点击 +按钮,插入元素的名称,转到...现在的ListView 使用您提供的名称显示 元素

现在真正的魔法'发生的情况:

让我们说我创造了一个&#39; Bag &#39; element,我在这个包上有一些对象,我想添加它们,但在相同 ListView <中添加它们不会有用/ strong> of Bag,所以:

点击 &#39;元素,一个品牌 new (空)ListView,我们的特殊&#39; + &#39;按钮,这将允许我创建 元素包中的对象。

只是为了让您明白..我希望该应用能够动态,以便在包<<>中包含 / strong> ..你总是可以在另一个和另一个

中创建 ListView

如果您没有了解此应用的行为,请不要暂停&#39;这个问题,而是向我提问,我很乐意回答所有人的问题!

IMGUR:http://imgur.com/a/lghZx

1 个答案:

答案 0 :(得分:1)

First it is not advisable to have listview inside a listview , as it creates scrolling problem .ExpandableListView is recommended

Preview: enter image description here

Now in this tutorial you will be able to add list item and sub list item with edittext , Lets start with defining layouts

1.Define ExpandableListView tag in activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1" >

    <EditText
        android:id="@+id/add_item_et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:onClick="addItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />
</LinearLayout>

<ExpandableListView
    android:id="@+id/parent_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ExpandableListView>

  1. Define layout for outer list list_group.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:background="#000000">
    
    
    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="17dp"
        android:textColor="#f9f93d" />
    
    </LinearLayout>
    
  2. Create layout for child list (We will have to different layout for child)

list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:orientation="vertical" >


    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

    </LinearLayout>

list_item_2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <EditText
        android:id="@+id/add_child_list_et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" 
        android:layout_marginLeft="32dp">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/add_child_list_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />

</LinearLayout>
  1. Define ExpandableListView in MainActivity.java

    public class MainActivity extends Activity {

    private ExpandableListView elv;
    private ExpandableListAdapter adapter;
    private List<String> listDataHeader;//This is main List
    private HashMap<String, List<String>> listDataChild;//This is child list
    private EditText addHeaders;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);
    
        elv = (ExpandableListView) findViewById(R.id.parent_list_view);
        addHeaders=(EditText) findViewById(R.id.add_item_et);
        init();
    
        adapter = new ExpandableListAdapter(MainActivity.this,listDataHeader,listDataChild);//This is a custom adapter that we will define shortly
    
        elv.setAdapter(adapter);
    
    
    }
    
    private void init() {
    
        listDataHeader = new ArrayList<String>();
        listDataChild=new HashMap<String, List<String>>(); 
    
        //Just creating one item in our list to start with something
        listDataHeader.add(new String("Header 1"));
        List<String> subHeaderOne=new ArrayList<String>();
        subHeaderOne.add("");//Create First item always empty as we will have different layout for this
        subHeaderOne.add("Header1 SubHeading 1");   
    
        listDataChild.put(listDataHeader.get(0), subHeaderOne);
    
    }
    
    public void addItem(View v) {
        //We will add new item in listDataHeader and also in listDataChild
        String header="";
        header=addHeaders.getText().toString();
        if(header.length()>1){
            listDataHeader.add(header);
            List<String> childList=new ArrayList<String>();
            childList.add("");//necessary to have atleast one empty item in our child list where we will set different layout
            listDataChild.put(header, childList);
        }
    
        addHeaders.setText("");
        adapter.notifyDataSetChanged();
    
        }
    
     }
    
  2. Now the Dirty part defining our own custom adapter ExpandableListAdapter.java

    public class ExpandableListAdapter extends BaseExpandableListAdapter{
    
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listDataChild;
    private Context context;
    
    public ExpandableListAdapter(Context context,
            List<String> listDataHeader,
            HashMap<String, List<String>> listDataChild) {
            this.listDataHeader=listDataHeader;
            this.listDataChild=listDataChild;
            this.context=context;
    }
    
    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return listDataChild.get(listDataHeader.get(groupPosition)).size();
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        return listDataHeader.get(groupPosition);
    }
    
    
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    
    
    @Override
    public boolean hasStableIds() {
        return false;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        final String headerText=(String) getGroup(groupPosition);
        if(convertView==null){
            LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.list_group, null);
        }
    
        TextView txtListHeader=(TextView) convertView.findViewById(R.id.lblListHeader);
        txtListHeader.setTypeface(null,Typeface.BOLD);
        txtListHeader.setText(headerText);
        return convertView;
    }
    
    
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    
    @Override
    public View getChildView(final int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText=(String) getChild(groupPosition, childPosition);
    
        if(convertView!=null)convertView=null;// If we remove this line then there was problem , Haven't figured it out , its temp solution
    
    
    
        if(convertView==null){
            LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            switch(childPosition){
            case 0:     
                //If child item is the first item in sub list ,we will set layout which contain EditText and button so to add more item in sub list(child list)
                convertView=inflater.inflate(R.layout.list_item_2, null);
                convertView.setId(0);
                break;
            default:
                //If child item is any other than first item in sub list ,we will set layout which contain just TextView
                convertView=inflater.inflate(R.layout.list_item, null);
                convertView.setId(1);
            }
    
        }
    
        int viewId=convertView.getId();
    
        switch(viewId){
        case 0:
            final EditText editText=(EditText) convertView.findViewById(R.id.add_child_list_et);
            Button addButton=(Button) convertView.findViewById(R.id.add_child_list_bt);
    
            addButton.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    String txt="";
    
                    txt=editText.getText().toString();
                    List<String> newList=listDataChild.get(listDataHeader.get(groupPosition));
                    newList.add(txt);
                    editText.setText("");
                    notifyDataSetChanged();
    
    
                }
            });
            break;
        case 1:
            TextView txtListChild=(TextView) convertView.findViewById(R.id.lblListItem);
            txtListChild.setText(childText+" "+childPosition);
            break;
        }
    
    
    
    
        return convertView;
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return (listDataChild.get(listDataHeader.get(groupPosition))).get(childPosition);
    }
    
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
     }
    

Feel Free to ask anything