ListView与子项目。 Android示例

时间:2016-02-26 09:15:37

标签: android

我是Android编程的新手,我想创建一个带有ListView和多个子项的应用程序需要一些建议,链接样本。 应用程序可能如下所示。

由于 enter image description here

3 个答案:

答案 0 :(得分:0)

对于此布局,您应该拥有适配器自定义适配器的概念。

然后通过以下链接 -

https://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

答案 1 :(得分:0)

可展开的ListView肯定会解决您的问题!

点击此链接: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

让我知道这是否适合你!!

答案 2 :(得分:0)

这是一个简单的可扩展列表示例。

第一 -

1 - 在activity_main.xml中创建ExpandableListView

2-Intialize并在MainActivity.java文件上设置适配器。

3 - 创建适配器。

4 - 创建父级和子级布局xml文件。

我的代码如下........

activity_main.xml中..............................

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Expandable List"
        android:layout_gravity="center"
        android:textColor="#30b171"
        android:background="#dde3ec"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

</LinearLayout>

MainActivity.java .........

package com.example.expandblelistapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

ExpandableListView explist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        explist=(ExpandableListView) findViewById(R.id.expandableListView1);
        MyExpandableListAdapter adapter=new MyExpandableListAdapter(this);
        explist.setAdapter(adapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

MyExpandableListAdapter.java ..

package com.example.expandblelistapp;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter{


    String[] groups={"Android","Apache_tomcat","Apple","Microsoft","Oracle"};

    String[][] children={
            {"JellyBean","HoneyComb","Kitkat","CupCake","Lollipop",},
            {"tomcat server","xampp server","wamp server","lamp server"},
            {"iphone","ipad","iwatch"},
            {".net","sqlServer","msoffice","xbox","explorer",},
            {"sap","business suite"},
                      };
    Integer[] img={R.drawable.android,R.drawable.apachetomcat,R.drawable.apple,
                    R.drawable.microsoft,R.drawable.oracle};


     Integer[][] childrenimg={
            {R.drawable.jellybean,R.drawable.gingerbread,R.drawable.kitkat,R.drawable.cupcake,R.drawable.lollipop},
            {R.drawable.images,R.drawable.images,R.drawable.images,R.drawable.images,},
            {R.drawable.iphone,R.drawable.ipad,R.drawable.iwatch},
            {R.drawable.net,R.drawable.outlook,R.drawable.office,R.drawable.xbox,R.drawable.explore},
            {R.drawable.osap,R.drawable.oserver},
                      };




     Context ctx;



    public MyExpandableListAdapter(Context context) {
         ctx=context;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return children[groupPosition][childPosition];
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return children[groupPosition].length;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.child_layout,parent,false);



         ImageView i=(ImageView) convertView.findViewById(R.id.imageView2);
         i.setImageResource(childrenimg[groupPosition][childPosition]);


        TextView dis=(TextView)convertView.findViewById(R.id.childtext);
        dis.setText(getChild(groupPosition, childPosition).toString());
        return convertView;


    }



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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

    LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView=inflater.inflate(R.layout.parent_layout,parent,false);

     ImageView i=(ImageView) convertView.findViewById(R.id.imageView1);
        i.setImageResource(img[groupPosition]);


    TextView dis=(TextView)convertView.findViewById(R.id.parenttext);
    dis.setText(groups[groupPosition]);
    return convertView;

    }

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



}

parent_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/parenttext"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

child_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/childtext"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="Large Text"
        android:textSize="15sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>