グループを閉じたり、選択した要素をスクロールしたりした後に選択範囲が消えないように、選択可能な要素(子)を使用してExpandableListViewを作成しようとしています。これに対するネイティブソリューションが見つからなかったため、要素の選択された状態を別のリストに保存し、onChildClickListnerで要素の状態と色を変更しようとしました。問題は-Listnerを動作させることができないということです。中には入りません。ただし、各子に個別にListnerを設定できます。これは機能しますが、私の問題は解決しません。この場合、グループ内の子供の位置に関する情報が得られないためです。それはおそらく小さな間違いです..ここにコードがあります:
psいくつかの変数名はドイツ語です、申し訳ありません=)ppsヒントをいただければ幸いです!
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;
}
}
}