2011-08-02 147 views
1

我正在開發一個用於蜂窩的應用程序,並且我已經在操作欄中添加了一些選項卡,但現在我不知道如何將ExpandableListView放置在其中一個選項卡中,或者在一個選項卡中的片段。將ExpandableListView添加到片段

回答

1

以下是對我的作品:

內部片段

private String[] categories; 
private String[][] categoryItems; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    categories = getResources().getStringArray(R.array.videos_curriculum); 

    String[] category1 = getResources().getStringArray(R.array.video_category1); 
    String[] category2 = getResources().getStringArray(R.array.video_category2); 
    String[] category3 = getResources().getStringArray(R.array.video_category3); 
    categoryItems = new String[][]{category1, category2, category3}; 

} 


@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    listView = (ExpandableListView) view.findViewById(R.id.expListView); 
    listView.setAdapter(new VideoGroupsListAdapter(categories, categoryItems)); 
    listView.setGroupIndicator(null); // I don't need group indicator on left 
} 

,內部類:

public class VideoGroupsListAdapter extends BaseExpandableListAdapter { 
    private final LayoutInflater inflater; 
    private String[] groups; 
    private String[][] children; 

    public VideoGroupsListAdapter(String[] groups, String[][] children) { 
     this.groups = groups; 
     this.children = children; 
     inflater = LayoutInflater.from(getActivity()); 
    } 

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

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

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

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

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

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.new_video_header, parent, false); 
      holder = new ViewHolder(); 

      holder.text = (TextView) convertView.findViewById(R.id.headerTitleTxt); 
      holder.icon = (TextView) convertView.findViewById(R.id.headerIconTxt); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.text.setText(getGroup(groupPosition).toString()); 
     if (isExpanded) { 
      holder.icon.setText(R.string.ic_down); 
     } else { 
      holder.icon.setText(R.string.ic_up); 
     } 

     return convertView; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.new_video_list_item, parent, false); 
      holder = new ViewHolder(); 

      holder.text = (TextView) convertView.findViewById(R.id.titleTxt); 
      holder.icon = (TextView) convertView.findViewById(R.id.watchedIconTxt); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.text.setText(getChild(groupPosition, childPosition).toString()); 

     return convertView; 
    } 

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

    private class ViewHolder { 
     TextView text; 
     TextView icon; 
    } 

} 

new_videos_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    style="@style/ListItem.Header.Light" 
    android:gravity="center_vertical" 
    android:paddingLeft="@dimen/default_scr_side_padding" 
    android:paddingRight="@dimen/default_scr_side_padding" 
    > 

    <!--Title--> 
    <TextView 
     android:id="@+id/headerTitleTxt" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingLeft="8dip" 
     style="@style/HeaderTitle.Dark" 
     /> 

    <!--Right Icon--> 
    <TextView 
     android:id="@+id/headerIconTxt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/ic_up" 
     /> 

</LinearLayout>