简体   繁体   中英

Set ImageButton visible in ExpandableListView for only one of the list groups

I have an ExpandableListView where I want have an ImageButton for only one of the groups. I am currently hiding it with visibility:gone in the layout for the group, and plan on making it visible for only one of the groups, where the section group's title is "Favorite Locations". The specific group is created at line:

Section oGeneralSection = new Section("Favorite Locations");

My current attempt in the SectionListAdapter.java

EDITED - the code below now works

    @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;
}

The program is throwing a 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 - contains and creates "Favorite Locations"

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 - where the ImageButton "favoritelocations_addbutton" is set as gone

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

Set the Visbility of your image button as GONE/VISIBLE in the getView() of your SectionListAdapter . something like this

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

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

}else{

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

}

Please note that switching the the visibilty is to Visible and gone based on the check is quite important as if you are only checking for the matching case and switching the visibility to visible and forgets to change the visibilty to Gone for teh other scenarios then your imageButton will be visible for each and every Item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM