ListView的定制行布局 - 机器人 [英] ListView with customized Row Layout - Android

查看:122
本文介绍了ListView的定制行布局 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个活动包含一个列表,其中行有一个自定义布局。 所以,我创建了 list_entry_layout.xml 文件中定义,我的列表中的每一行应该具有的布局(在我的例子中的每一行应该有一个标题和摘要):

 < XML版本=1.0编码=UTF-8&GT?;

<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / list_entry_title
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =20dp>
    < / TextView的>

    <的TextView
        机器人:ID =@ + ID / list_entry_summary
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =10dp>
    < / TextView的>

< / LinearLayout中>
 

我的问题是,我不知道如何将数据添加到 ListActivity 类的每一行。 用下面的code段我能加入每一行的标题:

 公共类MyActivity扩展ListActivity
{

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        的setContentView(R.layout.list_activity);

        ListView控件的ListView =(ListView控件)findViewById(android.R.id.list);
        的String []值=新的String [] {机器人,iPhone,WindowsMo​​bile的,
            黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X,
            Linux的,OS / 2};

        ArrayAdapter<字符串> titleAdapter =新的ArrayAdapter<字符串>(这一点,R.layout.list_entry_layout,R.id.list_entry_title,价值观);
        //指定适配器的ListView
        listView.setAdapter(titleAdapter);

    }
}
 

  

有关还加入了总结,我应该怎么办?

如果我添加此code我会有摘要可视化,而不是标题:

 的String []值=新的String [] {Android_summary,iPhone_summary,WindowsMo​​bile_summary,Blackberry_summary,WebOS_summary,Ubuntu_summary,Windows7_summary 最大OS X_summary,Linux_summary,OS / 2_summary};
ArrayAdapter<字符串> summaryAdapter =新的ArrayAdapter<字符串>(这一点,R.layout.list_entry_layout,R.id.list_entry_summary,价值观);
//指定适配器的ListView
listView.setAdapter(summaryAdapter);
 

下面是结果,我想获得:

解决方案

您需要创建自己的 ArrayAdapter

 私有类YourAdapter扩展ArrayAdapter<字符串> {
   //做一些工作
}
 

那么你应该指定如何会看你的行与XML,正是为了自己的目标,我建议你使用RelativeLayout的,它可以是这样的:

row.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的TextView
        机器人:ID =@ + ID /名称
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        />

    <的TextView
        机器人:ID =@ +帐号/电邮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ ID /名称
        />

< / RelativeLayout的>
 

于是在 YourAdapter 您需要设置超constuctor:

 公共YourAdapter(){
   超级(YourActivity.this,R.layout.row,数据);
}
 

那么对于自定义你的数据在的ListView +更有效地执行我建议你重写 getView()方法还可以使用座的设计模式

  @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
   ViewHolder支架=无效;
   LayoutInflater充气= getLayoutInflater();
      如果(convertView == NULL){
         convertView = inflater.inflate(R.layout.row,空,假);
         持有人=新ViewHolder(convertView);
         convertView.setTag(保持器);
      }
      其他 {
         支架=(ViewHolder)convertView.getTag();
      }
      holder.getUpperText()的setText(数据源[位置])。
      holder.getLowerText()的setText(数据源[位置])。

   返回convertView;
}
 

最后只是初始化的ListView ,并设置适配器

 的ListView列表=(ListView控件)findViewById(R.id.list);
list.setAdapter(新YourAdapter());
 


注意: 设计图案持有人重新保存每一行的子部件,所以你需要presents任意对象发现他们只有一次,然后用对象,你永远有机会获得它们。

实施可以是这样的:

 公共类ViewHolder {
   私人视图行;
   私人TextView的upperText = NULL,lowerText = NULL;

   公共ViewHolder(查看行){
      this.row =行;
   }

   公众的TextView getUpperText(){
      如果(this.upperText == NULL){
         this.upperText =(TextView中)inView.findViewById(R.id.someId);
      }
      返回this.upperText;
   }

   公众的TextView getLowerText(){
      如果(this.lowerText == NULL){
         this.lowerText =(TextView中)inView.findViewById(R.id.someId);
      }
      返回this.lowerText;
   }
}
 


希望它能帮助。

I'd like to create an Activity containing a list in which the rows have a custom layout. So I've created the list_entry_layout.xml file defining the layout that each row of my list should have (in my example each row should have a title and a summary):

<?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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp" >
    </TextView>

    <TextView
        android:id="@+id/list_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp" >
    </TextView>

</LinearLayout>

My problem is that I do not know how to add the data to each row in the ListActivity class. With the following code snippet I'm able to add the titles of each row:

public class MyActivity extends ListActivity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

        ArrayAdapter<String> titleAdapter = new ArrayAdapter<String>(this, R.layout.list_entry_layout, R.id.list_entry_title, values);
        // Assign adapter to ListView
        listView.setAdapter(titleAdapter);

    }
}

For adding also the summary how should I do?

If I add this code I will have the summaries visualized, and not the titles:

String[] values = new String[] { "Android_summary", "iPhone_summary", "WindowsMobile_summary", "Blackberry_summary", "WebOS_summary", "Ubuntu_summary", "Windows7_summary", "Max OS X_summary", "Linux_summary", "OS/2_summary" };
ArrayAdapter<String> summaryAdapter = new ArrayAdapter<String>(this, R.layout.list_entry_layout, R.id.list_entry_summary, values);
// Assign adapter to ListView
listView.setAdapter(summaryAdapter);

The following is the result I'd like to obtain:

解决方案

You need to create your own ArrayAdapter:

private class YourAdapter extends ArrayAdapter<String> {
   // do some work
}

Then you should specify how will look your row with XML, exactly for your goal, i recommend to you use RelativeLayout and it can looks like this:

row.xml

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

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        />

    <TextView 
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/name"
        />

</RelativeLayout>

So then in YourAdapter you have to set super constuctor:

public YourAdapter() {
   super(YourActivity.this, R.layout.row, data);
}

Then for customize your data in ListView + more effective implementation i recommend to you override getView() method and also use Holder design pattern.

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
   ViewHolder holder = null;
   LayoutInflater inflater = getLayoutInflater();
      if (convertView == null) {
         convertView = inflater.inflate(R.layout.row, null, false);
         holder = new ViewHolder(convertView);
         convertView.setTag(holder);
      }
      else {
         holder = (ViewHolder) convertView.getTag();
      }     
      holder.getUpperText().setText(dataSource[position]);
      holder.getLowerText().setText(dataSource[position]);

   return convertView;  
}

Finally just initialize ListView and set Adapter:

ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new YourAdapter());


Note: Design pattern Holder represents arbitrary object that holds child widgets of each row, so you need to find them only once and then with Holder object you will always have access to them.

Implementation of Holder can looks like this:

public class ViewHolder {
   private View row;
   private TextView upperText = null, lowerText = null;

   public ViewHolder(View row) {
      this.row = row;
   }

   public TextView getUpperText() {
      if (this.upperText == null) {
         this.upperText = (TextView) inView.findViewById(R.id.someId);
      }
      return this.upperText;
   }

   public TextView getLowerText() {
      if (this.lowerText == null) {
         this.lowerText = (TextView) inView.findViewById(R.id.someId);
      }
      return this.lowerText;
   }
}


Hope it helps.

这篇关于ListView的定制行布局 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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