我在android中使用自定义可扩展列表视图。在我的可扩展列表视图中,具有线性布局的项目。
您可以在此处找到我的可扩展列表视图。 https://drive.google.com/file/d/0B2NGqFM-F0bNcUZqUlRPaUZYUjg/edit?usp=sharing
在图像中,子项是一个linearlayout。如何获得主要项目,墙壁,桌子和入口项目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);
}
}
请告诉我如何在expandable listview中获取子子项id的单个项目。这样我就会分别为main,wall和剩余的所有项目编写点击操作。
答案 0 :(得分:1)
我所做的是使用onChildClick参数childPosition,如下所示:
expandableListView.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
{
int childItemID=childPosition;
}
});