[英]Expandable List View in Android
我有一個Expandable ListView,但遇到兩個問題。 Q1。 如果標題不包含任何子項,我想將其從列表中隱藏/刪除。 為此,我在getGroupView方法內部。
if ( getChildrenCount( groupPosition ) == 0 )
{
convertView.setVisibility(View.GONE);
}
假設我有10個標頭,標頭3,5和8沒有子級。 當我使用上面的代碼時,它隱藏了第3、5、8個標題,但問題是它在那里留有空白,並且看起來不像列表。 所以有什么主意如何使其看起來像列表嗎?
Q2。 當標題沒有任何子代時,我想顯示一條吐司消息,說“數據不可用”。 為此,我在getGroupView方法中使用了以下代碼,
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if ( getChildrenCount( groupPosition ) == 0 )
Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();
}
});
這顯示了吐司消息,但是這里的問題是-當我單擊帶有孩子頭的標題時,它們沒有展開。 如何解決這個問題呢?
這是填充數據集的代碼
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
listDataHeader.add(dayName_first);
listDataHeader.add(dayName_second);
listDataHeader.add(dayName_third);
listDataHeader.add(dayName_fourth);
listDataHeader.add(dayName_fifth);
listDataHeader.add(dayName_sixth);
listDataHeader.add(dayName_seventh);
firstDay = checkEmpty(firstDay); // Function to check whether the list is empty or not. Do I need to check each list here and add or somewhere else?
listDataChild.put(listDataHeader.get(0), firstDay);
listDataChild.put(listDataHeader.get(1), secondDay);
listDataChild.put(listDataHeader.get(2), thirdDay);
listDataChild.put(listDataHeader.get(3), fourthDay);
listDataChild.put(listDataHeader.get(4), fifthDay);
listDataChild.put(listDataHeader.get(5), sixthDay);
listDataChild.put(listDataHeader.get(6), seventhDay);
listAdapter = new StaffExpandableListAdapter(this, listDataHeader,
listDataChild);
llExpandable.invalidateViews();
llExpandable.setAdapter(listAdapter);
首先讓我回答第二個問題,因為這更簡單。
問題是,當您設置自定義單擊偵聽器時,它將覆蓋列表的默認之一。
因此,您需要做的是首先檢查孩子人數。 如果計數為零,則僅添加偵聽器。
if ( getChildrenCount( groupPosition ) == 0 )
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();
}
});
現在,回到第一個問題。 我假設您有一個存儲所有元素的數據集。 在為可擴展列表視圖創建適配器之前,請遍歷數據集並刪除所有沒有任何子元素的元素。 之后,從數據集中創建適配器。
試試這個代碼
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
使用以下鏈接http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/如果有用,請接受回答...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.