片段内的expandableListView

时间:2013-05-28 12:21:55

标签: android expandablelistview

我有一个Android应用程序,它包含一个ActionBar选项卡片段,我想将expandableListView放在片段中我该怎么做?这是我的代码:

这是ActionBarActivity

public class TabActionBarActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_action_bar);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    String label1 = getResources().getString(R.string.label1);
    Tab tab = actionBar.newTab();
    tab.setText(label1);

    TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);

   tab.setTabListener(tl);
    actionBar.addTab(tab);


    String label2 = getResources().getString(R.string.label2);
            tab = actionBar.newTab();
            tab.setText(label2);
            TabListener<Tab2Fragment> tl2 = new TabListener<Tab2Fragment>(this, label2, Tab2Fragment.class);
            tab.setTabListener(tl2);
            actionBar.addTab(tab);


}


private class TabListener<T extends Fragment> implements
            ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;


        public TabListener(Activity activity, String tag, Class<T> clz) {
                        mActivity = activity;
                        mTag = tag;
                        mClass = clz;
                    }



        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }




        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
             // Check if the fragment is already initialized
                        if (mFragment == null) {
                            // If not, instantiate and add it to the activity
                            mFragment = Fragment.instantiate(mActivity, mClass.getName());
                            ft.add(android.R.id.content, mFragment, mTag);
                        } else {
                            // If it exists, simply attach it in order to show it
                            ft.attach(mFragment);
                        }


        }




        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
             if (mFragment != null) {
                                 // Detach the fragment, because another one is being attached
                                 ft.detach(mFragment);
                             }


        }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tab_action_bar, menu);
    return true;
}

}

这是Tab1Fragment:

public class Tab1Fragment extends Fragment {

ExpandableListView mExpandableList;



@Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {
        return (LinearLayout) inflater.inflate(R.layout.tab1, container, false);




    }

}

2 个答案:

答案 0 :(得分:2)

这对我有用:

片段内部

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>

答案 1 :(得分:0)

在tab1.xml.Like中添加您的Expandable listview,

tab1.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ExpandableListView>

</RelativeLayout>