如何以编程方式调用setOnChildClickListener的onChildClick方法?

时间:2014-05-22 10:01:02

标签: android android-listview expandablelistview onclicklistener expandablelistadapter

我正在构建一个可扩展的列表视图。

一旦用户对子项进行选择,它就会重定向到webview。当用户返回列表视图页面时,我想保留选择。

以下是我正在使用的代码:

final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(this, groupList, laptopCollection);
expListView.setAdapter(expListAdapter);

expListView.setOnChildClickListener(new OnChildClickListener() 
{
     public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) 
     {

         final String selected = (String) expListAdapter.getChild(groupPosition, childPosition);

         LinearLayout llChildItem = (LinearLayout) v.findViewById(R.id.llChildItem);
         llChildItem.setBackgroundResource(R.drawable.nav_selected);

         Intent intent = new Intent(getApplicationContext(),MyWebView.class);
         intent.putExtra("url","http://www.google.com");
         intent.putExtra("groupPosition", groupPosition);
         intent.putExtra("childPosition", childPosition);
         startActivity(intent);

         return true;
     }
});

我正在通过groupPosition& childPosition通过意图。有没有办法可以通过编程方式调用onChildClick方法并传递这两个参数,以便保持所做的选择?

1 个答案:

答案 0 :(得分:0)

您可以使用startActivityForResult(intent,200);代替startActivity(intent); 启动webview.And从webview活动中,您可以将这些参数作为

发送回选择活动
 Intent data = new Intent();
           intent.putExtra("groupPosition", groupPosition);
             intent.putExtra("childPosition", childPosition);
                setResult(RESULT_OK, data);

在选择活动中,您可以将这些参数用作

string groupPosition ,childPosition ; //activity global parameters


@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

             if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
    groupPosition = data.getStringExtra("groupPosition");
    childPosition = data.getStringExtra("childPosition");
    }
    }

根据这些参数,您可以进行选择。

并将您的onChildClick方法覆盖为

//关于子点击监听器的Listview

expListView.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {

                    return true;
                }
            });