我正在尝试使用可扩展的列表视图制作一个简单的媒体播放器应用程序,以显示歌曲名称并将标题作为其文件夹名称。问题是当应用运行时,我打开一个可扩展列表,只有第一个子视图有效,而其他子视图无效。
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
final int a = i;
final int b = i1;
TextView txtListChild = (TextView) expandableListView.findViewById(R.id.lblListItem);
ImageView img = (ImageView)expandableListView.findViewById(R.id.img);
txtListChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Starting Music", Toast.LENGTH_SHORT).show();
try {
String s = mapping.get(headers.get(a));
String path = s+"/"+inner.get(headers.get(a)).get(b)+".mp3";
Uri myUri = Uri.parse(path);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (Exception e){
Log.i("Exception",e.toString());
}
}
});
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Stopping Music", Toast.LENGTH_SHORT).show();
if(mediaPlayer!=null){
mediaPlayer.stop();
}
}
});
return false;
}
});
这是我将onChild点击侦听器添加到我的主要活动中的代码,并且我的子视图具有TextView
和ImageView
。只有位于位置0的孩子可以工作,其余的则不能。
如果需要其他任何代码段,请在注释中提及。
这是子对象的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:padding="5dp"
android:layout_margin="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Song Name"
android:textSize="17sp"
android:layout_margin="5dp"
android:padding="5dp"/>
<ImageView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="@+id/img"
android:layout_margin="5dp"
android:src="@drawable/ic_action_name"
android:layout_alignParentRight="true"/>
</LinearLayout>
编辑: 适配器的代码->
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, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,//ArrayList<Pojo> list,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return this._listDataChild.get(this._listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return this._listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return this._listDataChild.get(this._listDataHeader.get(i)).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@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.heading_expandable, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_expandable, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
ImageView img = (ImageView)convertView.findViewById(R.id.img);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}