0

我正在使用 android 中的自定义可扩展列表视图。在我的可扩展列表视图中有线性布局的项目。

你可以在这里找到我的可扩展列表视图。 https://drive.google.com/file/d/0B2NGqFM-F0bNcUZqUlRPaUZYUjg/edit?usp=sharing

在图像中,子项是一个线性布局。我怎样才能获得像主、墙、表和条目项目 ID 这样的单个项目。这样我将执行单独的操作。

你可以在这里找到我的适配器。

package com.newsmartcontrol;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import com.smartcontrol.R;



public class ExpandsAdapter implements ExpandableListAdapter
{
Context context;
LayoutInflater layoutInflater;

public ExpandsAdapter (Context context, LayoutInflater layoutInflater)
{
    this.context = context;
    this.layoutInflater = layoutInflater;
}


@Override
public void registerDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return 4;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View view, ViewGroup parent) 
{
    view = View.inflate(context, R.layout.expands_group, null);
    TextView title_name = (TextView) view.findViewById(R.id.title_name);
    if(groupPosition == 0) {
        title_name.setText("Lights");
    }
    if(groupPosition == 1) {
        title_name.setText("AC-Thermostat");
    }
    if(groupPosition == 2) {
        title_name.setText("Drapery");
    }
    if(groupPosition == 3) {
        title_name.setText("TV");
    }
    view.invalidate();
    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View view, ViewGroup parent) 
{
    if(groupPosition == 0) {
        view = View.inflate(context, R.layout.lights_layout, null);
    }
    if(groupPosition == 1) {
        view = View.inflate(context, R.layout.ac_thermostat_layout, null);
    }
    if(groupPosition == 2) {
        view = View.inflate(context, R.layout.drapery_layout, null);
    }
    if(groupPosition == 3) {
        view = View.inflate(context, R.layout.tv_layout, null);
    }
    view.invalidate();
    return view;

}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean areAllItemsEnabled() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onGroupExpanded(int groupPosition) {
    // TODO Auto-generated method stub

}

@Override
public void onGroupCollapsed(int groupPosition) {
    // TODO Auto-generated method stub

}

@Override
public long getCombinedChildId(long groupId, long childId) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getCombinedGroupId(long groupId) {
    // TODO Auto-generated method stub
    return 0;
}


 }

我的活动就在这里。

 public class ExpandsMainActivity extends Activity
{
ExpandableListView  expandableListView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.expands_main);
    expandableListView = (ExpandableListView)findViewById(R.id.list);
    ExpandsAdapter adapter = new ExpandsAdapter(this, getLayoutInflater());
    expandableListView.setAdapter(adapter);

    }
          }

请告诉我如何在可扩展列表视图中获取子项 ID 的各个项。这样我就可以分别为 main、wall 和其余所有项目编写单击操作。

4

1 回答 1

1

我所做的是使用 onChildClick 参数 childPosition,如下所示:

expandableListView.setOnChildClickListener(new OnChildClickListener(){
    public boolean onChildClick(ExpandableListView  parent, View v,
    int groupPosition, int childPosition, long id)
    {
            int childItemID=childPosition;
    }
});
于 2014-05-28T07:01:23.817 回答