我正在尝试使用可选元素(子元素)创建一个ExpandableListView,以便在关闭组或滚动选定元素后,选择不会消失。 我没有找到任何本机解决方案,所以我尝试将元素的选定状态保存在单独的列表中,并更改onChildClickListner中元素的状态和颜色。问题是 - 我无法让Listner工作。它只是不会进去。但是我可以单独为每个孩子设置一个Listner。这确实有效,但没有解决我的问题,因为在这种情况下,我没有得到关于孩子在群体中的位置的任何信息。 这可能只是一个小错误..这是代码:
P.S。一些变量名称是德语,抱歉=) p.p.s.我会感激任何提示!
package com.example.android.apis;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
public class ApiDemos extends ExpandableListActivity {
// This one works just fine if i set it for each child
private View.OnClickListener cl1 = new View.OnClickListener() {
public void onClick(View v) {
if (((TextView) v).isSelected() == false) {
((TextView) v).setSelected(true);
((TextView) v).setTextColor(RED);
} else {
((TextView) v).setSelected(false);
((TextView) v).setTextColor(WHITE);
}
}
};
// each child has a relation with one of these:
class veranstaltungView {
public String text;
public boolean marked;
public veranstaltungView(String txt) {
text = txt;
marked = false;
}
}
// This one is set on the whole ExpandableListView. It just doesnt get
// inside!
private OnChildClickListener cl = new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (((TextView) v).isSelected() == false) {
veranstaltungenList.get(groupPosition).get(childPosition).marked = true;
((TextView) v).setSelected(true);
((TextView) v).setTextColor(RED);
} else {
veranstaltungenList.get(groupPosition).get(childPosition).marked = false;
((TextView) v).setSelected(false);
((TextView) v).setTextColor(WHITE);
}
return false;
}
};
private static final int WHITE = 0xffffffff;
private static final int RED = 0xffbc0000;
ExpandableListAdapter mAdapter;
private ArrayList<ArrayList<veranstaltungView>> veranstaltungenList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// generate some elements
veranstaltungenList = new ArrayList<ArrayList<veranstaltungView>>();
for (int i = 0; i < 6; i++) {
ArrayList<veranstaltungView> semester = new ArrayList<veranstaltungView>();
for (int j = 0; j < 3; j++) {
semester.add(new veranstaltungView("Veranstaltung " + i + "."
+ j));
}
veranstaltungenList.add(semester);
}
// Create and set the adapter
ExpandableListView elv = getExpandableListView();
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
elv.setItemsCanFocus(false);
elv.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
elv.setOnChildClickListener(cl);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// The groups
private String[] semesterArray = { "SEMESTER1", "SEMESTER2",
"SEMESTER3", "SEMESTER4", "SEMESTER5", "SEMESTER6" };
public Object getChild(int groupPosition, int childPosition) {
return veranstaltungenList.get(groupPosition).get(childPosition).text;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return veranstaltungenList.get(groupPosition).size();
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ApiDemos.this);
textView.setTextColor(WHITE);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = null;
textView = getGenericView();
textView.setText((String) getChild(groupPosition, childPosition));
textView.setClickable(true);
textView.setSelected(veranstaltungenList.get(groupPosition).get(
childPosition).marked);
if (veranstaltungenList.get(groupPosition).get(childPosition).marked == true)
textView.setTextColor(RED);
else
textView.setTextColor(WHITE);
// textView.setOnClickListener(cl1);
return textView;
}
public Object getGroup(int groupPosition) {
return semesterArray[groupPosition];
}
public int getGroupCount() {
return veranstaltungenList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
答案 0 :(得分:2)
问题是 getChildView 方法中的“ textView.setClickable(true); ”,阻止了 OnClickListener 的执行。 “ if((TextView)v).isSelected()== false)”行也应改为“ veranstaltungenList.get(groupPosition).get(childPosition).marked < /强>”。 经过那些改变就像魅力一样!它可能不是最好的解决方案,但它有效。希望这有助于某人!