【问题标题】:Fill Listview inside a Listview using adapters使用适配器在 Listview 中填充 Listview
【发布时间】:2015-11-22 06:43:33
【问题描述】:

我有一个片段。这个片段在他的 onCreateView 方法上加载的视图有一个 ListView(A)(在 Adapter(A) 中填充)。但是,这个 ListView(A) 里面还有另一个 ListView(B)。所以现在,我必须调用一个适配器(B)来填充这个列表视图(B)。如果我从片段中调用它,我会得到一个空指针,如果我从适配器(A)中调用它,它不会崩溃,但它不起作用。

如何在另一个适配器中调用适配器。

这是片段的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContentView = inflater.inflate(R.layout.fragment_kidscontacts, container, false);
    mAdapter = new KidsContactsAdapter(getActivity());
    final ListView list = (ListView) mContentView.findViewById(R.id.listViewKidsContacts);
    list.setAdapter(mAdapter); 
    return mContentView;
}

mAdapter 是对我所做的 Adapter(A) 的调用。 mContentView 是一个简单的视图。 现在,适配器(A)的代码,我创建了一个名为 holder 的 Holder 对象(它保存 xml 中的元素)。

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder; //this is the holder object I created

    if (convertView == null) { //View received from parameter.
        convertView = LayoutInflater.from(mContext).inflate(R.layout.kids_contacts_list_item, parent, false);
        holder = new ViewHolder(); 
        holder.listViewKidsContacts = (ListView) convertView.findViewById(R.id.insidelistViewKidsContacts); //ListView(B)
        holder.kidImage = (ImageView) convertView.findViewById(R.id.kid_image);
        holder.statusImageView = (ImageView) convertView.findViewById(R.id.status_image_view);
        holder.nameTxtView = (TextView) convertView.findViewById(R.id.kid_friend_txtview);
        holder.statusTextView = (TextView) convertView.findViewById(R.id.kid_status_txtview);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //Some work I do with the holder items assigning them 

    //Trying to fill the ListView(B)
        KidsInsideContactsAdapter mAdapter = new KidsInsideContactsAdapter(((Activity)mContext)); //This is the adapter(B)
        holder.listViewKidsContacts.setAdapter(mAdapter); //Assign the adapter(B) to the ListView(B)

    return convertView;
}

我在适配器 (B) 中有一些日志,但它们没有显示在 LogCat 中。列表视图也没有填充。它显示就像没有列表视图。但是在xml中有。

【问题讨论】:

    标签: android listview android-fragments android-listview android-adapter


    【解决方案1】:

    可能发生的一件事是第二个列表视图的高度设置不正确,因为它位于另一个列表视图中。我可能不得不处理一次。

    尝试在您的第一个适配器中使用此方法来设置列表视图的高度 - (在您设置适配器之后) 像 setListViewHeightBasedOnChildren(holder.listViewKidsContacts);

    //动态设置ListView高度的方法

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
            view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
    

    如果这不起作用,我会尝试在行上设置断点: KidsInsideContactsAdapter mAdapter = new KidsInsideContactsAdapter(((Activity)mContext)); //这是适配器(B) holder.listViewKidsContacts.setAdapter(mAdapter); //将适配器(B)分配给ListView(B)

    调试时检查 mAdapter 是否为空。还要在 KidsInsideContactsAdapter 的构造函数中放置断点,以查看是否一切正常。

    还要检查以确保 holder.listViewKidsContacts 是您在调试时要查找的列表视图。

    【讨论】:

    • 这修复了它。 getCount 返回 0,因为我犯了一个错误,所以如果有人遇到类似问题,请检查一下。
    • 感谢您的回答...我在适配器内部调用适配器,并且列表视图高度属性被动态更改...初始值为 0...我注意到当我被更改时,targetsdk更高的我看到列表视图项目不可见..在我看到你的答案后,我发现了问题......但是为什么目标低 api 项目可见但在更高目标 api 中不可见??
    【解决方案2】:

    你是对的。要填充第二个列表视图,请在您的第一个列表视图的适配器中

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

    方法(您在第一个列表视图中获取每个项目),您必须从视图中获取(第二个)列表视图(convertView 或膨胀第一个列表视图项目的布局)。一旦你抓住了你试图填充的(第二个)列表视图,在这个方法中,你会为它设置适配器,比如: ListView listView = (ListView) view.findViewById(R.id.___); //获取第一个列表视图中的第二个列表视图 listView.setAdapter(您的第二个列表视图的适配器)

    这应该可行。把你的代码放在这里,我可以看看它并告诉你为什么它可能不起作用。

    还请查看以下内容:(以防您尝试这样做)

    https://github.com/emilsjolander/StickyListHeaders

    或者 How to make sticky section headers (like iOS) in Android?

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2014-07-09
      • 1970-01-01
      • 2017-08-21
      • 2014-07-23
      • 1970-01-01
      相关资源
      最近更新 更多