zxciop110 2011-10-08 14:21
浏览 255
已采纳

这段代码如何重写ListView

网上的例子如下:

main.xml:定义Activity的界面布局:

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

android:id = "@+id/expandableListView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:drawSelectorOnTop = "false"/>

group.xml:定义一级列表的布局方式:

<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id = "@+id/textView01"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingLeft = "60px"
android:paddingTop = "10px"
android:paddingBottom = "10px"
android:textSize = "26sp"/>

child.xml:定义二级列表的布局方式:

<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_gravity = "center_vertical"
android:id = "@+id/imageView01"
android:layout_width = "70px"
android:layout_height = "70px"
android:paddingLeft = "30px"
android:paddingTop = "2px"
android:paddingBottom = "5px"
android:src = "@drawable/abc"/>
android:layout_gravity = "center_vertical"
android:id = "@+id/childTo"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingLeft = "30px"
android:paddingTop = "10px"
android:paddingBottom = "5px"
android:textSize = "30sp"/>

ExpandableList.java代码如下:

package snowfox.expandable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExpandableList extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExpandableListView elv = (ExpandableListView)findViewById(R.id.expandableListView);

//准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2"
List> groups = new ArrayList>();
Map group1 = new HashMap();
group1.put("group", "group1");
Map group2 = new HashMap();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);

//准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2"
List> child1 = new ArrayList>();
Map child1Data1 = new HashMap();
child1Data1.put("child", "child1Data1");
Map child1Data2 = new HashMap();
child1Data2.put("child", "child1Data2");
child1.add(child1Data1);
child1.add(child1Data2);

//准备第二个一级列表中的二级列表数据:一个二级列表,显示"child2Data1"
List> child2 = new ArrayList>();
Map child2Data1 = new HashMap();
child2Data1.put("child", "child2Data1");
child2.add(child2Data1);
//用一个list对象保存所有的二级列表数据
List>> childs = new ArrayList>>();
childs.add(child1);
childs.add(child2);

ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groups, childs);
elv.setAdapter(viewAdapter);
}

//自定义的ExpandListAdapter
class ExpandableAdapter extends BaseExpandableListAdapter
{
private Context context;
List> groups;
List>> childs;

/*

  • 构造函数:
  • 参数1:context对象
  • 参数2:一级列表数据源
  • 参数3:二级列表数据源
    */
    public ExpandableAdapter(Context context, List> groups, List>> childs)
    {
    this.groups = groups;
    this.childs = childs;
    this.context = context;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
    return childs.get(groupPosition).get(childPosition);
    }

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

    //获取二级列表的View对象
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
    ViewGroup parent)
    {
    @SuppressWarnings("unchecked")
    String text = ((Map) getChild(groupPosition, childPosition)).get("child");
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //获取二级列表对应的布局文件, 并将其各元素设置相应的属性
    LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.child, null);
    TextView tv = (TextView) linearLayout.findViewById(R.id.childTo);
    tv.setText(text);
    ImageView imageView = (ImageView)linearLayout.findViewById(R.id.imageView01);
    imageView.setImageResource(R.drawable.abc);

    return linearLayout;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
    return childs.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition)
    {
    return groups.get(groupPosition);
    }
    @Override
    public int getGroupCount()
    {
    return groups.size();
    }

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

    //获取一级列表View对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
    String text = groups.get(groupPosition).get("group");
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //获取一级列表布局文件,设置相应元素属性
    LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
    TextView textView = (TextView)linearLayout.findViewById(R.id.textView01);
    textView.setText(text);
    return linearLayout;
    }

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

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

    }
    }

  • 写回答

2条回答 默认 最新

  • 「已注销」 2011-10-08 15:34
    关注

    你可以创建一个类继承ListView.
    比如 类名 MyListView.java 继承ListView
    在包名为 com.tracy 中

    接下来就是在xml里使用刚重写的ListView了
    一般在xml用系统的listView控件是这样写的
    (注:那个属性名不一对哦,是手写的)
    使用自己重写的方法

    这样就可以了,不过你会发现自己重写的控件在xml里不能使用补全方式还获取控件属性。这个是正常的,如果要显示的话需要配置一些东西。

    好了。这样就可以了。希望你能看懂.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?