2012-05-16 18 views
9

में पिन किए गए समूह समूह के आइटम स्क्रॉल करते समय समूह आइटम को स्क्रीन के शीर्ष पर पिन करने का एक मानक तरीका है। मैंने ListView के साथ समान उदाहरण देखा। मुझे किन इंटरफेस को लागू करना चाहिए या किस तरीके से ओवरराइड करना चाहिए?ExpandableListView

उत्तर

15

में मैं पर पीटर Kuterna और एंड्रॉयड नमूना ExpandableList1.java की Pinned Header ListView आधारित समाधान मिल गया। PinnedHeaderExpListView.java

package com.example; 

import com.example.ExpandableList.MyExpandableListAdapter; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.TextView; 

/** 
* A ListView that maintains a header pinned at the top of the list. The 
* pinned header can be pushed up and dissolved as needed. 
*/ 
public class PinnedHeaderExpListView extends ExpandableListView{ 

    /** 
    * Adapter interface. The list adapter must implement this interface. 
    */ 
    public interface PinnedHeaderAdapter { 

     /** 
     * Pinned header state: don't show the header. 
     */ 
     public static final int PINNED_HEADER_GONE = 0; 

     /** 
     * Pinned header state: show the header at the top of the list. 
     */ 
     public static final int PINNED_HEADER_VISIBLE = 1; 

     /** 
     * Pinned header state: show the header. If the header extends beyond 
     * the bottom of the first shown element, push it up and clip. 
     */ 
     public static final int PINNED_HEADER_PUSHED_UP = 2; 

     /** 
     * Configures the pinned header view to match the first visible list item. 
     * 
     * @param header pinned header view. 
     * @param position position of the first visible list item. 
     * @param alpha fading of the header view, between 0 and 255. 
     */ 
     void configurePinnedHeader(View header, int position, int alpha); 
    } 

    private static final int MAX_ALPHA = 255; 

    private MyExpandableListAdapter mAdapter; 
    private View mHeaderView; 
    private boolean mHeaderViewVisible; 

    private int mHeaderViewWidth; 

    private int mHeaderViewHeight; 

    public PinnedHeaderExpListView(Context context) { 
     super(context); 
    } 

    public PinnedHeaderExpListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public PinnedHeaderExpListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setPinnedHeaderView(View view) { 
     mHeaderView = view; 

     // Disable vertical fading when the pinned header is present 
     // TODO change ListView to allow separate measures for top and bottom fading edge; 
     // in this particular case we would like to disable the top, but not the bottom edge. 
     if (mHeaderView != null) { 
      setFadingEdgeLength(0); 
     } 
     requestLayout(); 
    } 

    @Override 
    public void setAdapter(ExpandableListAdapter adapter) { 
     super.setAdapter(adapter); 
     mAdapter = (MyExpandableListAdapter)adapter; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     if (mHeaderView != null) { 
      measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); 
      mHeaderViewWidth = mHeaderView.getMeasuredWidth(); 
      mHeaderViewHeight = mHeaderView.getMeasuredHeight(); 
     } 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     if (mHeaderView != null) { 
      mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); 
      configureHeaderView(getFirstVisiblePosition()); 
     } 
    } 

    /** 
    * animating header pushing 
    * @param position 
    */ 
    public void configureHeaderView(int position) { 
     final int group = getPackedPositionGroup(getExpandableListPosition(position)); 
     int groupView = getFlatListPosition(getPackedPositionForGroup(group)); 

     if (mHeaderView == null) { 
      return; 
     } 

     mHeaderView.setOnClickListener(new OnClickListener() { 

      public void onClick(View header) { 
       if(!expandGroup(group)) collapseGroup(group); 
      } 
     }); 

     int state,nextSectionPosition = getFlatListPosition(getPackedPositionForGroup(group+1)); 

     if (mAdapter.getGroupCount()== 0) { 
      state = PinnedHeaderAdapter.PINNED_HEADER_GONE; 
     }else if (position < 0) { 
      state = PinnedHeaderAdapter.PINNED_HEADER_GONE; 
     }else if (nextSectionPosition != -1 && position == nextSectionPosition - 1) { 
      state=PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP; 
     }else state=PinnedHeaderAdapter.PINNED_HEADER_VISIBLE; 

     switch (state) {  
      case PinnedHeaderAdapter.PINNED_HEADER_GONE: { 
       mHeaderViewVisible = false; 
       break; 
      } 

      case PinnedHeaderAdapter.PINNED_HEADER_VISIBLE: { 
       mAdapter.configurePinnedHeader(mHeaderView, group, MAX_ALPHA); 
       if (mHeaderView.getTop() != 0) { 
        mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); 
       } 
       mHeaderViewVisible = true; 
       break; 
      } 

      case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP: { 
       View firstView = getChildAt(0); 
       if(firstView==null){ 
        if (mHeaderView.getTop() != 0) { 
         mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); 
        } 
        mHeaderViewVisible = true; 
        break; 
       } 
       int bottom = firstView.getBottom(); 
       int itemHeight = firstView.getHeight(); 
       int headerHeight = mHeaderView.getHeight(); 
       int y; 
       int alpha; 
       if (bottom < headerHeight) { 
        y = (bottom - headerHeight); 
        alpha = MAX_ALPHA * (headerHeight + y)/headerHeight; 
       } else { 
        y = 0; 
        alpha = MAX_ALPHA; 
       } 
       mAdapter.configurePinnedHeader(mHeaderView, group, alpha); 
       //выползание 
       if (mHeaderView.getTop() != y) { 
        mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y); 
       } 
       mHeaderViewVisible = true; 
       break; 
      } 
     } 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     super.dispatchDraw(canvas); 
     if (mHeaderViewVisible) { 
      drawChild(canvas, mHeaderView, getDrawingTime()); 
     } 
    } 

} 

ExpandableList.java

package com.example; 


import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.AbsListView.LayoutParams; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListAdapter; 
import android.widget.SectionIndexer; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.PinnedHeaderExpListView.PinnedHeaderAdapter; 



/** 
* Demonstrates expandable lists using a custom {@link ExpandableListAdapter} 
* from {@link BaseExpandableListAdapter}. 
*/ 
public class ExpandableList extends Activity { 

    MyExpandableListAdapter mAdapter; 
    PinnedHeaderExpListView elv; 

    private int mPinnedHeaderBackgroundColor; 
    private int mPinnedHeaderTextColor; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Set up our adapter 
     mAdapter = new MyExpandableListAdapter(); 
     elv = (PinnedHeaderExpListView) findViewById(R.id.list); 

     elv.setAdapter(mAdapter); 

     mPinnedHeaderBackgroundColor = getResources().getColor(android.R.color.black); 
     mPinnedHeaderTextColor = getResources().getColor(android.R.color.white); 

     elv.setGroupIndicator(null); 
     View h = LayoutInflater.from(this).inflate(R.layout.header, (ViewGroup) findViewById(R.id.root), false); 
     elv.setPinnedHeaderView(h); 
     elv.setOnScrollListener((OnScrollListener) mAdapter); 
     elv.setDividerHeight(0); 
    } 

    /** 
    * A simple adapter which maintains an ArrayList of photo resource Ids. 
    * Each photo is displayed as an image. This adapter supports clearing the 
    * list of photos and adding a new photo. 
    * 
    */ 
    public class MyExpandableListAdapter extends BaseExpandableListAdapter implements PinnedHeaderAdapter, OnScrollListener{ 
     // Sample data set. children[i] contains the children (String[]) for groups[i]. 
     private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private String[][] children = { 
       { "Arnold", "Barry", "Chuck", "David", "Stas", "Oleg", "Max","Alex","Romeo", "Adolf" }, 
       { "Ace", "Bandit", "Cha-Cha", "Deuce", "Nokki", "Baron", "Sharik", "Toshka","SObaka","Belka","Strelka","Zhuchka"}, 
       { "Fluffy", "Snuggles","Cate", "Yasha","Bars" }, 
       { "Goldy", "Bubbles","Fluffy", "Snuggles","Guffy", "Snoopy" } 
     }; 

     public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
     } 

     public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
     } 

     public int getChildrenCount(int groupPosition) { 
      return children[groupPosition].length; 
     } 

     public TextView getGenericView() { 
      // Layout parameters for the ExpandableListView 
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 64); 

      TextView textView = new TextView(ExpandableList.this); 
      textView.setLayoutParams(lp); 
      // Center the text vertically 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
      // Set the text starting position 
      textView.setPadding(36, 0, 0, 0); 
      return textView; 
     } 

     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
       View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getChild(groupPosition, childPosition).toString()); 
      return textView; 
     } 


     public Object getGroup(int groupPosition) { 
      return groups[groupPosition]; 
     } 

     public int getGroupCount() { 
      return groups.length; 
     } 

     public long getGroupId(int groupPosition) { 
      return groupPosition; 
     } 

     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
       ViewGroup parent) { 
      TextView textView = (TextView) LayoutInflater.from(getApplicationContext()).inflate(R.layout.header, parent, false); 
      textView.setText(getGroup(groupPosition).toString()); 
      return textView; 
     } 

     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 

     public boolean hasStableIds() { 
      return true; 
     } 


     /** 
     * размытие/пропадание хэдера 
     */ 
     public void configurePinnedHeader(View v, int position, int alpha) { 
      TextView header = (TextView) v; 
      final String title = (String) getGroup(position); 

      header.setText(title); 
      if (alpha == 255) { 
       header.setBackgroundColor(mPinnedHeaderBackgroundColor); 
       header.setTextColor(mPinnedHeaderTextColor); 
      } else { 
       header.setBackgroundColor(Color.argb(alpha, 
         Color.red(mPinnedHeaderBackgroundColor), 
         Color.green(mPinnedHeaderBackgroundColor), 
         Color.blue(mPinnedHeaderBackgroundColor))); 
       header.setTextColor(Color.argb(alpha, 
         Color.red(mPinnedHeaderTextColor), 
         Color.green(mPinnedHeaderTextColor), 
         Color.blue(mPinnedHeaderTextColor))); 
      } 
     } 

     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      if (view instanceof PinnedHeaderExpListView) { 
       ((PinnedHeaderExpListView) view).configureHeaderView(firstVisibleItem); 
      } 

     } 

     public void onScrollStateChanged(AbsListView view, int scrollState) { 
      // TODO Auto-generated method stub 

     } 

    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <view class="com.example.PinnedHeaderExpListView" 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

header.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="25dp" 
    android:background="@android:color/black" > 

</TextView> 

यह काम करता है, के रूप में उम्मीद पर क्लिक छोड़कर हैडर। मेरी इच्छा है कि हेडर पर क्लिक करने से समूह आइटम पर बराबर क्लिक किया जाएगा, लेकिन घटना भी नहीं होती है और ऑनक्लिक लिस्टर को नियंत्रण नहीं मिलता है। क्यूं कर ?

संपादित करें: यदि आप ExpandableList.java गतिविधि के onCreate() विधि के अंदर कोड का टुकड़ा निम्नलिखित जोड़ें शीर्षक पर क्लिक भी काम करता है।

 elv.setOnGroupClickListener(new OnGroupClickListener() { 

     @Override 
     public boolean onGroupClick(ExpandableListView parent, View v, 
       int groupPosition, long id) { 
      return false; 
     } 
    }); 
+0

पर के लिए कहा, आप कहाँ से अपने वर्गों मिलता है? क्योंकि समूह बच्चों के साथ सूची आइटम है। मैं कहीं भी नहीं देखता जहां आप हेडर और सेक्शन सेट करते हैं। क्या आप स्क्रीनशॉट जोड़ सकते हैं ताकि मैं जान सकूं कि इस कोड से क्या उम्मीद करनी है? आप अनुभाग कैसे सेट करते हैं? ConfigPinnedHeader में, यह एक शीर्षलेख बनाता है, लेकिन केवल एक बार कॉल किया जाता है क्योंकि मुझे नहीं पता कि हेडर के नीचे जाने वाले अनुभागों को कैसे बनाया जाए। मैंने कई बार जवाब पर पढ़ा है और मुझे इसे समझने की प्रतीत नहीं हो रही है। कृपया मदद करें? –

+0

यह बहुत अच्छा है ... धन्यवाद :) हम समूह शीर्षकों को हमेशा दिखाई दे सकते हैं ... इसके वर्तमान रूप में केवल वर्तमान सक्रिय शीर्षलेख पिन किया गया है यदि इसके साथ जुड़े आइटमों की संख्या बड़ी है और अन्य शीर्षकों को देखने वाले क्षेत्र से बाहर धकेल दिया जाता है – Nav

+0

अरे @ होमो विस्तार/पतन ऑपरेशन के लिए एनीमेशन जोड़ने का कोई तरीका है? – Tony

-3
एडाप्टर में

:

public void setSelectedPosition(int position){ 
    this.listChildPosition=position; 
} 

getchildview

 if (listChildPosition == childPosition) { 
     convertView.setBackgroundColor(context.getResources().getColor(
       R.color.white)); 
    } else { 
     convertView.setBackgroundColor(context.getResources().getColor(
       R.color.expandlist)); 
    } 

में onChildClick

adapter.setSelectedPosition(childPosition); 
    adapter.notifyDataSetChanged(); 
    v.setSelected(true); 
+0

यह नहीं है, क्या मैं सभी –

3

मैंने होमो इंकोग्निटो के समाधान को लागू करने की कोशिश की है और पिन किए गए हेडर की एक ही समस्या का सामना करने योग्य नहीं है।

अपराधी सूची दृश्य स्वयं ही सभी क्लिक ईवेंट का उपभोग करता है, इस प्रकार हमारे 'संवर्द्धित' हेडर व्यू में से कोई भी नहीं गुजरता है। तो आप ExpandableListView के क्लिक हैंडलिंग कार्यान्वयन में खुदाई करने का प्रयास कर सकते हैं, जो एडाप्टर व्यू तक सभी तरह से अपनी विरासत पर विचार कर रहा है।

ListView से क्लिक को हाइजैक करने का प्रयास करने के बजाय, मैं नीचे दिए गए सूची आइटम से हेडर क्लिक को अनुकरण करके इस तरह के मुद्दे को सर्क्यूविगेट करने का प्रयास करता हूं। ऐसा करने के लिए, आपको पहले देखने के लिए onChildClick को लागू करने की आवश्यकता है कि क्लिक किए गए आइटम की स्थिति पिन किए गए शीर्षलेख के नीचे है, यदि ऐसा है, तो आप क्लिक को सही शीर्षलेख पर रिले कर सकते हैं, यदि नहीं, तो बस उसके लिए क्लिक को संसाधित करें सामान्य रूप से आइटम।



उदाहरण में नीचे, क्लिक किए जाने आइटम पिन किए गए शीर्ष लेख के नीचे है, मैं बस ListView सच हैडर को स्क्रॉल कर दिया है, इस प्रकार की जगह 'संवर्धित' सच हेडर के साथ पिन किए गए शीर्ष लेख, और उपयोगकर्ता कर सकते हैं वहां से आगे की कार्रवाई करें, उदाहरण के लिए समूह को तोड़ना

ध्यान दें कि ऐसे उपयोगिता प्रवाह केवल तभी काम करता है जब आपके पास हेडर आइटम पर कोई क्लिक करने योग्य दृश्य न हो, अन्यथा आपको पिन क्लिक वर्चुअल हेडर और onInterceptTouchEvent के साथ सच्चे हेडर के बीच सभी क्लिक रिले और मैपिंग करना होगा ।गतिविधि onCreate में

public int getHeaderViewHeight(){ 
     return mHeaderViewHeight; 
    } 

, निम्नलिखित संलग्न:


होमो गुप्त के कोड के बाद , PinnedHeaderExpListView.java में निम्न विधि जोड़ने

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

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

      // we need to obtain the relative y coordinate of the child view, 
      // not its clicked subview, thus first we try to calculate its true index 
      long packedPos = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition); 
      int viewPos = elv.getFlatListPosition(packedPos) - elv.getFirstVisiblePosition(); 
      View childView = parent.getChildAt(viewPos); // got it 


      if (childView.getTop() < elv.getHeaderViewHeight()*.75){ 
       // if the clicked child item overlaps more than 25% 
       // of pinned header, consider it being underneath 
       long groupPackedPos = ExpandableListView.getPackedPositionForGroup(groupPosition); 
       int groupFlatPos = elv.getFlatListPosition(groupPackedPos); 
       elv.smoothScrollToPosition(groupFlatPos); 
      } 
      return true; 
     } 
    }); 
0

होमो गुप्त के जवाब में, बच्चे में देखें पिन किए गए हेड व्यू क्लिक इवेंट पर क्लिक और प्राप्त नहीं कर सकते हैं, लेकिन मुझे एक रास्ता मिला। https://github.com/chenjishi/PinnedHeadListView

private final Rect mRect = new Rect(); 
private final int[] mLocation = new int[2]; 

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    if (mHeaderView == null) return super.dispatchTouchEvent(ev); 

    if (mHeaderViewVisible) { 
     final int x = (int) ev.getX(); 
     final int y = (int) ev.getY(); 
     mHeaderView.getLocationOnScreen(mLocation); 
     mRect.left = mLocation[0]; 
     mRect.top = mLocation[1]; 
     mRect.right = mLocation[0] + mHeaderView.getWidth(); 
     mRect.bottom = mLocation[1] + mHeaderView.getHeight(); 

     if (mRect.contains(x, y)) { 
      if (ev.getAction() == MotionEvent.ACTION_UP) { 
       performViewClick(x, y); 
      } 
      return true; 
     } else { 
      return super.dispatchTouchEvent(ev); 
     } 
    } else { 
     return super.dispatchTouchEvent(ev); 
    } 
} 

private void performViewClick(int x, int y) { 
    if (null == mHeaderView) return; 

    final ViewGroup container = (ViewGroup) mHeaderView; 
    for (int i = 0; i < container.getChildCount(); i++) { 
     View view = container.getChildAt(i); 

     /** 
     * transform coordinate to find the child view we clicked 
     * getGlobalVisibleRect used for android 2.x, getLocalVisibleRect 
     * user for 3.x or above, maybe it's a bug 
     */ 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
      view.getGlobalVisibleRect(mRect); 
     } else { 
      view.getLocalVisibleRect(mRect); 
      int width = mRect.right - mRect.left; 
      mRect.left = Math.abs(mRect.left); 
      mRect.right = mRect.left + width; 
     } 

     if (mRect.contains(x, y)) { 
      view.performClick(); 
      break; 
     } 
    } 
} 

इस तरह से मैं पिन किए गए दृश्य में क्लिक करें घटना को संभालने है, dispatchTouchEvent ओवरराइड: मैं पर कोड डाल दिया।

enter image description here

संबंधित मुद्दे