我正在使用ExpandableListView。我在视图中有四个项目。这是列表父行的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/saved_tank_mixes_parent_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="@dimen/layout_margin" >
<com.lht.textview.LHTTextView
android:id="@+id/saved_mix_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/brown_for_text"
android:textSize="16sp"
app:fontFamily="helveticaltstdlight.ttf" />
<com.lht.textview.LHTTextView
android:id="@+id/saved_mix_time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/saved_mix_name_textview"
android:layout_marginLeft="1dp"
android:text="Saved 15 mins ago"
android:textColor="@color/gray_extra_lite"
android:textSize="12sp"
app:fontFamily="helveticaltstdlightcond.ttf" />
<ImageView
android:id="@+id/detail_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/detail_button"
android:contentDescription="@string/app_name" />
<Button
android:id="@+id/cross_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:onClick="onDeleteSavedMixClick"
android:background="@drawable/cross_button_selector"
android:visibility="gone" />
</RelativeLayout>
最后一个按钮(此处为十字按钮)用于从列表视图中删除项目。
当我点击这个十字按钮时,我想得到列表行的位置。我尝试实现onGroupClickListener来获取位置,但它不起作用。请帮帮我。
以下是我的自定义适配器的代码。
package com.lht.precisionlabs.mixtank.savedrecipe;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lht.precisionlabs.mixtank.R;
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
private Context mContext;
private ArrayList<HashMap<String, Object>> listViewDataItem;
private boolean isDeleteButtonVisible=false;
public MyExpandableListAdapter(Context context,ArrayList<HashMap<String, Object>> listViewDataSource)
{
this.mContext=context;
this.listViewDataItem=listViewDataSource;
}
public void setDeleteButtonVisibility(boolean isVisible)
{
this.isDeleteButtonVisible=isVisible;
}
public ArrayList<HashMap<String, Object>> getDataSource()
{
return listViewDataItem;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = inflater.inflate(R.layout.saved_tank_mix_child_row_layout, parent,false);
}
return convertView;
}
@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 listViewDataItem.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return listViewDataItem.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = inflater.inflate(R.layout.saved_tank_mixes_base_row,parent,false);
}
setDataOnView(convertView,groupPosition,isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
private void setDataOnView(View view,int groupPosition,boolean isExpanded)
{
RelativeLayout relativeLayout=(RelativeLayout)view;
TextView textView=(TextView)relativeLayout.findViewById(R.id.saved_mix_name_textview);
textView.setText((String)(listViewDataItem.get(groupPosition).get(mContext.getResources().getString(R.string.mix_name_key))));
textView=(TextView)relativeLayout.findViewById(R.id.saved_mix_time_textview);
textView.setText((String)(listViewDataItem.get(groupPosition).get(mContext.getResources().getString(R.string.mix_date_key))));
ImageView detailIcon=(ImageView)relativeLayout.findViewById(R.id.detail_icon);
Button deleteButton=(Button)relativeLayout.findViewById(R.id.cross_button);
if(isDeleteButtonVisible)
{
deleteButton.setVisibility(View.VISIBLE);
detailIcon.setVisibility(View.GONE);
}
else
{
deleteButton.setVisibility(View.GONE);
if(isExpanded)
detailIcon.setVisibility(View.GONE);
else
detailIcon.setVisibility(View.VISIBLE);
}
}
}