2016-11-28 87 views
0

我有一個ExpandleListView和一些TextViews和圖像。 克隆後,我想showTextView作爲孩子,並隱藏GroupView中的另一個TextView。不幸的是,列表顯示正確,但點擊後的項目根本不擴展,事件當我設置OnGroupClickListener並嘗試調試它時,斷點不會觸發。Expandable ListView不擴展

XML

<<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/notificationItem" 
    android:orientation="vertical" 
    android:padding="15sp" 
    android:animateLayoutChanges="true" 
    > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:layout_gravity="start" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:id="@+id/notificationLogo" 
     android:adjustViewBounds="true" 
     android:layout_alignParentLeft="true" 
     android:clickable="false"/> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingRight="20sp" 
     android:paddingLeft="10sp" 
     android:layout_toRightOf="@+id/notificationLogo" 
     android:orientation="vertical" 
     android:id="@+id/headerContent"> 

     <TextView 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/date"/> 
    <TextView 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/eventName"/> 

    <TextView 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/shortDescription" 
     android:ellipsize="end" 
     android:maxLines="2"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/fullDescription" 
      android:layout_gravity="center"/> 
    </LinearLayout> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/downButton" 
     android:layout_centerInParent="true" 
     android:layout_below="@+id/headerContent" 
     android:background="@android:color/transparent"/> 
</RelativeLayout> 
</LinearLayout> 

適配器類

public class NotificationAdapter extends BaseExpandableListAdapter { 

    private Context mContext; 
    private ViewHolder viewHolder; 
    final Typeface typeface; 
    List<MuseumNotification> notificationList; 

    public NotificationAdapter(Context context, List<MuseumNotification>notificationList){ 
     mContext = context; 
     typeface = Typeface.createFromAsset(mContext.getAssets(),"fonts/fontawesome-webfont.ttf"); 
     this.notificationList =notificationList; 
    } 

    @Override 
    public int getGroupCount() { 
     return notificationList.size(); 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return 1; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return notificationList.get(groupPosition); 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return notificationList.get(groupPosition); 
    } 

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

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

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     if(viewHolder==null) 
      viewHolder =new ViewHolder(); 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.notification_item,parent,false); 

     viewHolder.notificationTitle=(TextView)convertView.findViewById(R.id.eventName); 
     viewHolder.shortDescription=(TextView)convertView.findViewById(R.id.shortDescription); 
     viewHolder.image_url=(ImageView)convertView.findViewById(R.id.notificationLogo); 
     viewHolder.notificationTitle.setText(notificationList.get(groupPosition).getTitle()); 
     viewHolder.shortDescription.setText(notificationList.get(groupPosition).getText()); 
     Glide.with(mContext) 
       .load("www.logo.png") 
       .fitCenter() 
       .into(viewHolder.image_url); 
     viewHolder.navDown=(Button)convertView.findViewById(R.id.downButton); 
     viewHolder.navDown.setTypeface(typeface); 
     viewHolder.navDown.setText(R.string.navDown); 
     return convertView; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.notification_item,parent,false); 
     viewHolder.description=(TextView)convertView.findViewById(R.id.fullDescription); 
     viewHolder.description.setText(notificationList.get(groupPosition).getText()); 
     return convertView; 
    } 

    @Override 
    public void onGroupExpanded(int groupPosition) { 
     super.onGroupExpanded(groupPosition); 
    } 

    @Override 
    public void onGroupCollapsed(int groupPosition) { 
     super.onGroupCollapsed(groupPosition); 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return false; 
    } 
    private class ViewHolder{ 
     private TextView notificationTitle,description,shortDescription; 
     private ImageView image_url; 
     private Button navDown; 
    } 
} 

編輯:解決:在組項目i有一個可點擊的元件(按鈕)。它應該是不可聚焦的(android:focusable =「false」,XML)

回答

0

從你提到的代碼中,我認爲你沒有在你的代碼中定義HashMap<>()。你需要定義你已經存儲的HashMap作爲key的'group_title'和作爲value的'子標題'。通過使用期望的groupPosition(密鑰)訪問散列映射的特定值,替換getChild(int groupPosition, int childPosition)方法中的代碼。

對於EG- 如果你讓一個HashMap原樣 HashMap<>(MuseumNotification, String) hm; 然後在getChild(int groupPosition, int childPosition)方法寫代碼如下

return this.hm.get(this.notificationList.get(groupPosition)) 
       .get(childPosition); 

這裏notificationList是你在你的代碼。這已經定義了List會給你需要那個特定的孩子groupPosition ..

你的代碼在中返回Group(Object),有你想要一個Child(對象)

0

samarth kejriwal您answear不但是解決這個問題,當我嘗試programicly在onCreate方法來打開項目其正確地將擴大(由expandleListView.expandGroup(position);)我試圖expandleListView.setOnItemClickListener,但其不工作。只有當我點擊它時,我才能滾動我的列表。