【问题标题】:Clickable Views in Custom ExpandableListView自定义 ExpandableListView 中的可点击视图
【发布时间】:2012-07-07 06:03:34
【问题描述】:

transferAvailPowered.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="left">
<TextView
    android:id="@+id/availSerial"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:focusable="false"
    android:padding="5dp"
    android:gravity="left" />
<TextView
    android:id="@+id/availModel"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:focusable="false"
    android:padding="5dp"
    android:gravity="left" />
<AutoCompleteTextView
    android:id="@+id/availSite"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:hint="To Site"
    android:background="@android:color/white"
    android:textColor="@android:color/black"
    android:textCursorDrawable="@null"
    android:focusable="false"
    android:layout_margin="5dp"
    android:gravity="left" />
<ImageButton
    android:id="@+id/addToTransfer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:background="@drawable/addsmall"
    android:focusable="false"
    android:gravity="left" />

transferAvailAttached.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp">
<TextView
    android:id="@+id/availSerial"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:focusable="false"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="right" />
<TextView
    android:id="@+id/availModel"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<CheckBox
    android:id="@+id/include"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left|center_vertical"
    android:focusable="false"
    android:layout_marginRight="30dp"
    android:background="@drawable/bg_checkbox" />
<ImageButton
    android:id="@+id/removeAttachment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:background="@drawable/deletesmall"
    android:gravity="right" />

适配器

class EquipAdapter : BaseExpandableListAdapter
{
    private List<CPEquipment> Parent { get; set; }
    private List<List<CPEquipment>> Child { get; set; }
    private Context _context { get; set; }
    private IListAdapter _adapter { get; set; }
    private ExpandableListView _list { get; set; }

    public EquipAdapter(Context context, List<CPEquipment> parent, List<List<CPEquipment>> child, IListAdapter adapter, ExpandableListView list)
    {
        _context = context;
        Parent = parent;
        Child = child;
        _adapter = adapter;
        _list = list;
    }

    public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
    {
        List<CPEquipment> level1 = Child.ElementAt(groupPosition);
        CPEquipment level2 = level1.ElementAt(childPosition);
        E e = new E() {Serial = level2.Serial, Model = level2.Model};
        return e;
    }

    public override long GetChildId(int groupPosition, int childPosition)
    {
        return Convert.ToInt32(groupPosition.ToString(CultureInfo.InvariantCulture) + childPosition.ToString(CultureInfo.InvariantCulture));
    }

    public override int GetChildrenCount(int groupPosition)
    {
        return Child.ElementAt(groupPosition).Count;
    }

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);
        }

        E e = (E)GetChild(groupPosition, childPosition);
        TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
        serial.Text = e.Serial;
        TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
        model.Text = e.Model;          

        return convertView;
    }

    public override Object GetGroup(int groupPosition)
    {
        CPEquipment c = Parent.ElementAt(groupPosition);
        E e = new E(){Serial = c.Serial, Model = c.Model, Type = c.Status}; 

        return e;
    }

    public override long GetGroupId(int groupPosition)
    {
        return groupPosition;
    }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        E e = (E)GetGroup(groupPosition);

        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
                convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
     }

        TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
        serial.Text = e.Serial;
        TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
        model.Text = e.Model;
        AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
        acText.Adapter = _adapter;

        _list.ExpandGroup(groupPosition);

        return convertView;
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    public override int GroupCount
    {
        get { return Parent.Count; }
    }

    public override bool HasStableIds
    {
        get { return true; }
    }

}

结果:

在这种情况下,父组中的 AutoCompleteTextView 和绿色加号按钮应该是“可选择的”,以便用户可以在字段中输入信息并单击该按钮而不会折叠组。并且子项中的 CheckBox 和红色 x 按钮也应该是“可选择的”,以便用户可以选中 CheckBox 并单击按钮。唯一真正起作用的部分是 CheckBox 是可选择的并且组不会折叠,因为由于缺少更好的术语,组布局是“死的”并且在按下时什么也不做。而看似“工作”的 CheckBox 甚至不是这样,因为它做了一件奇怪的事情,选中或取消选中一个会随机检查或取消选中其他人。

【问题讨论】:

  • 在你的代码中有 Button 或 checkBox 代码的 sn-p 吗?
  • 是的。请查看包含的 xml。第一个有 ImageButton,第二个有 CheckBox 和 ImageButton。

标签: android xamarin.android


【解决方案1】:

下面的代码是解决复选框问题“CheckBox 甚至不是这样,因为它做了一件奇怪的事情,选中或取消选中一个会随机选中或取消选中其他人。”连同 ImageButton 和 CheckBox 处理。

public override View GetChildView(final int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
    convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);

    E e = (E)GetChild(groupPosition, childPosition);
    TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
    serial.Text = e.Serial;
    TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
    model.Text = e.Model;     

    CheckBox include = (CheckBox)convertView.FindViewById(Resource.Id.include); 

    include.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            //DO your checkbox handling here
        }
    });

    ImageButton removeAttachment =(CheckBox)convertView.FindViewById(Resource.Id. removeAttachment);  

    removeAttachment.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //DO your imageButton handling here
        }
    });

    return convertView;
}

public override View GetGroupView(final int groupPosition, bool isExpanded, View convertview, ViewGroup parent)
{
    View convertView = convertview;
    if (convertView == null) 
    {
        E e = (E)GetGroup(groupPosition);

        LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
        convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
    }

    TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
    serial.Text = e.Serial;
    TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
    model.Text = e.Model;
    AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
    acText.Adapter = _adapter;

    ImageButton addToTransfer =(CheckBox)convertView.FindViewById(Resource.Id. addToTransfer);  

    addToTransfer.setOnClickListener(new OnClickListener() {                        
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //DO your addToTransfer imageButton handling here

        }
    });

    _list.ExpandGroup(groupPosition);

    return convertView;
}

【讨论】:

  • 好消息和坏消息。好消息是,CheckBoxes 和 AutoCompleteTextViews 似乎不再相互镜像。现在,当您滚动或 AutoCompleteTextView 失去焦点或任何值重置为默认值时,坏消息就出现了。 ExpandableListView 中是否有某种事件我需要捕获和处理,还是在视图本身中?
  • 是的,您必须通过持久存储数据来手动处理它,或者您必须维护 textView 文本数组。
  • 除了设置 OnClickListener 之外,您还可以只订阅 removeAttachmentaddToTransfer ImageButtons 的 Click 事件。同样,复选框也有类似的事件。这样可以更轻松地直接访问 GetChildView 和 GetGroupView 中发生的事情。
  • 谢谢。这似乎比仅仅将一些 LinearLayouts 附加到 ScrollView 更痛苦。我知道这不是推荐的方式,但它就像一种魅力。还是谢谢。
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多