您好,我想要一个可扩展列表视图的意图..但是每次我单击一个孩子时..
它只是显示一个TOAST ..而没有按照我的意图进行..我的意图在代码的最后一部分..任何帮助都将受到高度赞赏。
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
public class LessonsMenu extends ExpandableListActivity implements
OnChildClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ExpandableListView expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
setGroupData();
setChildGroupData();
NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
mNewAdapter
.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(mNewAdapter);
expandbleLis.setOnChildClickListener(this);
}
public void setGroupData() {
groupItem.add("Audio");
groupItem.add("Camera");
groupItem.add("External Storage");
groupItem.add("Input");
groupItem.add("Security");
groupItem.add("Sensors");
groupItem.add("Dalvik");
groupItem.add("Bluetooth");
groupItem.add("DRM");
groupItem.add("Encryption");
groupItem.add("Graphics");
groupItem.add("Media");
groupItem.add("Kernel");
groupItem.add("Power");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
ArrayList<String> child = new ArrayList<String>();
child.add("Implementation");
child.add("Latency Measure");
child.add("Design");
childItem.add(child);
child = new ArrayList<String>();
child.add("Camera HAL3");
child.add("HAL Subsystem");
child.add("Metadata and Controls");
child.add("3A Modes and State");
child.add("Output and Cropping");
child.add("Errors and Streams");
child.add("Request Creation");
childItem.add(child);
child = new ArrayList<String>();
child.add("Device Specific Configuration");
child.add("Typical Configuration Examples");
childItem.add(child);
child = new ArrayList<String>();
child.add("Key Layout Files");
child.add("Key Character Maps Files");
child.add("Input Device Configurations Files");
child.add("Migration Guide");
child.add("Keyboard Devices");
child.add("Touch Devices");
child.add("Dumpsys");
child.add("Getevent");
child.add("Validate Keymaps");
childItem.add(child);
child = new ArrayList<String>();
child.add("dm-verity on boot");
child.add("Security-Enchanced Linux");
childItem.add(child);
child = new ArrayList<String>();
child.add("Base Sensors");
child.add("Composite Sensors");
child.add("Batching Results");
childItem.add(child);
child = new ArrayList<String>();
child.add("Bytecode Format");
child.add(".Dex Format");
child.add("Instruction Formats");
child.add("Introducing ART");
childItem.add(child);
child = new ArrayList<String>();
child.add("Bluetooth");
childItem.add(child);
child = new ArrayList<String>();
child.add("Digital Rights Management");
childItem.add(child);
child = new ArrayList<String>();
child.add("Encryption");
childItem.add(child);
child = new ArrayList<String>();
child.add("Graphics");
childItem.add(child);
child = new ArrayList<String>();
child.add("Media");
childItem.add(child);
child = new ArrayList<String>();
child.add("Kernel");
childItem.add(child);
child = new ArrayList<String>();
child.add("Power");
childItem.add(child);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// Create a switch that switches on the specific child position.
switch(childPosition) {
case 0:
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, AudioImplementation.class);
startActivity(child0Intent);
break;
case 1:
// Go to child #1 specific class.
Intent child1Intent = new Intent(this, AudioLatency.class);
startActivity(child1Intent);
break;
}
return false;
}
}
NewAdapter.java
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("unchecked")
public class NewAdapter extends BaseExpandableListAdapter {
public ArrayList<String> groupItem, tempChild;
public ArrayList<Object> Childtem = new ArrayList<Object>();
public LayoutInflater minflater;
public Activity activity;
public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
groupItem = grList;
this.Childtem = childItem;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
tempChild = (ArrayList<String>) Childtem.get(groupPosition);
TextView text = null;
if (convertView == null) {
convertView = minflater.inflate(R.layout.childrow, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(tempChild.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, tempChild.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) Childtem.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return groupItem.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.grouprow, null);
}
((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
childrow.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="false"
android:orientation="vertical"
android:background="#FFFFFF"
android:paddingLeft="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="39dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/childImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:background="@drawable/ic_launcher"
android:contentDescription="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="@string/hello_world"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/white" />
</LinearLayout>
最佳答案
您遇到的问题是,项目内部有可单击的组件。
因此,除非您触摸项目中不包含带有点击监听器的组件的部分(例如,在TextView
之外),否则不会调用子点击监听器。
因此,如果要使用onChildClick
回调,则应将TextView
设置为clickable false,或者可以将TextView's
宽度减小,以便用户可以在项目基础View
上按任意一个。