【问题标题】:add button to only one row in android listView在android listView中仅将按钮添加到一行
【发布时间】:2013-01-28 19:34:11
【问题描述】:

我正在尝试在我的 listView 中间添加一个按钮。理想情况下,该按钮将拆分 listView,然后它将继续,但如果这不可能,我可以在 listView 的一行内使用一个按钮。 例如。我的列表视图将包含第一行(图像 + 文本)、第二行(图像 + 文本)、按钮,然后继续列表视图。

我编写了以下代码。这会在 listView 的一行中添加一个按钮,但同时它也会在我的 listView 的每一行中添加一个空按钮(带有现在文本的按钮)。此外,中心的重力设置不起作用。

我的 xml

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

     <ImageView android:id="@+id/imgUserIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:scaleType="fitStart" />

     <Button
         android:id="@+id/buttonShowHide"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:gravity="center"
         android:text="@string/showHide" />

     <TextView android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" 
                    android:layout_marginTop="5dp"
            />

</LinearLayout>

我的适配器

public class UserAdapter extends ArrayAdapter<UserAccountData> {
    Context context;
    int layoutResourceId;
    User data[] = null;

    public UserAdapter(Context context, int layoutResourceId,
            UserAccountData[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserAccountDataHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new UserAccountDataHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.imgUserIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
            holder.showHide = (Button) row.findViewById(R.id.buttonShowHide);


            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
        }

        User user = data[position];
        holder.txtTitle.setText(user.title);    
        holder.imgIcon.setImageResource(user.icon);
        holder.showHide.setText(user.buttonName);
        return row;
    }

    static class UserHolder {
        ImageView imgIcon;
        TextView txtTitle;
        Button showHide;    
    }
}

该行的我的 Java 对象。我创建了两个构造函数,一个用于按钮,一个用于图像和文本。

public class UserAccountData {
    public int icon;
    public String type;
    public String title;
    public CharSequence buttonName;
    public UserAccountData(){
        super();
    }
    // for image and text
    public UserAccountData(int icon, String title, String type) {
        super();
        this.icon = icon;
        this.title = title;
        this.type = type;
    }
    // for button
    public UserData(CharSequence buttonName, String type) {
        super();
        this.buttonName = buttonName;
        this.type = type;
    }

    public void setType(String type){
        this.type = type;
    }
}

在我的活动中,我将以下两行添加到数组中,稍后我的适配器将使用它来创建 listView(我传递给它的 ArrayList 被更改为数组)

user_data.add(new UserAccountData(icon, "title,"type"));
user_data.add(new UserAccountData("show Password","button"));

a) 有没有办法将 listView 和中间分开并只添加一个按钮?并继续相同的listView?因为我当前的解决方案试图在一行中添加一个按钮。

b) 有什么想法为什么我实际上还要在图标、标题类型行中添加一个空按钮? 我在实际的 listView 上得到图标、标题、空按钮

非常感谢

更新: 找到两个博客 http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/http://android.amberfog.com/?p=296 ,但仍然没有任何运气。将不胜感激一些更深入的帮助

【问题讨论】:

    标签: android android-listview android-button android-adapter


    【解决方案1】:

    有没有办法将 listView 和中间分开并添加一个按钮?并继续相同的listView?因为我当前的解决方案试图在一行中添加一个按钮。

    如果我理解你的问题,你想要这样的东西:

    我的列表视图将有:

    • 图片+文字
    • 图片+文字
    • 按钮
    • 图片+文字

    等等……

    如果您覆盖 getViewTypeCount()getItemViewType(),您可以拥有多种类型的行布局。

    • getViewTypeCount() 应该返回类型的数量,在本例中为 2。
    • getItemViewType(int position) 将返回 position 处的行的类型,在本例中为 0 或 1。

    加法

    我真的不知道如何区分图像文本行和按钮。我试图找到一种方法来查看我的图像是否为空(使用第二个构造函数),但这似乎不起作用

    这听起来不错,但由于iconint,它永远不会是null,未初始化整数的默认值为0。试试:

    @Override
    public int getItemViewType(int position) {
        UserAccountData data = getItem(position);
        if(data.icon == 0)
            return 1;
        return 0;
    
        // The same thing in one line:
        //return getItem(position).icon == 0 ? 1 : 0;
    }
    

    【讨论】:

    • 嘿山姆,谢谢这就是我想要得到的。我是 android 新手,想知道您是否可以给我一些示例,或者为我正在尝试完成的此类任务建议一个很好的教程? (图+文的基础教程我都做过)
    • 不久前我写了一个simple example 来回答另一个问题。否则this website 有一个很好的、重要的例子。
    • 我刚刚注意到您的更新,不要使用 logc.at 中的第一个示例,效率非常低。不过来自 amberfog.com 的那个很棒,这些图表比我发布的链接更好。您具体遇到了什么问题?
    • 我遇到的问题很少。 a)我真的不知道如何区分图像文本行和按钮。我试图找到一种方法来查看我的图像是否为空(使用第二个构造函数),但这似乎不起作用b)我查看了getViewTypeCountgetItemViewType 上的一些堆栈溢出帖子。我不确定如何在我的代码中实现它们。主要是 getItemViewType ,因为在我的情况下,我不知道按钮之前会有多少行。我将查看您在评论中发布的示例并进行报告
    • 看看你的例子,我现在更清楚我不明白的地方:)。就我而言,我不知道按钮之前会有多少行,我只知道在那之前的最后一行是什么。我不知道如何使用getViewTypeCount 以及如何判断下一行是否应该是按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2010-10-02
    • 2014-10-23
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多