在可扩展列表视图中处理包含多个视图的子视图上的单击事件
handling click event on childview containing multiple views in expandable listview
我有像 this 这样的可扩展列表视图。我想在用户点击查看描述时打开另一个 activity。我尝试在我的自定义可扩展列表适配器的 getchildview 方法中设置按钮侦听器,但我知道我无法使用我的适配器 class。我也不能在子点击上使用(据我所知)因为我的子视图中有多个视图。我完全糊涂了....并且已经尝试了一天。我用谷歌搜索了很多但没有找到解决方案。
这是我的可扩展列表的自定义适配器:
package com.qandeelabbasi.homeactivity;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, HashMap<String, String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, HashMap<String, String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public HashMap<String, String> getChild(int groupPosition,
int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition));
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String Ven = getChild(groupPosition, childPosition).get("venue");
final String Dat = getChild(groupPosition, childPosition).get("date");
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout_exview,
parent, false);
}
TextView txtListChild1 = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild1.setText(Ven);
TextView txtListChild2 = (TextView) convertView
.findViewById(R.id.textView1);
txtListChild2.setText(Dat);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parent_layout_exview,
null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的子视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/textVenue1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Venue:"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="5dp"
android:text=""
android:typeface="sans" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/textDate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Date:"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="15dp"
android:text="12/05/2014"
android:typeface="sans" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:weightSum="2" >
<Button
android:id="@+id/buttonJoin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:maxHeight="40dp"
android:minHeight="25dp"
android:text="Join" />
<Button
android:id="@+id/buttonMaybe1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:maxHeight="40dp"
android:minHeight="25dp"
android:text="May be" />
</LinearLayout>
<Button
android:id="@+id/buttonSeeDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/button_style"
android:text="See Description" />
</LinearLayout>
在 getChildView() 中设置 onClickListener 有什么问题?
Button Bt_Name = (Button)convertView.findViewById(R.id.buttonSeeDescription);
Bt_Name.setOnClickListener(new OnClickListener(
@Override
private void Onclick(View view){
Intent intent = new Intent(_context, DestinationActivity.class);
_context.startActivity(intent);
});
我有像 this 这样的可扩展列表视图。我想在用户点击查看描述时打开另一个 activity。我尝试在我的自定义可扩展列表适配器的 getchildview 方法中设置按钮侦听器,但我知道我无法使用我的适配器 class。我也不能在子点击上使用(据我所知)因为我的子视图中有多个视图。我完全糊涂了....并且已经尝试了一天。我用谷歌搜索了很多但没有找到解决方案。
这是我的可扩展列表的自定义适配器:
package com.qandeelabbasi.homeactivity;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, HashMap<String, String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, HashMap<String, String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public HashMap<String, String> getChild(int groupPosition,
int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition));
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String Ven = getChild(groupPosition, childPosition).get("venue");
final String Dat = getChild(groupPosition, childPosition).get("date");
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout_exview,
parent, false);
}
TextView txtListChild1 = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild1.setText(Ven);
TextView txtListChild2 = (TextView) convertView
.findViewById(R.id.textView1);
txtListChild2.setText(Dat);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parent_layout_exview,
null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的子视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/textVenue1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Venue:"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="5dp"
android:text=""
android:typeface="sans" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/textDate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Date:"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="15dp"
android:text="12/05/2014"
android:typeface="sans" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:weightSum="2" >
<Button
android:id="@+id/buttonJoin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:maxHeight="40dp"
android:minHeight="25dp"
android:text="Join" />
<Button
android:id="@+id/buttonMaybe1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:maxHeight="40dp"
android:minHeight="25dp"
android:text="May be" />
</LinearLayout>
<Button
android:id="@+id/buttonSeeDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/button_style"
android:text="See Description" />
</LinearLayout>
在 getChildView() 中设置 onClickListener 有什么问题?
Button Bt_Name = (Button)convertView.findViewById(R.id.buttonSeeDescription);
Bt_Name.setOnClickListener(new OnClickListener(
@Override
private void Onclick(View view){
Intent intent = new Intent(_context, DestinationActivity.class);
_context.startActivity(intent);
});