我需要幫助的以下問題:將按鈕添加到expendableListView去年兒童
有沒有辦法添加在「expendableListView」一個按鈕,最後一個孩子?
的按鈕應該是下面的 「短文本」 字段。
如果您需要更多信息,請留下評論,我會提供。
我需要幫助的以下問題:將按鈕添加到expendableListView去年兒童
有沒有辦法添加在「expendableListView」一個按鈕,最後一個孩子?
的按鈕應該是下面的 「短文本」 字段。
如果您需要更多信息,請留下評論,我會提供。
您的ExpandableListView
應該支持一些數據模型。在每個列表項行中,您將在View(TextView或Button)中顯示文本。所以首先讓自己保留此信息類:
public class RowData {
public static final int TYPE_TEXT = 0;
public static final int TYPE_BUTTON = 1;
private String label;
private int type;
public RowData(String label, int type) {
this.label = label;
this.type = type;
}
public RowData(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public int getType() {
return type;
}
}
最簡單的模型(因爲我們沒有你的代碼的理解),將是一個LinkedHashMap<String, List<RowData>>
。密鑰是指組數據(「00010」,「00020」,「00030」等),而附加到這些密鑰的每個值指的是組子組(例如「00010」),您將擁有[「Material Group:No信息發現「等)]。現在,在您的活動保持對它的引用這個數據模型:
private Map<String, List<RowData>> myData = new LinkedHashMap<String, List<RowData>>();
你需要保持到ExpandableListView的適配器參考:
private BaseExpandableListAdapter adapter = new CustomSpecificAdapter(yourActivityContext, myData);
當你需要添加額外的領域,你只要加入它的數據模型對象:
myData.get("00010").add(new RowData("Button value", RowData.TYPE_BUTTON));
或用於TextView
: myData.get( 「00010」)添加(新RowData( 「TextView的值」));。 並通知適配器:
adapter.notifyDataSetChanged();
這將更新可展開列表。但是,您需要爲每行指定您將擁有的視圖類型,您對每個位置的類型,因爲這與視圖回收非常相關。所以,你的相關CustomSpecificAdapter
方法可能看起來是這樣的:public class CustomSpecificAdapter extends BaseExpandableListAdapter
private LinkedHashMap<String, List<RowData>> modelData;
private Context context;
public CustomSpecificAdapter(Context context, LinkedHashMap<String, List<RowData>> modelData) {
this.context = context;
this.modelData = modelData;
}
private RowData getMyDataObj(int groupPosition, int childPosition) {
/**
* safe to do this since we are using a LinkedHashMap
* */
List<List<RowData>> children = new ArrayList<List<RowData>>(modelData.values());
return children.get(groupPosition).get(childPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return getMyDataObj(groupPosition, childPosition);
}
@Override
public int getChildTypeCount() {
/**
* since you will have a button and a textview there will be 2 types of views
* */
return 2;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
RowData data = getMyDataObj(groupPosition, childPosition);
if (data.getType() == RowData.TYPE_TEXT) {
return 0;
}
// for button
return 1;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
/**
* this is the intersting part
* */
RowData data = getMyDataObj(groupPosition, childPosition);
TextView myTv = null;
if(convertView == null) {
if(data.getType() == RowData.TYPE_BUTTON) {
convertView = // inflate from button layout, in fact it's best to have only a <Button ... /> in the XML layout
} else {
convertView = // inflate from TextView layout, in fact it's best to have only a <Button ... /> in the XML layout
}
}
/**
* Have the same id for both TextView and Button - Button extends from TextView and so setText() method is available through polimorphysm. Also no need for ViewHolder as getViewById() will refer to itself.
* */
myTv = (TextView) convertView.findViewById(the_same_id_in_both_xml_files);
myTv.setText(data.getLabel());
return convertView;
}
首先,非常感謝您的努力,我仍然遇到一些錯誤,但我已經掌握了基本知識。但爲什麼'myTv =(TextView)convertView.findViewById(textView1);'找不到我的視圖? –
您需要爲這兩種類型的佈局發佈XML佈局。它應該在那裏。 – gunar
有沒有一種方法來添加一個按鈕,最後一個孩子在 「expendableListView」?
我相信是的,你應該爲你需要的組中的最後一個孩子膨脹另一個視圖(包含按鈕的視圖)。
事情是這樣的:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
if(groupPosition == FIRST_GROUP_POSITION && isLasChild){
convertView = inflater.inflate(R.layout.child_layout_with_buttons, null);
}else{
convertView = inflater.inflate(R.layout.normal_child_layout, null);
}
}
//....
}
好的,到目前爲止,我得到一個按鈕作爲第一組的最後一個孩子。但是我想要一個按鈕作爲每個組的最後一個孩子(所以我只是使用了'if(isLastChild){//按鈕... else {// text'),但是按鈕現在是第二組中的倒數第二個,和第三最後第三組等等,你有什麼想法爲什麼? –
不,不好意思,不知道爲什麼。 –
首先,你需要實現自己的ExpandableListAdapter
,並實現getChildView
控制每個子行的看法。試試這個:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
if(isLastChild){
view = getLayoutInflater().inflate(R.layout.item_with_button, null);
}else{
view = getLayoutInflater().inflate(R.layout.item, null);
}
}
return view;
}
希望這會有所幫助。
您已經有每個組的文字瀏覽列表,您需要在每個列表下方的底部有一個按鈕? – gunar
是的,確切地說。「Textview元素」的數量是可變的。 –