一个ListView可以有两种或两种以上不同类型的行布局Android中? [英] Can a ListView have two or more different types of row layouts in Android?

查看:117
本文介绍了一个ListView可以有两种或两种以上不同类型的行布局Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个非常简单的ListView在显示只是一个文本值列表的ListActivity。这是我目前的code:

I have a very simple ListView right now in a ListActivity that displays just a list of text values. Here's my current code:

public class InfoActivity extends ListActivity {

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

        List<String> values = new ArrayList<String>();
        // loads up the values array here
        // ...

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

有关InfoActivity我的XML文件,因为我用的是ListActivity有什么也没有。

My XML file for InfoActivity has nothing in it because I am using a ListActivity.

我想这样做使自定义布局的行什么。前5行将具有layoutA.xml和第二5行将具有layoutB.xml

What I want to do it make custom layouts for the rows. The first 5 rows will have layoutA.xml and the second 5 rows will have layoutB.xml.

我如何做到这一点?我迷失在哪里开始。很显然,我正在寻找最简单的code可能。

How do I do this? I'm lost on where to begin. Obviously I'm looking for the simplest code possible.

推荐答案

这是很容易的,你只需要扩展ArrayAdapter。我ArrayAdapter做两件不同的事情:

This is easy enough, you simply need to extends ArrayAdapter. My ArrayAdapter does two things differently:


  • 而不是路过一人RESOURCEID,我的适配器采取ResourceIds阵列

  • getView()检查位置并加载相应的资源

这是一个简单的例子。如果你打算使之更加复杂比上半年看起来一种方式,下半年是不同的,那么你应该覆盖的 getViewTypeCount()和<一个href=\"http://developer.android.com/reference/android/widget/BaseAdapter.html#getItemViewType%28int%29\"相对=nofollow> getItemViewType()。

This is a trivial example. If you plan on making it more complex than the first half looks one way and the second half is different, then you should override getViewTypeCount() and getItemViewType().

的例子:

public class Example extends Activity {
    public class MyArrayAdapter<T> extends ArrayAdapter<T> {
        LayoutInflater mInflater;
        int[] mLayoutResourceIds;

        public MyArrayAdapter(Context context, int[] textViewResourceId, List<T> objects) {
            super(context, textViewResourceId[0], objects);
            mInflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
            mLayoutResourceIds = textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null && position > 2)
                convertView = mInflater.inflate(mLayoutResourceIds[1], parent, false);
            return super.getView(position, convertView, parent);
        }
    }

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

        String[] array = new String[] {"one", "two", "three", "four", "five", "six"};
        List<String> list = new ArrayList<String>();
        Collections.addAll(list, array);

        ListView listView = new ListView(this);
        listView.setAdapter(new MyArrayAdapter<String>(this, new int[] {android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_single_choice}, list));
        setContentView(listView);
    }
}

这篇关于一个ListView可以有两种或两种以上不同类型的行布局Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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