Dhi*_*man 1 android expandablelistview expandablelistadapter
我将使用自定义可扩展列表适配器实现可扩展的列表视图.
并且还有组扩展和组崩溃的图像更改.
我的可扩展列表视图效果很好但是当我在这些事件上设置图像更改时会出现问题.即当我展开一个时,另一个人的图像也会自动改变.我想我不能保持组项目的正确地址.
这是我的代码.
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;
}
Run Code Online (Sandbox Code Playgroud)
和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;
}
}
Run Code Online (Sandbox Code Playgroud)
Zeb*_*eba 11
在您的自定义适配器中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);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在更改组扩展(折叠或扩展)的情况下更改父项图像的最佳方法是使用getGroupView()方法和属性isExpandedbolean 方法.
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
...
if (isExpanded) {
groupHolder.img.setImageResource(R.drawable.group_down);
} else {
groupHolder.img.setImageResource(R.drawable.group_up);
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6939 次 |
| 最近记录: |