在点击更改Android图片,自定义扩展列表适配器 [英] Android Change image on click,custom expandable list adapter
问题描述
我要去实施消耗的ListView自定义扩展列表适配器..
而且还对组图像变化扩大和崩溃组...我可扩展列表视图工作做好,但是当我设置的图像变化对这些事件有,当我展开一个autometically另外一个人的形象也改变一些problem..ie ...我想我不能举行组项目的正确地址。 PLZ帮我...
这里是我的code ..
公共类HomeFrame扩展片段{ ExpandableListAdapter listAdapter;
ExpandableListView expListView;
清单<串GT; listDataHeader;
ImageView的img_selection;
HashMap的<字符串列表<串GT;> listDataChild =新的HashMap<字符串列表<串GT;>();
公共静态INT [] = GalImages新INT [] {
R.drawable.banner_one,
R.drawable.banner,
R.drawable.banner_three,
R.drawable.banner_two,};
公共HomeFrame(){ } @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.home_frame,集装箱,FALSE); ViewPager viewPager =(ViewPager)rootView.findViewById(R.id.banner);
ImageAdapter适配器=新ImageAdapter(getActivity());
viewPager.setAdapter(适配器); //img_selection=(ImageView)rootView.findViewById(R.id.img_selection); expListView =(ExpandableListView)rootView.findViewById(R.id.lvExp); // preparing列表数据
prepareListData(); listAdapter =新ExpListAdapter(getActivity(),listDataHeader,
listDataChild); //设置列表适配器
expListView.setAdapter(listAdapter); //列表视图组单击监听器
expListView.setOnGroupClickListener(新OnGroupClickListener(){ @覆盖
公共布尔onGroupClick(ExpandableListView父视图V,
INT groupPosition,长ID){
Toast.makeText(getActivity(),
集团已点击+ listDataHeader.get(groupPosition)
Toast.LENGTH_SHORT).show();
返回false; }
}); //列表视图集团扩大监听器
expListView.setOnGroupExpandListener(新OnGroupExpandListener(){ @覆盖
公共无效onGroupExpand(INT groupPosition){ Toast.makeText(getActivity(),
groupPosition +展开,
Toast.LENGTH_SHORT).show(); img_selection =(ImageView的)expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
img_selection.setImageResource(R.drawable.arrow_se);
}
}); //列表视图组collasped监听器
expListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){ @覆盖
公共无效onGroupCollapse(INT groupPosition){ Toast.makeText(getActivity(),
groupPosition +坍塌,
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);
}
}); //列表视图儿童点击监听器
expListView.setOnChildClickListener(新OnChildClickListener(){ @覆盖
公共布尔onChildClick(ExpandableListView父视图V,
INT groupPosition,诠释childPosition,长ID){
// TODO自动生成方法存根
/ *
* Toast.makeText(getActivity(),
* listDataHeader.get(groupPosition)+:+
* listDataChild.get(listDataHeader.get(groupPosition))。得到(
* childPosition),Toast.LENGTH_SHORT).show();
* /
返回false;
}
}); 返回rootView;
}
和适配器类。
公共类ExpListAdapter扩展BaseExpandableListAdapter { 私人语境_context;
私人列表<串GT; _listDataHeader; //头标题
//子数据头标题格式,标题子
私人的HashMap<字符串列表<串GT;> _listDataChild; 公共ExpListAdapter(上下文的背景下,列表与LT;弦乐> listDataHeader,
HashMap的<字符串列表<串GT;> listChildData){
this._context =背景;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData; } @覆盖
公共对象getChild(INT groupPosition,诠释childPosititon){
返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
获得(childPosititon);
} @覆盖
众长getChildId(INT groupPosition,诠释childPosition){
返回childPosition;
} @覆盖
公共查看getChildView(INT groupPosition,最终诠释childPosition,
布尔isLastChild,查看convertView,父母的ViewGroup){ 最后弦乐childText =(字符串)getChild(groupPosition,childPosition); 如果(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);
返回convertView;
} @覆盖
公众诠释getChildrenCount(INT groupPosition){
返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
。尺寸();
} @覆盖
公共对象getGroup(INT groupPosition){
返回this._listDataHeader.get(groupPosition);
} @覆盖
公众诠释getGroupCount(){
返回this._listDataHeader.size();
} @覆盖
众长getGroupId(INT groupPosition){
返回groupPosition;
} @覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,父母的ViewGroup){
字符串headerTitle =(字符串)getGroup(groupPosition);
如果(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); 返回convertView;
} @覆盖
公共布尔hasStableIds(){
返回false;
} @覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}}
在自定义适配器在 getGroupView()
做到这一点:
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,观景,
父母的ViewGroup){ 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);
}
i am going to implementing expendable listview with custom expandable list adapter.. And also image change on group expand and group collapse...My Expandable list view work well but when i set image change on those event there are some problem..i.e when i expand one the autometically another one's image also change...i think i can not hold the proper address of the group item. Plz help me ... here is my code..
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;
}
And Adapter class is..
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;
}
}
In your custom adapter in the getGroupView()
do this:
@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图片,自定义扩展列表适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!