2012-12-28 51 views
1

我使用組件android-section-list(http://code.google.com/p/android-section-list/)在我的listView中創建節。如何在按下某個按鈕後隱藏部分?在列表視圖中隱藏節標籤

我嘗試做這樣

public void onClick(View v) { 

     switch(v.getId()) { 
      case R.id.sortTitle: 
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
        View convertView = inflater.inflate(R.layout.market_list_separator, null, false); 
        TextView separatorLayout = (TextView) convertView.findViewById(R.id.section_view); 
         separatorLayout.setVisibility(View.GONE); 
         sectionAdapter.notifyDataSetChanged(); 
       setButtonState(sortByTitle); 
       adapter.sort((Comparator<SectionListItem>) sortByTitle()); 
       break; 

     } 

    } 

但這種方式不工作。


我的適配器把數據放在列表行和SectionListAdapter繪製節中。

public class SectionListAdapter extends BaseAdapter implements ListAdapter, 
     OnItemClickListener { 
    private final DataSetObserver dataSetObserver = new DataSetObserver() { 
     @Override 
     public void onChanged() { 
      super.onChanged(); 
      updateSessionCache(); 
     } 

     @Override 
     public void onInvalidated() { 
      super.onInvalidated(); 
      updateSessionCache(); 
     }; 
    }; 

    private final ListAdapter linkedAdapter; 
    private final Map<Integer, String> sectionPositions = new LinkedHashMap<Integer, String>(); 
    private final Map<Integer, Integer> itemPositions = new LinkedHashMap<Integer, Integer>(); 
    private final Map<View, String> currentViewSections = new HashMap<View, String>(); 
    private int viewTypeCount; 
    protected final LayoutInflater inflater; 

    private View transparentSectionView; 

    private OnItemClickListener linkedListener; 

    public SectionListAdapter(final LayoutInflater inflater, 
      final ListAdapter linkedAdapter) { 
     this.linkedAdapter = linkedAdapter; 
     this.inflater = inflater; 
     linkedAdapter.registerDataSetObserver(dataSetObserver); 
     updateSessionCache(); 
    } 

    private boolean isTheSame(final String previousSection, 
      final String newSection) { 
     if (previousSection == null) { 
      return newSection == null; 
     } else { 
      return previousSection.equals(newSection); 
     } 
    } 

    private synchronized void updateSessionCache() { 
     int currentPosition = 0; 
     sectionPositions.clear(); 
     itemPositions.clear(); 
     viewTypeCount = linkedAdapter.getViewTypeCount() + 1; 
     String currentSection = null; 
     final int count = linkedAdapter.getCount(); 
     for (int i = 0; i < count; i++) { 
      final SectionListItem item = (SectionListItem) linkedAdapter 
        .getItem(i); 
      if (!isTheSame(currentSection, item.section)) { 
       sectionPositions.put(currentPosition, item.section); 
       currentSection = item.section; 
       currentPosition++; 
      } 
      itemPositions.put(currentPosition, i); 
      currentPosition++; 
     } 
    } 


    public synchronized int getCount() { 
     return sectionPositions.size() + itemPositions.size(); 
    } 


    public synchronized Object getItem(final int position) { 
     if (isSection(position)) { 
      return sectionPositions.get(position); 
     } else { 
      final int linkedItemPosition = getLinkedPosition(position); 
      return linkedAdapter.getItem(linkedItemPosition); 
     } 
    } 

    public synchronized boolean isSection(final int position) { 
     return sectionPositions.containsKey(position); 
    } 

    public synchronized String getSectionName(final int position) { 
     if (isSection(position)) { 
      return sectionPositions.get(position); 
     } else { 
      return null; 
     } 
    } 


    public long getItemId(final int position) { 
     if (isSection(position)) { 
      return sectionPositions.get(position).hashCode(); 
     } else { 
      return linkedAdapter.getItemId(getLinkedPosition(position)); 
     } 
    } 

    protected Integer getLinkedPosition(final int position) { 
     return itemPositions.get(position); 
    } 

    @Override 
    public int getItemViewType(final int position) { 
     if (isSection(position)) { 
      return viewTypeCount - 1; 
     } 
     return linkedAdapter.getItemViewType(getLinkedPosition(position)); 
    } 

    private View getSectionView(final View convertView, final String section) { 
     View theView = convertView; 
     if (theView == null) { 
      theView = createNewSectionView(); 
     } 
     setSectionText(section, theView); 
     replaceSectionViewsInMaps(section, theView); 
     return theView; 
    } 

    protected void setSectionText(final String section, final View sectionView) { 
     final TextView textView = (TextView) sectionView.findViewById(R.id.listTextView); 
     textView.setText(section); 
    } 

    protected synchronized void replaceSectionViewsInMaps(final String section, 
      final View theView) { 
     if (currentViewSections.containsKey(theView)) { 
      currentViewSections.remove(theView); 
     } 
     currentViewSections.put(theView, section); 
    } 

    protected View createNewSectionView() { 
     return inflater.inflate(R.layout.section_view, null); 
    } 


    public View getView(final int position, final View convertView, 
      final ViewGroup parent) { 
     if (isSection(position)) { 
      return getSectionView(convertView, sectionPositions.get(position)); 
     } 
     return linkedAdapter.getView(getLinkedPosition(position), convertView, 
       parent); 
    } 

    @Override 
    public int getViewTypeCount() { 
     return viewTypeCount; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return linkedAdapter.hasStableIds(); 
    } 

    @Override 
    public boolean isEmpty() { 
     return linkedAdapter.isEmpty(); 
    } 

    @Override 
    public void registerDataSetObserver(final DataSetObserver observer) { 
     linkedAdapter.registerDataSetObserver(observer); 
    } 

    @Override 
    public void unregisterDataSetObserver(final DataSetObserver observer) { 
     linkedAdapter.unregisterDataSetObserver(observer); 
    } 

    @Override 
    public boolean areAllItemsEnabled() { 
     return linkedAdapter.areAllItemsEnabled(); 
    } 

    @Override 
    public boolean isEnabled(final int position) { 
     if (isSection(position)) { 
      return true; 
     } 
     return linkedAdapter.isEnabled(getLinkedPosition(position)); 
    } 

    public void makeSectionInvisibleIfFirstInList(final int firstVisibleItem) { 
     final String section = getSectionName(firstVisibleItem); 
     // only make invisible the first section with that name in case there 
     // are more with the same name 
     boolean alreadySetFirstSectionIvisible = false; 
     for (final Entry<View, String> itemView : currentViewSections 
       .entrySet()) { 
      if (itemView.getValue().equals(section) 
        && !alreadySetFirstSectionIvisible) { 
       itemView.getKey().setVisibility(View.INVISIBLE); 
       alreadySetFirstSectionIvisible = true; 
      } else { 
       itemView.getKey().setVisibility(View.VISIBLE); 
      } 
     } 
     for (final Entry<Integer, String> entry : sectionPositions.entrySet()) { 
      if (entry.getKey() > firstVisibleItem + 1) { 
       break; 
      } 
      setSectionText(entry.getValue(), getTransparentSectionView()); 
     } 
    } 

    public synchronized View getTransparentSectionView() { 
     if (transparentSectionView == null) { 
      transparentSectionView = createNewSectionView(); 
     } 
     return transparentSectionView; 
    } 

    protected void sectionClicked(final String section) { 
     // 
    } 


    public void onItemClick(final AdapterView<?> parent, final View view, 
      final int position, final long id) { 
     if (isSection(position)) { 
      sectionClicked(getSectionName(position)); 
     } else if (linkedListener != null) { 
      linkedListener.onItemClick(parent, view, 
        getLinkedPosition(position), id); 
     } 
    } 

    public void setOnItemClickListener(final OnItemClickListener linkedListener) { 
     this.linkedListener = linkedListener; 
    } 
} 

我在哪裏可以檢查此適配器的可見性?

+0

從'sectionPositions'集合中刪除應該隱藏的部分,並在適配器中調用'notifyDataSetChanged'方法。你的適配器也很複雜,我以更簡單的方式顯示部分:https://github.com/vortexwolf/2ch-Browser/blob/development/src/com/vortexwolf/dvach/adapters/BoardsListAdapter.java – vorrtex

回答

0

這有效,但將被適配器中的getView()方法覆蓋。您必須在適配器bcoz中處理此操作,每次視圖在屏幕上可見時,視圖更新以便在您的案例中可見。

對於要存儲的數據,添加可見性字段(默認情況下保持可見),並將可見性更改爲onClick()方法中的GONE。

如果您不明白,請將您的代碼發佈給我,並讓您知道要做出哪些更改。

+0

它必須在我的適配器或組件SectionListAdapter? – wa1demar

+0

我真的不能說更少的細節,因爲我不知道你的適配器的細節。但是無論哪個適配器添加這些部分都應該完成這項工作。 – preeya