gpt4 book ai didi

java - 将 ImageButton 设置为仅对其中一个列表组在 ExpandableListView 中可见

转载 作者:行者123 更新时间:2023-12-01 14:06:37 25 4
gpt4 key购买 nike

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

最佳答案

SectionListAdaptergetView() 中将图像按钮的可见性设置为 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)

}

请注意,根据检查将可见性切换为可见并消失非常重要,就好像您仅检查匹配的情况并将可见性切换为可见而忘记将可见性更改为其他情况下的消失那么你的 imageButton 对于每个 Item 都是可见的。

关于java - 将 ImageButton 设置为仅对其中一个列表组在 ExpandableListView 中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com