在ListView图形布局中自定义列表行[英] Custom list row in ListView graphical layout

本文是小编为大家收集整理的关于在ListView图形布局中自定义列表行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

关于如何创建自定义行并将其添加到 ListView 有很多帖子和无数问题.但是,我找到的所有帖子都使用 Java 代码将自定义行布局文件设置为列表.大多数示例都这样做:

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this,R.layout.row_layout,R.id.text1,colors);

我想知道是否没有办法直接在 XML 文件中执行此操作?ListView 上没有可以为行设置自定义布局文件的属性吗?

我之所以这么问是因为我试图在 Eclipse 的图形布局工具中完成所有(大部分)布局工作,并且我希望能够在不启动应用程序的情况下预览我的布局.

这当然应该以一种或另一种方式成为可能吗?如果我可以在 XML 中使用它,我会很高兴,但如果我必须编写一个自定义类,该类将显示在图形布局编辑器的"自定义"部分,也可以这样做.

非常感谢您的时间.

推荐答案

可以在图形编辑器中预览 ListViews.只需右键单击 ListView 并选择"预览列表内容".然后选择"自定义"并选择自定义行布局.

这是一个例子:

my_custom_list.xml

 <ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false">
     <!-- Preview: listitem=@layout/account_list_item -->
 </ListView>

my_custom_row.xml

<CheckBox
    android:id="@+id/icon"

    android:layout_width="wrap_content"
    android:layout_height="fill_parent"

    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dip"
     />

<TextView  
    android:id="@+id/secondLine"

    android:layout_width="fill_parent"
    android:layout_height="26dip" 

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:singleLine="true"
    android:ellipsize="marquee"
    android:text="mail@mail.com" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/secondLine"
    android:layout_alignWithParentIfMissing="true"

    android:gravity="center_vertical"
    android:text="Account name" />

在 my_custom_list.xml 的图形编辑器中,右键单击列表.从"预览列表内容"的"自定义"选项中选择 my_custom_row.

其他推荐答案

当然可以,但首先需要创建一个继承ArrayAdapter的类<>

这个类可以是这样的:

public class Mylistcustom extends ArrayAdapter<String> {

private LayoutInflater mInflater;
private int mViewResourceId;
private String[] your_date;

    public Mylistcustom(Context ctx, int viewResourceId, String[] data) {

    super(ctx, viewResourceId,data);
    this.contexto=ctx;
    mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = viewResourceId;
    this.your_data=data;



    @Override
    public int getCount() {
    return your_data.length;
}

@Override
    public String getItem(int position) {
    return your_data[position];
}

@Override
    public long getItemId(int position) {
    return 0;
}

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

    rowView = mInflater.inflate(mViewResourceId, null);// you are inflating your xml layout
    text=(textView) rowView.findViewById(R.id.textView);//find a textview in your layout, of course you can add whatever you want such as ImaveView Buttons ....
    text.setText(your_data[position]); // and set text from your_data
    return rowView;  

}}

在列表视图活动中,您只需调用构造函数并传递数据、布局和上下文

Myadapter = new Mylistcustom(context, R.layout.YOUR_XML_LAYOUT,DATA);

// and finally set the adapter to your list
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(Myadapter);

本文地址:https://www.itbaoku.cn/post/102327.html

问题描述

There are many many posts and an infinite number of questions on how to create a custom row and add it to a ListView. All the posts I find however, use Java code to set the custom row layout file to the list. Most samples do something like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this,R.layout.row_layout,R.id.text1,colors);

I was wondering if there isn't a way to do this in the XML files directly? Isn't there a property on the ListView where you can set a custom layout file for the rows?

The reason I'm asking is because I'm trying to do all (most) of my layout work in the Graphical Layout tool in Eclipse and I would like to be able to preview my layouts in it without launching the app.

Surely this should be possible in one way or another? If I could to it in the XML I would be very happy but if I have to write a custom class that will show up in the "custom" part of the graphical layout editor that will do as well.

Many thanks for your time.

推荐答案

It is possible to preview ListViews in the graphical editor. Just right-click the ListView and choose "Preview List Content". Then select "Custom" and choose the custom row layout.

Here's an example:

my_custom_list.xml

 <ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false">
     <!-- Preview: listitem=@layout/account_list_item -->
 </ListView>

my_custom_row.xml

<CheckBox
    android:id="@+id/icon"

    android:layout_width="wrap_content"
    android:layout_height="fill_parent"

    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dip"
     />

<TextView  
    android:id="@+id/secondLine"

    android:layout_width="fill_parent"
    android:layout_height="26dip" 

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:singleLine="true"
    android:ellipsize="marquee"
    android:text="mail@mail.com" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/secondLine"
    android:layout_alignWithParentIfMissing="true"

    android:gravity="center_vertical"
    android:text="Account name" />

In the graphical editor of my_custom_list.xml, right click the list. Choose the my_custom_row from the "Custom" option in the "Preview List Content".

其他推荐答案

yes of course, but you first need to create a class that extends ArrayAdapter<>

this class could be as this one:

public class Mylistcustom extends ArrayAdapter<String> {

private LayoutInflater mInflater;
private int mViewResourceId;
private String[] your_date;

    public Mylistcustom(Context ctx, int viewResourceId, String[] data) {

    super(ctx, viewResourceId,data);
    this.contexto=ctx;
    mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = viewResourceId;
    this.your_data=data;



    @Override
    public int getCount() {
    return your_data.length;
}

@Override
    public String getItem(int position) {
    return your_data[position];
}

@Override
    public long getItemId(int position) {
    return 0;
}

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

    rowView = mInflater.inflate(mViewResourceId, null);// you are inflating your xml layout
    text=(textView) rowView.findViewById(R.id.textView);//find a textview in your layout, of course you can add whatever you want such as ImaveView Buttons ....
    text.setText(your_data[position]); // and set text from your_data
    return rowView;  

} }

and in the list view activity you only have to call the constructor and pass your data, the layout and the context

Myadapter = new Mylistcustom(context, R.layout.YOUR_XML_LAYOUT,DATA);

// and finally set the adapter to your list
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(Myadapter);