【问题标题】:AddView() to Expandable ListViewAddView() 到可展开的 ListView
【发布时间】:2015-05-21 15:41:36
【问题描述】:

我有一个可扩展的 ListView。我需要将 View(与 textview 的相对布局)动态集成到 ListView 中。
我有一个 axml 文件(它就像我的动态视图的模板)。
我就是这样的

View _view = LayoutInflater.Inflate(Resource.Layout.DynamicControl,null);  

之后,我正在尝试放入可扩展列表(称为“_List”):

_List.AddView(_view,0); //where 0 is position(index)

比我使用适配器,放入 ListView 数据:

_List.SetAdapter(new _Adapter(this, List<MyClass>, _List));  

IDE 给我例外:

java.lang.UnsupportedOperationException: addView(View) 不是 在 AdapterView 中支持

我读到了,AddView() 没有集成到 listView 视图中,我需要使用(总是)适配器。这是真的吗?(也许存在解决方法,或者我可以覆盖我的构造函数(自定义_Adapter)并提供另一个参数,如上下文和视图?)。
顺便问一下,为什么AddFooterView / AddHeaderView 工作正常?

PS 对不起我的英语。谢谢!

更新:

http://i.stack.imgur.com/5LhLA.png

"My New View" - // Row for Expandable ListView,which is RelativeLayout(into this Layout i got TextView).  

我该怎么做?

【问题讨论】:

    标签: android listview xamarin expandablelistview


    【解决方案1】:

    适配器视图不支持 addView()。 块引用

    这意味着AdapterView - > ListView、ExpandableListView、ViewPager ...等

    我不知道该怎么做。 给我看样机,或者 UI 的任何图片。那我可以帮你。

    【讨论】:

    • 我添加了评论(样机),你能说点什么吗?顺便说一句,为什么 AddHeader/Footer View 可以正常工作,但对于其他 AddView,却不行?逻辑在哪里:|
    • 首先声明你的适配器和它的全局数据。在这种情况下 List myDatas = new ArrayList(); ... 添加一些数据到您的 myDatas _Adapter myAdapter = new _Adapter(... , myDatas, ...); ... _List.setAdapter(myAdapter);然后,如果要添加行: myDatas.add(yourIndex, new MyClass(...)); myAdapter.notifyDataSetChanged();
    • 谢谢老兄!我做了我的解决方法(在特定条件下在 GroupView 上查看的不同布局),但你的想法很棒。我会测试后者。
    【解决方案2】:

    是的,我们只能在适配器的帮助下将视图添加到列表视图。要添加自定义视图,我们需要通过扩展适配器类之一来定义自己的适配器,例如 BaseAdapter,CursorAdapter 等

    例如:我们有

    public class ContactListAdapter extends BaseAdapter{
        private List<String> name;
        private LayoutInflater layoutInflater;
    
        public ContactListAdapter(Activity activity) {
            layoutInflater = activity.getLayoutInflater();
            name = new ArrayList<String>();
        }
    
        public void addMessage(String message) {
            name.add(message);
            notifyDataSetChanged();
        }
        public void removeMessage(String message) {
            name.remove(message);
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return name.size();
        }
    
        @Override
        public Object getItem(int i) {
            return name.get(i);
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
    
        @Override
        public int getViewTypeCount() {
            return 2;
        }
    
        @Override
        public int getItemViewType(int i) {
             return 0;
        }
    
        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            int direction = getItemViewType(i);
    
    
                     if (convertView == null) {
    
                //LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.contact_list, viewGroup, false);
            }
    
            String message = name.get(i);
    
    
    
            final TextView txtMessage = (TextView) convertView.findViewById(R.id.contactName);
            txtMessage.setText(message.toString());
    
            ImageView imageView=(ImageView)convertView.findViewById(R.id.contactImage);
    
            return convertView;
        }
    }
    

    此适配器添加一个包含一个图像和一个 TextView 的联系人列表视图。 添加页眉和页脚视图只是为了定义页眉和页脚部分,它是固定的,与具有所有相同类型的视图页眉和页脚的列表视图无关。

    【讨论】:

      【解决方案3】:

      所以解决方法是(对我来说),生成后置条件并放置不同的布局:

      var inflater = _context.GetSystemService(Context.LayoutInflaterService) as  LayoutInflater;
      
        if(!SpecificLayout)
          {
            view = inflater.Inflate(Resource.Layout.SpecificLayout, null);
          }
        else
          {
            view = inflater.Inflate(Resource.Layout.AnotherLayout, null);
          }
      

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 1970-01-01
        • 2019-03-23
        • 2019-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多