java - 本例如何实现onchildclick监听?

标签 java android listview clickable

所以我有一个可扩展的 ListView 。我想要的是让每个 child 都可以点击。如果我按第一个我想打开class1,如果我按第二个我想打开class2,如果我按第三个我想打开class3等等......我是编程新手所以请像对待傻瓜一样向我解释。

这是我的 Activity

public class Mecanica  extends ExpandableListActivity implements OnChildClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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("Directia");
        groupItem.add("Franarea");
        groupItem.add("Motorul");
        groupItem.add("Rotile");
        groupItem.add("Siguranta si control");
        groupItem.add("Suspensia");
        groupItem.add("Transmisia");
    }

    ArrayList<String> groupItem = new ArrayList<String>();
    ArrayList<Object> childItem = new ArrayList<Object>();

    public void setChildGroupData() {
        /**
         * Add Data For TecthNology
         */
        ArrayList<String> child = new ArrayList<String>();
        child.add("Java");
        child.add("Drupal");
        child.add(".Net Framework");
        child.add("PHP");
        childItem.add(child);

        /**
         * Add Data For Mobile
         */
        child = new ArrayList<String>();
        child.add("Android");
        child.add("Window Mobile");
        child.add("iPHone");
        child.add("Blackberry");
        childItem.add(child);
        /**
         * Add Data For Manufacture
         */
        child = new ArrayList<String>();
        child.add("HTC");
        child.add("Apple");
        child.add("Samsung");
        child.add("Nokia");
        childItem.add(child);
        /**
         * Add Data For Extras
         */
        child = new ArrayList<String>();
        child.add("Contact Us");
        child.add("About Us");
        child.add("Location");
        child.add("Root Cause");
        childItem.add(child);
    }    
}

和适配器类

@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 true;
    }
}

最佳答案

我找不到你应该实现的 OnChildClickListener

这将使您能够访问被点击的特定子项,从而访问您的项目列表中的位置。

实现可能看起来像这个例子:

@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, Child0Activity.class);
        startActivity(child0Intent);
        break;
    case 1:
        // Go to child #1 specific class.
        Intent child1Intent = new Intent(this, Child1Activity.class);
        startActivity(child1Intent);
        break;
    }
    return false;
}

可以在此处找到有关如何实现它的完整示例:

http://www.mysamplecode.com/2012/10/android-expandablelistview-example.html

关于java - 本例如何实现onchildclick监听?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989782/

相关文章:

java - 使用 Servlets/JSP 的好的开源 Web 应用程序?

android - Spritesheet 以编程方式切割 : best practices

listview - Flutter/Dart : Handling Dropdown changes within a ListView. 生成器,下拉按钮未更新,

.net - 在 asp.net 3.5 中将 List<string> 绑定(bind)到 Listview

android - OverlayItem 中的可点击列表(适用于 Android 的 MapView)

java - 谷歌开发人员地理图表和 DAO 对象

java - 在java中将日期转换为时间戳

java - Android在Java中超时重复任务

java - 如何在另一个方法中调用onReceive方法?

java - 为一个 servlet 配置 web.xml (Tomcat 5) 以处理所有传入请求?