gpt4 book ai didi

java - 可扩展 ListView 上子项的 Intent

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:48 25 4
gpt4 key购买 nike

你好,我想在我的可扩展 ListView 上有一个 Intent ..但是每次我点击一个 child ..它只是显示一个 TOAST.. 并且不符合我的 Intent .. 我的 Intent 是代码的最后部分.. 非常感谢任何帮助..

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 设置为可点击 false 或者您可以将 >TextView 的 宽度较小,因此用户可以在项目基础 View 上按任意一个。

关于java - 可扩展 ListView 上子项的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com