两种观点在各列表项 [英] Two views in each list item

查看:97
本文介绍了两种观点在各列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示在列表中的每个元素两种不同的观点。这两个vews是文本的意见,但我希望他们中的一个被封闭在用不同颜色的正方形。我所知道的是可能的,因为我已经知道了,但我无法理解正确!

I'm trying to show two different views in each element of the list. Both vews are text views, but I want one of them to be enclosed in a square with a different color. I know is possible because I've read about it but I'm not able to understand it properly!

任何想法?

谢谢!

推荐答案

您可以创建自己的 适配器的名单。适配器是什么决定如何显示在列表中的项目。
下面是一个例子:

You can create your own adapter for the list. The adapter is what decides how to display the items in the list.
Here is an example:

class MyAdapter extends ArrayAdapter<TheObjectsToPopulateYourList>{
        public MyAdapter(Context context, int textViewResourceId, ArrayList<TheObjectsToPopulateYourList]> objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.your_layout, null);

            TextView myTextView1 = (TextView)convertView.findViewById(R.id.yourFirstTextView);
            myTextView1.setText(getItem(position*2)); //getItem gets the item (String in this case) from the list we specified when creating the adapter.
            //position is the current position of the list, and since each position has two items, we have to multiply the position by 2 to get to the right item-list-position.
            TextView myTextView2 = (TextView)convertView.findViewById(R.id.yourSecondTextView);
            myTextView2.setText(getItem(position*2 +1 ));
            myTextView2.setBackgroundColor(0xFFFF00FF); //Any color. Use setBackgroundResource to use a resource object (drawable etc.)

            return convertView;
        }
    }

和您还需要行布局包含所有你需要的元素(这是将在每行列表的显示),我们称之为'thelinelayoutfile.xml,并把它在布局文件夹:

And you also need the line-layout to contain all the elements you need (this is what will be displayed on each line of the list), let's call it 'thelinelayoutfile.xml' and put it in the layout folder:

<?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="vertical"
    android:padding="15dp">
    <TextView 
        android:id="@+id/yourFirstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="line1"/>
    <TextView 
        android:id="@+id/yourSecondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="line2"/>
</LinearLayout>

然后,当你初始化列表(在的onCreate()的方法,也许?) 你叫

Then when you initialize your list (in your onCreate() method, perhaps?) You call

//You can create the list anywhere, or use an array.
//I will create it here, just for the sake of demonstration.
ArrayList<String> myLines = new ArrayList<String>();
myLines.add("item1, line1");
myLines.add("item1, line2");
myLines.add("item2, line1");
myLines.add("item2, line2");

//set the list adapter:
ListView myList = (ListView)findViewById(R.id.whateveryourlistidis);
myList.setAdapter(new MyAdapter(this, R.layout.thelinelayoutfile, myLines));

这篇关于两种观点在各列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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