我有一个 ExpandableListView,我希望其中一个组只有一个 ImageButton。我目前以可见性隐藏它:在组的布局中消失,并计划使其仅对其中一个组可见,其中部分组的标题是“最喜欢的位置”。特定组在以下行创建:
Section oGeneralSection = new Section("Favorite Locations");
我目前在 SectionListAdapter.java 中的尝试
已编辑 - 下面的代码现在可以使用
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.slidingmenu_sectionview,
parent, false);
}
TextView textView = (TextView) convertView
.findViewById(R.id.slidingmenu_section_title);
textView.setText(((Section) getGroup(groupPosition)).getTitle());
//TODO make this set the add_image_button to VISIBLE
if( sections.get(groupPosition).getTitle().equalsIgnoreCase("Favorite Locations") ){
convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.VISIBLE);
}else{
convertView.findViewById(R.id.favoritelocations_addbutton).setVisibility(View.GONE);
}
return convertView;
}
程序抛出 NullPointerException
09-17 22:18:06.566: W/dalvikvm(24531): threadid=1: thread exiting with uncaught exception (group=0x40cf8438)
09-17 22:18:06.636: E/AndroidRuntime(24531): FATAL EXCEPTION: main
09-17 22:18:06.636: E/AndroidRuntime(24531): java.lang.NullPointerException
09-17 22:18:06.636: E/AndroidRuntime(24531): at com.planner.SectionListAdapter.getGroupView(SectionListAdapter.java:85)
09-17 22:18:06.636: E/AndroidRuntime(24531): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
09-17 22:18:06.636: E/AndroidRuntime(24531): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
09-17 22:18:06.636: E/AndroidRuntime(24531): at android.widget.ListView.makeAndAddView(ListView.java:1769)
SlidingMenuFragment.java - 包含并创建“最喜欢的位置”
public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {
private ExpandableListView sectionListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<Section> sectionList = createMenu();
View view = inflater.inflate(R.layout.slidingmenu_fragment, container, false);
this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);
this.sectionListView.setGroupIndicator(null);
SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
this.sectionListView.setAdapter(sectionListAdapter);
this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
this.sectionListView.setOnChildClickListener(this);
int count = sectionListAdapter.getGroupCount();
for (int position = 0; position < count; position++) {
this.sectionListView.expandGroup(position);
}
return view;
}
/**
* Creates the menu for the SlidingMenu by adding all the items to the section list
* @return sectionList the list of sections with section items
*/
private List<Section> createMenu() {
List<Section> sectionList = new ArrayList<Section>();
//TODO implement Set Current on mStartSection and mEndSection
Section mStartSection = new Section("Start Location");
mStartSection.addSectionItem(sID_SET_AND_PROMPT_PERM_START_LOC,"Set Permanent", "slidingmenu_setpermanent");
mStartSection.addSectionItem(sID_CLEAR_PERM_START_LOC,"Clear Permanent", "slidingmenu_clear");
// mStartSection.addSectionItem(sID_SET_CURRENT_AS_START,"Set Current", "slidingmenu_friends");
Section mEndSection = new Section("End Location");
mEndSection.addSectionItem(sID_SET_AND_PROMPT_PERM_END_LOC,"Set Permanent", "slidingmenu_setpermanent");
mEndSection.addSectionItem(sID_CLEAR_PERM_END_LOC,"Clear Permanent", "slidingmenu_clear");
// mEndSection.addSectionItem(sID_SET_CURRENT_AS_END,"Set Current", "slidingmenu_friends");
Section oGeneralSection = new Section("Favorite Locations");
sectionList.add(mStartSection);
sectionList.add(mEndSection);
sectionList.add(oGeneralSection);
return sectionList;
}
...
}
slipmenu_sectionview.xml - ImageButton“favoritelocations_addbutton”设置为消失
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/slidingmenu_section_title"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Section"
android:textSize="22sp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:background="@android:color/transparent"
android:textColor="#FFFFFF"
android:textStyle="bold|italic" />
<ImageButton
android:id="@+id/favoritelocations_addbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_add"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/white"/>
</LinearLayout>