我试图在列表的每个元素中显示两个不同的视图.两个vews都是文本视图,但我希望其中一个被包含在具有不同颜色的正方形中.我知道是可能的,因为我已经读过它,但我无法理解它!
有任何想法吗?
谢谢!
解决方法:
您可以为列表创建自己的adapter.适配器决定如何显示列表中的项目.
这是一个例子:
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’并将其放在布局文件夹中:
<?xml version="1.0" en@R_404_5563@="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()方法中,也许?)
你打电话
//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));
总结 以上是内存溢出为你收集整理的android – 每个列表项中的两个视图全部内容,希望文章能够帮你解决android – 每个列表项中的两个视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)