【问题标题】:How do I add Views to a ListView?如何将视图添加到 ListView?
【发布时间】:2015-02-02 22:26:00
【问题描述】:

我遇到的每个示例/教程都只是将字符串添加到 ListViews,但我想添加膨胀视图。我已经测试了膨胀的视图,如果我使用 addView() 将它添加到某个 ViewGroup(如 RelativeLayout)中,它看起来很好。但是将膨胀的视图添加到 ListView 只是以文本的形式显示它:

ArrayList<View> friend_request_items = new ArrayList<View>();  //a list containing the items we want to add 

ListView friend_request_list = (ListView) findViewById( R.id.friend_request_list );

ArrayAdapter<View> friend_request_adapter = new ArrayAdapter<View>( this, android.R.layout.simple_list_item_1, friend_request_items);

friend_request_list.setAdapter( friend_request_adapter );

//friend_template is just my own xml file containing the view I'm inflating
View friend_request_item = LayoutInflater.from(this).inflate(R.layout.friend_template, null);

friend_request_items.add( friend_request_item ); 
friend_request_adapter.notifyDataSetChanged();

我认为问题在于我传递给适配器的布局,但我不知道将其更改为什么(或者这甚至是解决方案)。

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    这是一个使用自定义 ArrayAdapter 和自定义 xml 布局Using lists in Android 的不错的教程。 您应该继承一个 ArrayAdapter 并在 getView 方法中填充一行,这是一种常见且有效的解决方案,而不是将视图作为列表的通用参数传递,您应该传递一个数据类,例如 ArrayAdapter&lt;FriendRequest&gt; 或其他东西像这样

    public class FriendRequestsAdapter extends ArrayAdapter<FriendRequest> {
    private LayoutInflater inflater;
    
    public FriendRequestsAdapter(Context context, List<FriendRequest> requests) {
        super(context, R.layout.friend_request_row, requests);
        this.inflater = ((Activity)context).getLayoutInflater();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.friend_request_row, parent, false);
        }
    
        FriendRequest item = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.textView);
    
        return convertView;
    }
    

    }

    【讨论】:

      【解决方案2】:

      我遇到的每个示例/教程都只是将字符串添加到 ListViews

      还有其他示例,尽管您不太可能找到您所提议的示例。

      我想添加膨胀视图

      ListAdapter 从其getView() 方法返回“膨胀视图”。由您决定它是如何做到这一点的。

      此外,ListView 背后的意义在于能够回收列表行Views,因为列表可能很大并且行在堆空间方面很昂贵。您的代码似乎预先创建了所有行视图,这仅适用于小型数据集。

      但是将膨胀的视图添加到 ListView 只是以文本的形式显示它

      ListViewArrayAdapter 上调用getView() 时,ArrayAdapter 获取所需行的模型,在其上调用toString(),并将其倒入由其他构造函数参数标识的TextView(在在这种情况下,TextView 的格式与 android.R.id.simple_list_item_1 中定义的一样。

      我认为问题在于我传递给适配器的布局,但我不知道将其更改为什么(或者这甚至是解决方案)。

      首先,我强烈建议您重新考虑您最初的想法(“我想添加膨胀视图”)。请记住,您必须处理配置更改(例如,屏幕旋转、区域设置更改)和进程终止,并且您的“膨胀视图”在任何一种情况下都会消失。 ListViewListAdapter 旨在将模型数据转换为适当的可视化表示,并以节省内存的方式进行。

      话虽如此,如果您完全相信自己的整体方法,您将需要创建一个自定义 BaseAdapterArrayAdapter 子类,其中在 getView() 中您返回给定 @ 所需的“膨胀视图” 987654338@.

      【讨论】:

      • 感谢您的所有澄清。我认为有一种超级简单的方法可以将我膨胀的视图添加到默认的 ArrayAdapter,但我想我必须创建一个全新的类来实现该 getView() 方法。哦,关于我提供的示例,这不是我的实际代码。我只是让示例尽可能简单,这样人们就不必查看不必要的代码:) 我保证我不会预先创建行视图。
      猜你喜欢
      • 1970-01-01
      • 2014-08-26
      • 2017-09-03
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多