2012-07-31 50 views
0

我创建了ExpandableListView搜索活动,其中显示3组 - 曲目,艺术家,专辑。 当用户输入EditText中的文本时,相应的搜索结果显示在ExpandableListView中。Android:ExpandableListView ChildClickListener无法在列表滚动后工作

问题: 当用户点击任何子项时,我想执行一些操作。起初这个功能工作正常,但是当我向上或向下滚动列表时,OnChildClickListener停止工作。没有孩子被点击选择。

这是视图更新&位置的问题吗?

注:我使用的是BaseExpandableListAdapter

下面是代码:(我贴什么,我认为是必要的PLZ让我知道,如果需要其他的部分。):

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.search); 

      etSearchBar = (EditText) findViewById(R.id.etSearchBar); 
      etSearchBar.addTextChangedListener(this); 

      lvExpSearchResult = (ExpandableListView) findViewById(R.id.lvExpSearchResult); 
      lvExpSearchResult.setGroupIndicator(null); 
      lvExpSearchResult.setChildIndicator(null); 
      lvExpSearchResult.setOnTouchListener(this); 
      lvExpSearchResult.setOnChildClickListener(this); 
     } 

    @Override 
     public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, 
       int childPosition, long id) { 
      lvExpSearchResult.smoothScrollToPosition(groupPosition); 
      Toast.makeText(SearchActivity.this, group[groupPosition], Toast.LENGTH_SHORT).show(); 
      //Toast.makeText(SearchActivity.this, children[groupPosition][childPosition], Toast.LENGTH_LONG).show(); 
      return true; 
     } 

@Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if (s.length() > 0) { 
      group = new String[] { "Tracks", "Albums", "Artists" }; 
      ArrayList<String> result = null; 

      result = getQueryResult(MediaStore.Audio.Media.TITLE); 
      result = new Utilities().removeDuplicatesAL(result); 
      String[] tracks = result.toArray(new String[result.size()]); 

      result = getQueryResult(MediaStore.Audio.Media.ALBUM); 
      result = new Utilities().removeDuplicatesAL(result); 
      String[] albums = result.toArray(new String[result.size()]); 

      result = getQueryResult(MediaStore.Audio.Media.ARTIST); 
      result = new Utilities().removeDuplicatesAL(result); 
      String[] artists = result.toArray(new String[result.size()]); 

      children = new String[][] { tracks, albums, artists }; 

      adapter = new SearchAdapter(this, group, children); 
     } else { 
      adapter = new SearchAdapter(this, new String[0], new String[0][0]); 
     } 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     lvExpSearchResult.setAdapter(adapter); 
    } 

// BaseExpandableListAdapter code - getGroupView & getChildView 
@Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     TextView childRow = (TextView) convertView; 
     if (childRow == null) { 
      LayoutInflater inflator = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      try { 
       childRow = (TextView) inflator.inflate(
         android.R.layout.simple_expandable_list_item_1, null); 
      } catch (Exception e) { 
       Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
     childRow.setText(children[groupPosition][childPosition]); 
     return childRow; 
    } 

@Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     TextView headerRow = (TextView) convertView; 
     if (headerRow == null) { 
      LayoutInflater inflator = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      headerRow = (TextView) inflator.inflate(
        android.R.layout.simple_expandable_list_item_1, null); 
     } 
     headerRow.setTypeface(Typeface.DEFAULT_BOLD); 
     headerRow.setText(group[groupPosition]); 

     ExpandableListView eLV = (ExpandableListView)parent; 
     eLV.expandGroup(groupPosition); 

     return headerRow; 
    } 
+0

当你在EditText中输入文本会发生什么?也许你每次更改文本时都会初始化你的列表?尝试使用自定义的ExpandableList并覆盖getChildView并在其上设置点击监听器。 – 2012-07-31 09:02:54

+0

是的,每次用户键入一个新列表时,都会传递给一个自定义的'BaseExpandListListAdapter'并且结果会被更新。我在'EditText'上添加了一个'addTextChangeListener',它调用适配器并显示结果。代码已更新。 – reiley 2012-07-31 09:20:03

+1

尝试在你的适配器的getChildView方法的childView上设置onClickListener,像这样https://github.com/gobozov/myshows/blob/master/src/ru/myshows/fragments/EpisodesFragment.java – 2012-07-31 09:27:23

回答

2

我认为你的孩子的方法问题,只是这个代码实现并试用这个,

@Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return ; 
     // false = default and do not get click 
     // true = get the focus of child and got the click 
    }