使用自定义复合视图作为ListView的项目 [英] using custom Compound View as a Listview's item

查看:153
本文介绍了使用自定义复合视图作为ListView的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我已经开发了定制的复合视图,我不确定如何在我的列表视图中显示。

我所做的:

- >我的自定义视图化合物

 公共类HZScrollView扩展的LinearLayout {公共HZScrollView(上下文的背景下){
    超级(上下文);
    initView(上下文);
}   私人无效initView(上下文的背景下){        mContext =背景;        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        ViewGroup.LayoutParams LP =新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                               ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(LP);        //膨胀XML资源,重视
        mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.hz_scroll_view,这一点,真正的);
    }
}公共无效addContent(字符串名称,字符串年龄,性别的字符串){
//内容被添加到各个部件此化合物视图内
}

- >我的适配器

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
如果(convertView == NULL){
convertView =新HZScrollView(背景); //< ---问题!
}
}

我遇到的主要问题是,标有问题行导致异常了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.my.app/.MainActivity: android.view.InflateException:其中,合并/>可以用一个有效的ViewGroup根attachToRoot = TRUE

仅用于

在围绕净样品code时,getView(),通常膨胀的XML布局,但在我的情况该化合物的观点是完全独立的。

问:

这怎么可能插入/附上我的自定义视图复合到ListView项?<​​/ P>

解决方案

解决方案:

1)中的适配器,分配HZScrollView到convertView是细

2),以修复的LayoutParams另一个问题,需要更新使用AbsListView.LayoutParams代替ViewGroup.LayoutParams(该initView(),因为父容器是一个ListView)

3)固定InflateException,使用子视图&LT;合并&GT; 在XML中,我重构了包中的LinearLayout的子视图。注意:使用&LT;合并&GT; 在XML的hz_scroll_view文件就好了。


最有意思的,对我来说,是点#1,因为我不确定是否指定一个自定义的化合物,以列表视图项目是可行的。

Problem:

I've developed a custom compound view and I'm unsure of how to display it in my listview.

What I've done:

-> My custom compound view

public class HZScrollView extends LinearLayout {

public HZScrollView(Context context) {
    super(context);
    initView(context);
}

   private void initView(Context context) {

        mContext = context;

        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                               ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(lp);

        //inflate XML resource and attach
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.hz_scroll_view, this, true);
    }
}

public void addContent(String name, String age, String sex) {
//content is added to the individual widgets within this compound view
}

-> My Adapter

public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new HZScrollView(context); //<--- PROBLEM !
}
}

The major problem I'm experiencing is that the line marked with "PROBLEM" causes the exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/.MainActivity}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true

In sample code around the 'net, the getView() usually inflates an XML layout, but in my case the compound view is completely self-contained.

Question:

How is it possible to insert/attach my custom compound view into the listview item ?

解决方案

Solution:

1) in the adapter, assigning the HZScrollView to convertView is fine

2) to fix another problem with LayoutParams, the initView() needed to be updated to use AbsListView.LayoutParams instead of ViewGroup.LayoutParams (since the parent container is a listView)

3) to fix the InflateException, child views used <merge> in the XML, I refactored that to wrap the child views in LinearLayout's. Note: using <merge> in the XML for "hz_scroll_view" file is just fine.


The really interesting part, for me, was point #1 because I was unsure whether assigning a custom compound view to a listview item would work.

这篇关于使用自定义复合视图作为ListView的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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