如何创建列表,不同的元素和动作? [英] How to create list with different elements and actions?

查看:126
本文介绍了如何创建列表,不同的元素和动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建列表,不同类型的项目。他们应该调用不同的意图或做其他事情(显示地图等)。它应该像联系方式。项和操作的数字是predefined。

I want to create list with different types of items. They should call different intents or do other things (display a map etc.) . It should act like contact details. Numbers of items and actions is predefined.

如何实现这种效果优雅?我不需要精确的code,但准则和信息去哪里找。任何帮助将AP preciated:)

How to achieve this effect elegantly? I don't need exact code but guidelines and information where to look. Any help will be appreciated :)

更新:

通过这样的效果:我的意思是创建不同类型的项目(onClickListener,布局)的列表。在上图中可以看到,你有各种各样的选择联系人:有人打电话,发电子邮件,聊天,看谷歌地图等,所有这些选项都以分组名单

By "this effect" I mean creating a list of different types of items (onClickListener, layout). On picture above you can see that you have a contact with various options: calling somebody, emailing, chatting, looking at google maps etc. All of those options are grouped at list.

我想知道是否可以通过XML布局,而不必定义适配器类的定制来实现。我想也能够添加一些静态的头行,如:分类名称。

I'm wondering if it could be achieved by xml layout without defining custom Adapter class. I want also be able to add some static header rows with eg. category name.

推荐答案

我看到实现这一目标将是确实是创建一个自定义适配器类的唯一途径。 我用它来创建一个文件浏览器,基于阉选择的项目是文件或文件夹不同的动作。

The only way I see to achieve this would be to indeed create a custom adapter class. I'm using this to create a file browsers, with different actions based on wether the item selected is a file or a folder.

基本上,你需要创建扩展ArrayAdapter(你可以用另外一个基类,如果所有的项目从同一个类继承)的自定义适配器。下面是一个示例code:

Basically, You need to create a custom adapter extending ArrayAdapter (you can use another base class if all your items inherit from the same class). Here's a sample code :

public class MyCustomAdapter extends ArrayAdapter<Object> {


    public MyCustomAdapter(Context context, int textViewResourceId,
            ArrayList<Object> objects) {
        super(context, textViewResourceId, objects);
        mList = objects;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        Object obj = mList.get(position);
        View v = convertView;
        LayoutInflater vi = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (obj.getClass().isAssignableFrom(MyClass1.class)){
            v = vi.inflate(R.layout.myclass1_item_layout, null);
            setupViewClass1(obj,v);
        } else if (obj.getClass().isAssignableFrom(MyClass2.class)){
            v = vi.inflate(R.layout.myclass2_item_layout, null);
            setupViewClass2(obj,v);
        }

        return v;
    }

    private void setupViewClass1 (Object obj, View v){  
        // setup the content of your view (labels, images, ...)
    }

    private void setupViewClass2 (Object obj, View v){  
        // setup the content of your view (labels, images, ...)
    }

    private ArrayList<Object> mList;

}

然后,你需要添加一个OnItemClickListener以及一个OnCreateContextMenuListener处理click和长preSS事件的名单上,再次使得在类的对象的过滤器。

Then you need to add an OnItemClickListener as well as an OnCreateContextMenuListener to handle the click and longpress event on your list, again making a filter on the class of your object.

这篇关于如何创建列表,不同的元素和动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆