[英]expandable listview with custom Child
是否可以僅在可擴展父級的一個子級中添加Button?
像貝婁圖像:
編輯:
好的,這是通過適配器完成此操作的方法:
static class ViewHolder {
TextView textView;
Button button1, button2;
}
@Override
public int getChildTypeCount() {
return 2;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
if (childPosition == 0)
return 0;
else
return 1;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
if (childPosition == 0) {
// 1st view specifics here
convertView = mInflater.inflate(R.layout.exp_list_child_2, parent, false);
holder.button1 = (Button) convertView.findViewById(R.id.button1);
holder.button2 = (Button) convertView.findViewById(R.id.button2);
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "Some action here", Toast.LENGTH_SHORT).show();
}
});
} else {
// Other views
convertView = mInflater.inflate(R.layout.exp_list_child, parent, false);
}
holder.textView = (TextView) convertView.findViewById(R.id.childItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText("Use your data here for other items");
return convertView;
}
這里要注意的幾件事:
getChildTypeCount
和getChildType
@ FrankN.Stein提供的解決方案也有資格。 我不確定哪種是性能更好的(您必須自己進行測試)。 盡管如此,請記住,即使GONE視圖是不可見的,並且不占用布局中的任何空間,它們仍然存在於視圖層次結構和內存中。
編輯2 :
當適配器重新使用視圖時,它必須知道要獲取哪個視圖。 因此它調用帶有孩子位置的getChildType
並詢問:“這是什么類型”?
如果只有一個類型(默認為默認類型),則適配器無法區分它們,因此會在不應使用的位置重新使用您的第一個孩子布局。
這個答案有一張漂亮的圖片,解釋了如何重新使用視圖。
希望這可以解決問題:-)編碼愉快!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.