的Android嵌套列表视图适配器仅加入第一个元素(Android nested listview

2019-10-22 20:55发布

我的目的是在这样的名单列表:

-alllist--------
----row---------
----row---------
-----(list)-----
-------textview
-------textview
---row---------
-----(list)----
.....

MainAdapter类是工作的罚款与其他元素,但PlaceAdapter仅将第一(或最后,我想不出)项的“嵌套”列表中。

问题是什么?

MainAdapter:

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

   PlaceHolder placeHolder = null;

   if (convertView == null) {
     placeHolder = new PlaceHolder();
     convertView =      mInflater.inflate(R.layout.convers_place,  null);
     placeHolder.listView = (ListView) convertView.findViewById(R.id.listView);
     convertView.setTag(placeHolder);

   } else {
      placeHolder = (PlaceHolder)convertView.getTag();
}


PlaceAdapter adapter = new PlaceAdapter(ctx);
placeHolder.listView.setAdapter(adapter);
String arr[] = conversion.message.split(Pattern.quote("!!")); // Not empty
adapter.add(new PlaceModel(arr[0]));
adapter.add(new PlaceModel(arr[1]));

}

PlaceAdapter:

public class PlaceAdapter extends BaseAdapter {

    Context ctx;
    private List<PlaceModel> places = new ArrayList<>();
    private LayoutInflater mInflater;

    public PlaceAdapter(Context ctx){
       this.ctx = ctx;
       mInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void add(PlaceModel object) {
        this.places.add(object);
    }

    @Override
    public int getCount() {
        return this.places.size();
    }

    @Override
    public PlaceModel getItem(int position) {
        return this.places.get(position);
    }

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

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

        PlaceHolder_ placeHolder = null;

        PlaceModel place = getItem(position);

        if (convertView == null) {
            placeHolder = new PlaceHolder_();
            convertView = mInflater.inflate(R.layout.convers_place_row,  null);
            placeHolder.name= (TextView)convertView.findViewById(R.id.row_name);
            placeHolder.address= (TextView)convertView.findViewById(R.id.row_address);
            placeHolder.map= (Button)convertView.findViewById(R.id.row_map);
            convertView.setTag(placeHolder);
        }

       else {
            placeHolder = (PlaceHolder_)convertView.getTag();
       }

        placeHolder.name.setText(place.getName());
        placeHolder.address.setText(place.getAddress());

        return convertView;
    }

    public static class PlaceHolder_ {
        public TextView name;
        public TextView address;
        public Button map;
    }

}

notifyDataSetChanged()不会受到影响。

Answer 1:

我有一个类似的问题,当我把一个ListView一个内ScrollView

问题是:我设置ListViewwrap_content可滚动视图内( ScrollView在我的情况)只会使它跨越周围的第一个孩子,而其他人将在溢出。

至于解决方法:更新适配器的数据(如添加新的项目)后,我也更新ListView从代码中的高度(根据儿童)。

这是一个例子 :

public class Utils {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }     
}


文章来源: Android nested listview adapter adding only first element