如何手动展开和折叠可展开的ListView?我知道expandgroup(),但不确定在哪里设置onclicklistener(),因为这段代码的一半是在单独的库项目中。
可扩展的交付列表

package com.goosesys.dta_pta_test;
[imports removed to save space]

public class ExpandableDeliveryList<T> extends ExpandableListActivity {

private ArrayList<GooseDeliveryItem> parentItems = new ArrayList<GooseDeliveryItem>();
private ArrayList<DeliverySiteExtras> childItems = new ArrayList<DeliverySiteExtras>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // CREATE THE EXPANDABLE LIST AND SET PROPERTIES //
    final ExpandableListView expandList = getExpandableListView();
    expandList.setDividerHeight(0);
    expandList.setGroupIndicator(null);
    expandList.setClickable(false);

    // LIST OF PARENTS //
    setGroupParents();

    // CHILDREN //
    setChildData();

    // CREATE ADAPTER //
    GooseExpandableArrayAdapter<?> adapter = new GooseExpandableArrayAdapter<Object>(
            R.layout.goose_delivery_item,
            R.layout.goose_delivery_item_child,
            parentItems,
            childItems);
    adapter.setInflater((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    expandList.setAdapter(adapter);
    expandList.setOnChildClickListener(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case android.R.id.home:
        {
            Intent intent = new Intent(this, Main.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;
        }

        default:
        {
            return super.onOptionsItemSelected(item);
        }
    }
}

public void setGroupParents()
{
    DatabaseHelper dbHelper = new DatabaseHelper(this);
    List<DeliverySite> sites = new ArrayList<DeliverySite>();
    sites = dbHelper.getAllSites();

    GooseDeliveryItem[] deliveries = new GooseDeliveryItem[sites.size()];

    for(int i=0; i<sites.size(); i++)
    {
        Delivery del = new Delivery();

        try
        {
            del = dbHelper.getDeliveryByJobNo(sites.get(i).id);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        final GooseDeliveryItem gdi;

        if((Double.isNaN(sites.get(i).lat)) || (Double.isNaN(sites.get(i).lng)))
        {
            gdi = new GooseDeliveryItem(sites.get(i).id, sites.get(i).company);
        }
        else
        {
            gdi = new GooseDeliveryItem(sites.get(i).id, sites.get(i).company, sites.get(i).lat, sites.get(i).lng);
        }

        if(del.getReportedFully() == 1)
        {
            gdi.isReportedFully = true;
        }

        deliveries[i] = gdi;
    }

    // FINALLY ADD THESE ITEMS TO THE PARENT ITEMS LIST ARRAY //
    for(GooseDeliveryItem g : deliveries)
        parentItems.add(g);
}

public void setChildData()
{
    //DatabaseHelper dbHelper = new DatabaseHelper(this);
    ArrayList<DeliverySiteExtras> extras = new ArrayList<DeliverySiteExtras>();

    for(int i=0; i<parentItems.size(); i++)
    {
        DeliverySiteExtras dse = new DeliverySiteExtras();
        extras.add(dse);
    }

    childItems = extras;
}
}

定制
package com.goosesys.gooselib.Views;

[imports removed to save space]

public class GooseExpandableArrayAdapter<Object> extends BaseExpandableListAdapter
{
private Activity activity;
private ArrayList<DeliverySiteExtras> childItems;
private LayoutInflater inflater;
ArrayList<GooseDeliveryItem> parentItems;
private DeliverySiteExtras child;
private int layoutId;
private int childLayoutId;

public GooseExpandableArrayAdapter(int layoutId, int childLayoutId, ArrayList<GooseDeliveryItem> parents, ArrayList<DeliverySiteExtras> children)
{
    this.layoutId = layoutId;
    this.childLayoutId = childLayoutId;
    this.parentItems = (ArrayList<GooseDeliveryItem>) parents;
    this.childItems = (ArrayList<DeliverySiteExtras>)children;
}

public GooseExpandableArrayAdapter(ArrayList<GooseDeliveryItem> parents, ArrayList<DeliverySiteExtras> children, int layoutId)
{
    this.parentItems = parents;
    this.childItems = children;
    this.layoutId = layoutId;
}

public void setInflater(LayoutInflater inflater, Activity activity)
{
    this.inflater = inflater;
    this.activity = activity;
}

@Override
public Object getChild(int arg0, int arg1)
{
    return null;
}

@Override
public long getChildId(int arg0, int arg1)
{
    return 0;
}

/*
 * Child view get method
 * Utilise this to edit view properties at run time
 */
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    child = childItems.get(groupPosition);

    if(convertView == null)
    {
        convertView = inflater.inflate(this.childLayoutId, null);
    }

    // GET ALL THE OBJECT VIEWS AND SET THEM HERE //
    setGeoLocation(groupPosition, convertView);

    convertView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        }
    });

    return convertView;
}

@Override
public void onGroupCollapsed(int groupPosition)
{
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition)
{
    super.onGroupExpanded(groupPosition);
}

@Override
public int getChildrenCount(int groupPosition)
{
    return 1; //childItems.get(groupPosition);
}

@Override
public Object getGroup(int groupPosition)
{
    return null;
}

@Override
public int getGroupCount()
{
    return parentItems.size();
}

@Override
public long getGroupId(int arg0)
{
    return 0;
}

/*
 * Parent View Object get method
 * Utilise this to edit view properties at run time.
 */
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    if(convertView == null)
    {
        convertView = inflater.inflate(this.layoutId, null);
    }

    // GET ALL OBJECT VIEWS AND SET THEM HERE -- PARENT VIEW //
    TextView name = (TextView)convertView.findViewById(R.id.customerName);
    name.setText(parentItems.get(groupPosition).customerText);

    ImageView go = (ImageView)convertView.findViewById(R.id.moreDetails);
    go.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(activity, DeliveryJobActivity.class);
            i.putExtra("obj", parentItems.get(groupPosition));
            activity.startActivity(i);
        }
    });

    return convertView;
}

@Override
public boolean hasStableIds()
{
    return false;
}

@Override
public boolean isChildSelectable(int arg0, int arg1)
{
    return false;
}

private void setGeoLocation(final int groupPosition, View parent)
{
    GeoLocation geoLocation = new GeoLocation(activity);
    final double lat = geoLocation.getLatitude();
    final double lng = geoLocation.getLongitude();

    // GET OUR START LOCATION //
    Location startLocation = new Location("Start");
    startLocation.setLatitude(lat);
    startLocation.setLongitude(lng);

    // GET OUR DESTINATION //
    Location destination = new Location("End");
    destination.setLatitude(((GooseDeliveryItem)parentItems.get(groupPosition)).latitude);
    destination.setLongitude(((GooseDeliveryItem)parentItems.get(groupPosition)).longitude);

    double distanceValue = startLocation.distanceTo(destination);

    TextView tv = (TextView)parent.findViewById(R.id.extraHeader);
    tv.setText(parentItems.get(groupPosition).customerText + " information:");

    TextView ds = (TextView)parent.findViewById(R.id.deliveryDistance);
    ds.setText("Distance (from location): " + String.valueOf(Math.ceil(distanceValue * GooseConsts.METERS_TO_A_MILE)) + " Mi (approx)");

    ImageView img = (ImageView)parent.findViewById(R.id.directionsImage);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // invoke google maps with lat / lng position
            Intent navigation = new Intent(Intent.ACTION_VIEW, Uri.parse(
                    "http://maps.google.com/maps?saddr="
                    + (lat) + "," + (lng)
                    + "&daddr="
                    + ((GooseDeliveryItem)parentItems.get(groupPosition)).latitude + ","
                    + ((GooseDeliveryItem)parentItems.get(groupPosition)).longitude
                    ));
            activity.startActivity(navigation);
        }
    });
}
}

理想情况下,我的老板希望有一个“+”按钮,单击该按钮可以手动展开列表视图。而不是在视图上的任何地方单击并自动执行。这可能吗?此外,设置setClickable(false)似乎没有效果。因为当单击任何列表项时,它仍将展开。我是不是也错过了什么?
干杯。

最佳答案

可以添加ExpandableListView组单击侦听器(“ExpandableListView::SetOnGroupClickListener”)来监视和抑制ListView组的单击事件。使用代码示例,这将在创建expandablelistview之后在“expandabledeliverylist”模块中完成。
然后在“+”(和“-”)按钮单击处理程序中,可以使用“expandablelistview::expandgroup()”和“expandablelistview::collapsegroup()”方法添加逻辑以展开/折叠部分或全部列表视图组。
不需要从重写的“isChildSelectable()”方法返回“false”,因为这对您试图完成的任务没有影响(并且将阻止任何人单击/选择ListView中的子项)。
代码示例如下所示:

// CREATE THE EXPANDABLE LIST AND SET PROPERTIES //
final ExpandableListView expandList = getExpandableListView();
//...

expandList.setOnGroupClickListener(new android.widget.ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(    ExpandableListView  parent,
                                    View                view,
                                    int                 groupPosition,
                                    long                id) {

        //  some code...

        //  return "true" to consume the event (and prevent the Group from expanding/collapsing) / "false" to allow the Group to expand/collapse normally
        return true;
    }
});

要手动展开和折叠ListView组,请执行以下操作:
//  enumerate thru the ExpandableListView Groups
//  [in this code example, a single index is used...]
int groupPosition = 0;

//  expand the ListView Group/s
if (m_expandableListView.isGroupExpanded(groupPosition) == false) {

    //  API Level 14+ allows you to animate the Group expansion...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        m_expandableListView.expandGroup(groupPosition, true);
    }

    //  else just expand the Group without animation
    else {
        m_expandableListView.expandGroup(groupPosition);
    }
}

//  collapse the ListView Group/s
else {
    m_expandableListView.collapseGroup(groupPosition);
}

希望这有帮助。

关于android - 覆盖默认的expandablelistview展开行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20543695/

10-12 00:22
查看更多