问题描述
在我的应用程序,我需要一个expandableListView内显示的图像和它必须是图像的GridView控件,而这些图像可以选择,所以我有一个expandableListAdapter,并在它的内部,我实现一个gridview适配器,该图像显示精细,你可以在图片1看,现在我想要实现的是画面2,我怎么能传递groupPosition和childPosition到gridAdapter和磨片我点击图像,使其突出?
图2:
公共类ExpandListAdapter扩展BaseExpandableListAdapter {
公共静态最终诠释CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
公共静态最终诠释CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/ **
*没有一个孩子可以选择
* /
公共静态最终诠释CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/ **
*一个每组单项选择题
* /
公共静态最终诠释CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/ **
*所有组的一个单一的选择
* /
公共静态最终诠释CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
私人上下文的背景下;
私人的ArrayList<组>组;
私人的ArrayList< ArrayList的<儿童>>孩子=新的ArrayList();
私人的ArrayList<儿童> listchild;
私人GridAdapter适配器;
私人CustomGridView GridView控件;
私人SparseArray< SparseBooleanArray> checkedPositions;
私有静态最后弦乐LOG_TAG = ExpandListAdapter.class.getSimpleName();
私人诠释choiceMode = CHOICE_MODE_MULTIPLE;
公共ExpandListAdapter(语境上下文,的ArrayList&其中;组>组){
this.context =背景;
this.groups =组;
checkedPositions =新SparseArray< SparseBooleanArray>();
孩子=新的ArrayList();
的for(int i = 0; I< groups.size();我++){
child.add(ⅰ,groups.get(ⅰ).getItems());
}
}
公共ExpandListAdapter(上下文的背景下,ArrayList的<组>儿童,INT choiceMode){
这(背景下,子女);
//对于现在的选择模式CHOICE_MODE_MULTIPLE_MODAL
//未执行
如果(choiceMode == CHOICE_MODE_MULTIPLE_MODAL){
抛出新的RuntimeException(选择模式CHOICE_MODE_MULTIPLE_MODAL+
一直尚未实现);
}
this.choiceMode = choiceMode;
}
@覆盖
公共对象getChild(INT groupPosition,诠释childPosition){
返回child.get(childPosition);
}
@覆盖
众长getChildId(INT groupPosition,诠释childPosition){
返回childPosition;
}
@覆盖
公共查看getChildView(最终诠释groupPosition,最终诠释childPosition,
布尔isLastChild,查看convertView,ViewGroup中父){
如果(convertView == NULL){
LayoutInflater infalInflater =(LayoutInflater)上下文
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview,NULL);
}
listchild =新的ArrayList<儿童>();
为(诠释J = 0; J&其中; groups.get(groupPosition).getItems()的大小(); J ++){
listchild.add(child.get(groupPosition)获得(j)条);
}
GridView控件=(CustomGridView)convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(真正的);
适配器=新GridAdapter(背景下,listchild,checkedPositions,groupPosition,childPosition);
gridView.setAdapter(适配器); //适配器
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
gridView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
setClicked(groupPosition,childPosition);
的System.out.println(位置+ checkedPositions.get(groupPosition));
如果(checkedPositions.get(groupPosition)!= NULL){
布尔=器isChecked checkedPositions.get(groupPosition)获得(childPosition);
如果(!器isChecked){
}
} 其他 {
的System.out.println(假);
}
}
});
返回convertView;
}
公共无效setClicked(INT groupPosition,诠释childPosition){
开关(choiceMode){
案例CHOICE_MODE_MULTIPLE:
SparseBooleanArray checkedChildPositionsMultiple = checkedPositions.get(groupPosition);
//如果组中有没有任何子检查
如果(checkedChildPositionsMultiple == NULL){
checkedChildPositionsMultiple =新SparseBooleanArray();
//默认情况下,不检查孩子的状况
//所以点击将使它
checkedChildPositionsMultiple.put(childPosition,真正的);
checkedPositions.put(groupPosition,checkedChildPositionsMultiple);
} 其他 {
布尔oldState = checkedChildPositionsMultiple.get(childPosition);
checkedChildPositionsMultiple.put(childPosition,oldState!);
}
打破;
// TODO:实现它
案例CHOICE_MODE_MULTIPLE_MODAL:
抛出新的RuntimeException(选择模式CHOICE_MODE_MULTIPLE_MODAL+
一直尚未实现);
案例CHOICE_MODE_NONE:
checkedPositions.clear();
打破;
案例CHOICE_MODE_SINGLE_PER_GROUP:
SparseBooleanArray checkedChildPositionsSingle = checkedPositions.get(groupPosition);
//如果组中有没有任何子检查
如果(checkedChildPositionsSingle == NULL){
checkedChildPositionsSingle =新SparseBooleanArray();
//默认情况下,不检查孩子的状况
checkedChildPositionsSingle.put(childPosition,真正的);
checkedPositions.put(groupPosition,checkedChildPositionsSingle);
} 其他 {
布尔oldState = checkedChildPositionsSingle.get(childPosition);
//如果旧的国家是虚假的,将其设置为唯一的其中一个是真实的
如果(!oldState){
checkedChildPositionsSingle.clear();
checkedChildPositionsSingle.put(childPosition,oldState!);
} //否则不允许用户取消它
}
打破;
//该模式将消除来自其他小组的所有选中的位置
//并启用只是一个从选择的组
案例CHOICE_MODE_SINGLE_ABSOLUTE:
checkedPositions.clear();
SparseBooleanArray checkedChildPositionsSingleAbsolute =新SparseBooleanArray();
checkedChildPositionsSingleAbsolute.put(childPosition,真正的);
checkedPositions.put(groupPosition,checkedChildPositionsSingleAbsolute);
打破;
}
//通知,一些数据已被改变
notifyDataSetChanged();
Log.v(LOG_TAG列表中的位置更新);
Log.v(LOG_TAG,PrintSparseArrays.sparseArrayToString(checkedPositions));
}
公共无效setChoiceMode(INT choiceMode){
this.choiceMode = choiceMode;
//对于现在的选择模式CHOICE_MODEL_MULTIPLE_MODAL
//未执行
如果(choiceMode == CHOICE_MODE_MULTIPLE_MODAL){
抛出新的RuntimeException(选择模式CHOICE_MODE_MULTIPLE_MODAL+
一直尚未实现);
}
checkedPositions.clear();
Log.v(LOG_TAG,选择模式已经改变现在是+ this.choiceMode);
}
@覆盖
公众诠释getChildrenCount(INT n组){
返回1;
}
@覆盖
公共对象getGroup(INT groupPosition){
返回groups.get(groupPosition);
}
@覆盖
公众诠释getGroupCount(){
返回groups.size();
}
@覆盖
众长getGroupId(INT groupPosition){
返回groupPosition;
}
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,ViewGroup中父){
组组=(集团)getGroup(groupPosition);
如果(convertView == NULL){
LayoutInflater INF =(LayoutInflater)上下文
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item,NULL);
}
ExpandableListView mExpandableListView =(ExpandableListView)父母;
mExpandableListView.expandGroup(groupPosition);
TextView的电视=(TextView中)convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
返回convertView;
}
@覆盖
公共布尔hasStableIds(){
返回true;
}
@覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}
}
GridAdapter:
公共类GridAdapter扩展了BaseAdapter {
私人语境mContext;
私人的ArrayList<儿童>儿童;
ImageLoader的ImageLoader的= AppController.getInstance()getImageLoader()。
私人SparseArray< SparseBooleanArray> checkedPositions;
INT groupPosition,childPosition;
公共GridAdapter(上下文的背景下,ArrayList的<儿童> childValues,SparseArray< SparseBooleanArray> checkedPositions,INT groupPosition,诠释childPosition){
mContext =背景;
孩子= childValues;
this.checkedPositions = checkedPositions;
this.childPosition = childPosition;
this.groupPosition = groupPosition;
}
@覆盖
公众诠释getCount将(){
返回child.size();
}
@覆盖
公共对象的getItem(INT位置){
返回的位置;
}
@覆盖
众长getItemId(INT为arg0){
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder支架=无效;
NetworkImageView我;
childPosition =位置;
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item,NULL);
持有人=新ViewHolder();
如果(ImageLoader的== NULL)
ImageLoader的= AppController.getInstance()getImageLoader()。
I =(NetworkImageView)convertView
.findViewById(R.id.flag);
i.setImageUrl(将String.valueOf(child.get(childPosition).getImage()),ImageLoader的);
convertView.setTag(保持器);
的System.out.println(位置+ checkedPositions.get(groupPosition));
如果(checkedPositions.get(groupPosition)!= NULL){
布尔=器isChecked checkedPositions.get(groupPosition)获得(childPosition);
i.setBackgroundResource(R.color.bg);
如果(!器isChecked){
}
} 其他 {
的System.out.println(假);
}
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text =(TextView中)convertView.findViewById(R.id.label);
holder.text.setText(child.get(位置).getName());
返回convertView;
}
静态类ViewHolder {
TextView的文字;
}
}
在我的假设
ExpandListAdapter类
-
在getChildView()方法
childPosition
不取决于哪个国家标志被点击,因为 childPosition这里返回网格视图中的位置,不属于它的标志 -
所以,没有必要通过
childPosition
在GridView控件的构造函数,因为childPosition总是返回1 -
要得到实际的标志,点击位置使用onClickListener您GridAdapter内,如:
@覆盖 公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人= NULL;
NetworkImageView我;
childPosition =位置;
如果(convertView == NULL){
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
convertView.setOnclickListener(新OnClickListener {
@覆盖
的onClick(视图v){
器isChecked = child.get(childPosition).isChecked();
如果(!器isChecked){
child.get(childPosition).setChecked(真正的);
}
其他 {
child.get(childPosition).setChecked(假);
}
});
holder.text =(TextView中)convertView.findViewById(R.id.label);
holder.text.setText(child.get(位置).getName());
返回convertView;
}
并从ExpandListAdapter类中删除您入住的逻辑:
gridView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
setClicked(groupPosition,childPosition);
的System.out.println(位置+ checkedPositions.get(groupPosition));
如果(checkedPositions.get(groupPosition)!= NULL){
布尔=器isChecked checkedPositions.get(groupPosition)获得(childPosition);
如果(!器isChecked){
}
} 其他 {
的System.out.println(假);
}
}
});
In my app I need to show images inside a expandableListView and it has to be a gridview of images, and these images can be selectable so I have an expandableListAdapter and inside of it I'm implementing a gridview adapter,the images are showing fine as you can see in the picture 1, now what I'm trying to achieve is the picture 2, how can I pass the groupPosition and the childPosition to the gridAdapter and whe I click on the image make it highlight? :
Picture 2:
public class ExpandListAdapter extends BaseExpandableListAdapter {
public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/**
* No child could be selected
*/
public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/**
* One single choice per group
*/
public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/**
* One single choice for all the groups
*/
public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
private Context context;
private ArrayList<Group> groups;
private ArrayList<ArrayList<Child>> child = new ArrayList();
private ArrayList<Child> listchild;
private GridAdapter adapter;
private CustomGridView gridView;
private SparseArray<SparseBooleanArray> checkedPositions;
private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();
private int choiceMode = CHOICE_MODE_MULTIPLE;
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
checkedPositions = new SparseArray<SparseBooleanArray>();
child = new ArrayList();
for (int i = 0; i < groups.size(); i++) {
child.add(i, groups.get(i).getItems());
}
}
public ExpandListAdapter(Context context, ArrayList<Group> children, int choiceMode) {
this(context, children);
// For now the choice mode CHOICE_MODE_MULTIPLE_MODAL
// is not implemented
if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
}
this.choiceMode = choiceMode;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
listchild = new ArrayList<Child>();
for (int j = 0; j < groups.get(groupPosition).getItems().size(); j++) {
listchild.add(child.get(groupPosition).get(j));
}
gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(true);
adapter = new GridAdapter(context, listchild, checkedPositions, groupPosition, childPosition);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setClicked(groupPosition, childPosition);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
if(!isChecked){
}
} else {
System.out.println("false");
}
}
});
return convertView;
}
public void setClicked(int groupPosition, int childPosition) {
switch (choiceMode) {
case CHOICE_MODE_MULTIPLE:
SparseBooleanArray checkedChildPositionsMultiple = checkedPositions.get(groupPosition);
// if in the group there was not any child checked
if (checkedChildPositionsMultiple == null) {
checkedChildPositionsMultiple = new SparseBooleanArray();
// By default, the status of a child is not checked
// So a click will enable it
checkedChildPositionsMultiple.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsMultiple);
} else {
boolean oldState = checkedChildPositionsMultiple.get(childPosition);
checkedChildPositionsMultiple.put(childPosition, !oldState);
}
break;
// TODO: Implement it
case CHOICE_MODE_MULTIPLE_MODAL:
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
case CHOICE_MODE_NONE:
checkedPositions.clear();
break;
case CHOICE_MODE_SINGLE_PER_GROUP:
SparseBooleanArray checkedChildPositionsSingle = checkedPositions.get(groupPosition);
// If in the group there was not any child checked
if (checkedChildPositionsSingle == null) {
checkedChildPositionsSingle = new SparseBooleanArray();
// By default, the status of a child is not checked
checkedChildPositionsSingle.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsSingle);
} else {
boolean oldState = checkedChildPositionsSingle.get(childPosition);
// If the old state was false, set it as the unique one which is true
if (!oldState) {
checkedChildPositionsSingle.clear();
checkedChildPositionsSingle.put(childPosition, !oldState);
} // Else does not allow the user to uncheck it
}
break;
// This mode will remove all the checked positions from other groups
// and enable just one from the selected group
case CHOICE_MODE_SINGLE_ABSOLUTE:
checkedPositions.clear();
SparseBooleanArray checkedChildPositionsSingleAbsolute = new SparseBooleanArray();
checkedChildPositionsSingleAbsolute.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsSingleAbsolute);
break;
}
// Notify that some data has been changed
notifyDataSetChanged();
Log.v(LOG_TAG, "List position updated");
Log.v(LOG_TAG, PrintSparseArrays.sparseArrayToString(checkedPositions));
}
public void setChoiceMode(int choiceMode) {
this.choiceMode = choiceMode;
// For now the choice mode CHOICE_MODEL_MULTIPLE_MODAL
// is not implemented
if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
}
checkedPositions.clear();
Log.v(LOG_TAG, "The choice mode has been changed. Now it is " + this.choiceMode);
}
@Override
public int getChildrenCount(int nGroup) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
GridAdapter:
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Child> child;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private SparseArray<SparseBooleanArray> checkedPositions;
int groupPosition, childPosition;
public GridAdapter(Context context, ArrayList<Child> childValues, SparseArray<SparseBooleanArray> checkedPositions, int groupPosition, int childPosition) {
mContext = context;
child = childValues;
this.checkedPositions = checkedPositions;
this.childPosition = childPosition;
this.groupPosition = groupPosition;
}
@Override
public int getCount() {
return child.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
NetworkImageView i;
childPosition = position;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
i = (NetworkImageView) convertView
.findViewById(R.id.flag);
i.setImageUrl(String.valueOf(child.get(childPosition).getImage()), imageLoader);
convertView.setTag(holder);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
i.setBackgroundResource(R.color.bg);
if(!isChecked){
}
} else {
System.out.println("false");
}
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.text.setText(child.get(position).getName());
return convertView;
}
static class ViewHolder {
TextView text;
}
}
In My Assumption
ExpandListAdapter Class
In getChildView() method
childPosition
does not depends on what country flag is being clicked , Because childPosition here returns the position of the Grid view, not the flags under itSo, no need to pass
childPosition
in constructor of GridView ,since childPosition will always return 1To get actual flag clicked position Use onClickListener inside your GridAdapter like:
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
NetworkImageView i;
childPosition = position;
if (convertView == null) {
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnclickListener(new OnClickListener{
@Override
onClick(View v){
isChecked=child.get(childPosition).isChecked();
if(!isChecked){
child.get(childPosition).setChecked(true);
}
else {
child.get(childPosition).setChecked(false);
}
});
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.text.setText(child.get(position).getName());
return convertView;
}
and remove your Check logic from ExpandListAdapter Class :
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setClicked(groupPosition, childPosition);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
if(!isChecked){
}
} else {
System.out.println("false");
}
}
});
这篇关于GridView控件内ExpandableListView有多个选择在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!