gpt4 book ai didi

Android 单击时更改图像,自定义可扩展列表适配器

转载 作者:行者123 更新时间:2023-11-29 14:19:03 25 4
gpt4 key购买 nike

我将使用自定义可扩展列表适配器实现可扩展 ListView 。
以及组扩展和组折叠时的图像更改。

我的可扩展 ListView 运行良好,但是当我为这些事件设置图像更改时,出现了一些问题。即当我展开一个时,另一个的图像也会自动改变。我想我无法保存组项目的正确地址。

这是我的代码。

public class HomeFrame extends Fragment {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
ImageView img_selection;
HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
public static int[] GalImages = new int[] {
R.drawable.banner_one,
R.drawable.banner,
R.drawable.banner_three,
R.drawable.banner_two,};
public HomeFrame() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.home_frame, container, false);

ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.banner);
ImageAdapter adapter = new ImageAdapter(getActivity());
viewPager.setAdapter(adapter);

//img_selection=(ImageView)rootView.findViewById(R.id.img_selection);

expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

// preparing list data
prepareListData();

listAdapter = new ExpListAdapter(getActivity(), listDataHeader,
listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);

// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getActivity(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;

}
});

// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {

Toast.makeText(getActivity(),
groupPosition + " Expanded",
Toast.LENGTH_SHORT).show();

img_selection=(ImageView)expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
img_selection.setImageResource(R.drawable.arrow_se);
}
});

// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {

Toast.makeText(getActivity(),
groupPosition + " Collapsed",
Toast.LENGTH_SHORT).show();

img_selection=(ImageView) expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
//ImageView img_selection=(ImageView) get.findViewById(R.id.img_selection);

img_selection.setImageResource(R.drawable.arrow);
}
});

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
/*
* Toast.makeText( getActivity(),
* listDataHeader.get(groupPosition) + " : " +
* listDataChild.get( listDataHeader.get(groupPosition)).get(
* childPosition), Toast.LENGTH_SHORT) .show();
*/
return false;
}
});

return rootView;
}

Adapter 类是..

public class ExpListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;

}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_home, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.heading);
//ImageView img_selection=(ImageView)convertView.findViewById(R.id.img_selection);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
//img_selection.setImageResource(R.drawable.arrow);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

最佳答案

getGroupView() 中的自定义适配器中执行此操作:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view,
ViewGroup parent) {

ImageView img_selection=(ImageView) view.findViewById(R.id.img_selection);
int imageResourceId = isExpanded ? R.drawable.group_closed
: R.drawable.group_open;
img_selection.setImageResource(imageResourceId);
}

关于Android 单击时更改图像,自定义可扩展列表适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911021/

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