在ExpandableListView中仅为其中一个列表组设置ImageButton

时间:2013-09-17 11:36:06

标签: java android android-fragments expandablelistview

我有一个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;
}
...
}

slidingmenu_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>

1 个答案:

答案 0 :(得分:1)

SectionListAdapter getView()中将图像的可见性按钮设置为GONE / VISIBLE。像这样的东西

if( sectionList.get(position).get(Something).equalsIgnoreCase("Favorite Locations") ){

findViewById(R.id.ImageButton).setVisibilty(View.Visible)

}else{

    findViewById(R.id.ImageButton).setVisibilty(View.Gone)

}

请注意,将visibilty切换为Visible并且根据检查消失是非常重要的,就好像您只检查匹配的情况并将可见性切换为可见并忘记将visibilty更改为Gone for teh其他方案那么你的imageButton对于每一个项目都是可见的。