Я направил это руководство для расширения ListView
http://www.androidhive.info/2013/07/and ... -tutorial/
Моя главная деятельность
BusActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List listDataHeader;
HashMap listDataChild,listDataStart,listDataEnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild,listDataStart,listDataEnd);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList();
listDataChild = new HashMap();
listDataStart = new HashMap();
listDataEnd = new HashMap();
// Adding child data
listDataHeader.add("Volvo");
listDataHeader.add("Deluxe");
listDataHeader.add("Express");
listDataHeader.add("Ordinary");
// Adding child data
List volvo = new ArrayList();
volvo.add("The Shawshank Redemption");
volvo.add("The Godfather");
volvo.add("The Godfather: Part II");
volvo.add("Pulp Fiction");
List nowShowing = new ArrayList();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
List now = new ArrayList();
now.add("The Conjuring");
now.add("Despicable Me 2");
now.add("Turbo");
now.add("Grown Ups 2");
List comingSoon = new ArrayList();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
List comingup = new ArrayList();
comingup.add("2 Guns");
comingup.add("The Smurfs 2");
comingup.add("The Spectacular Now");
comingup.add("The Canyons");
listDataChild.put(listDataHeader.get(0), volvo); // Header, Child data
listDataChild.put(listDataHeader.get(1), now);
listDataStart.put(listDataHeader.get(0), nowShowing); // Header, Child data
listDataStart.put(listDataHeader.get(1), comingup);
);
listDataEnd.put(listDataHeader.get(0), volvo); // Header, Child data
listDataEnd.put(listDataHeader.get(2), comingSoon);
}
< /code>
} < /p>
Мой адаптер < /p>
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap _listDataChild,listStart,listEnd;
public ExpandableListAdapter(Context context, List listDataHeader,
HashMap listChildData,HashMap listStart, HashMap listEnd) {
this._context = context;
this._listDataHeader = listDataHeader;
System.out.println("Start"+listStart+"Bus"+listChildData+"End"+listEnd);
this._listDataChild = listChildData;
this.listStart=listStart;
this.listEnd=listEnd;
}
@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);
System.out.println("Child Text"+childText);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListBus= (TextView) convertView
.findViewById(R.id.txt_busno);
if(groupPosition==0)
{
//txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
//txtListBus.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
txtListBus.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==2)
{
txtListBus.setBackgroundColor(Color.RED);
txtListBus.setTextColor(Color.BLACK);
}
else
{
txtListBus.setBackgroundColor(Color.BLUE);
txtListBus.setTextColor(Color.WHITE);
}
txtListBus.setText(childText);
TextView txtList= (TextView) convertView
.findViewById(R.id.txt_start);
if(groupPosition==0)
{
txtList.setBackgroundColor(Color.parseColor("#FED966"));
txtList.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txtList.setBackgroundColor(Color.parseColor("#64D8CD"));
txtList.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
txtList.setBackgroundColor(Color.RED);
txtList.setTextColor(Color.BLACK);
}
else
{
txtList.setBackgroundColor(Color.BLUE);
txtList.setTextColor(Color.WHITE);
}
txtList.setText(childText);
TextView txt= (TextView) convertView
.findViewById(R.id.txt_end);
if(groupPosition==0)
{
txt.setBackgroundColor(Color.parseColor("#FED966"));
txt.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txt.setBackgroundColor(Color.parseColor("#64D8CD"));
txt.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
txt.setBackgroundColor(Color.RED);
txt.setTextColor(Color.BLACK);
}
else
{
txt.setBackgroundColor(Color.BLUE);
txt.setTextColor(Color.WHITE);
}
txt.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);
System.out.println("Group position"+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.ls_head);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
if(groupPosition==0)
{
lblListHeader.setBackgroundColor(Color.parseColor("#FFD243"));
lblListHeader.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
lblListHeader.setBackgroundColor(Color.parseColor("#00BFAC"));
lblListHeader.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
lblListHeader.setBackgroundColor(Color.RED);
lblListHeader.setTextColor(Color.BLACK);
}
else
{
lblListHeader.setBackgroundColor(Color.BLUE);
lblListHeader.setTextColor(Color.WHITE);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
< /code>
} < /p>
list_item mayout < /p>
< /code>
< /p>
list_group остается такой же, как и ссылка < /p>
В этом списке дочерних людей есть только текстовый обзор. Я хочу добавить ImageView и TextView в список детей. Я могу добавить это, изменив List_item.xml, чтобы сдержать представления. Как я могу добавить данные в этот представление ребенка, в котором есть несколько представлений?
Подробнее здесь: https://stackoverflow.com/questions/190 ... iple-views
Как добавить данные в расширяемый ListView с ребенком, у которого есть несколько просмотров ⇐ Android
Форум для тех, кто программирует под Android
1757019910
Anonymous
Я направил это руководство для расширения ListView
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Моя главная деятельность
BusActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List listDataHeader;
HashMap listDataChild,listDataStart,listDataEnd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild,listDataStart,listDataEnd);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList();
listDataChild = new HashMap();
listDataStart = new HashMap();
listDataEnd = new HashMap();
// Adding child data
listDataHeader.add("Volvo");
listDataHeader.add("Deluxe");
listDataHeader.add("Express");
listDataHeader.add("Ordinary");
// Adding child data
List volvo = new ArrayList();
volvo.add("The Shawshank Redemption");
volvo.add("The Godfather");
volvo.add("The Godfather: Part II");
volvo.add("Pulp Fiction");
List nowShowing = new ArrayList();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
List now = new ArrayList();
now.add("The Conjuring");
now.add("Despicable Me 2");
now.add("Turbo");
now.add("Grown Ups 2");
List comingSoon = new ArrayList();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
List comingup = new ArrayList();
comingup.add("2 Guns");
comingup.add("The Smurfs 2");
comingup.add("The Spectacular Now");
comingup.add("The Canyons");
listDataChild.put(listDataHeader.get(0), volvo); // Header, Child data
listDataChild.put(listDataHeader.get(1), now);
listDataStart.put(listDataHeader.get(0), nowShowing); // Header, Child data
listDataStart.put(listDataHeader.get(1), comingup);
);
listDataEnd.put(listDataHeader.get(0), volvo); // Header, Child data
listDataEnd.put(listDataHeader.get(2), comingSoon);
}
< /code>
} < /p>
Мой адаптер < /p>
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap _listDataChild,listStart,listEnd;
public ExpandableListAdapter(Context context, List listDataHeader,
HashMap listChildData,HashMap listStart, HashMap listEnd) {
this._context = context;
this._listDataHeader = listDataHeader;
System.out.println("Start"+listStart+"Bus"+listChildData+"End"+listEnd);
this._listDataChild = listChildData;
this.listStart=listStart;
this.listEnd=listEnd;
}
@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);
System.out.println("Child Text"+childText);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListBus= (TextView) convertView
.findViewById(R.id.txt_busno);
if(groupPosition==0)
{
//txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
//txtListBus.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txtListBus.setBackgroundColor(Color.parseColor("#FED966"));
txtListBus.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==2)
{
txtListBus.setBackgroundColor(Color.RED);
txtListBus.setTextColor(Color.BLACK);
}
else
{
txtListBus.setBackgroundColor(Color.BLUE);
txtListBus.setTextColor(Color.WHITE);
}
txtListBus.setText(childText);
TextView txtList= (TextView) convertView
.findViewById(R.id.txt_start);
if(groupPosition==0)
{
txtList.setBackgroundColor(Color.parseColor("#FED966"));
txtList.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txtList.setBackgroundColor(Color.parseColor("#64D8CD"));
txtList.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
txtList.setBackgroundColor(Color.RED);
txtList.setTextColor(Color.BLACK);
}
else
{
txtList.setBackgroundColor(Color.BLUE);
txtList.setTextColor(Color.WHITE);
}
txtList.setText(childText);
TextView txt= (TextView) convertView
.findViewById(R.id.txt_end);
if(groupPosition==0)
{
txt.setBackgroundColor(Color.parseColor("#FED966"));
txt.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
txt.setBackgroundColor(Color.parseColor("#64D8CD"));
txt.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
txt.setBackgroundColor(Color.RED);
txt.setTextColor(Color.BLACK);
}
else
{
txt.setBackgroundColor(Color.BLUE);
txt.setTextColor(Color.WHITE);
}
txt.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);
System.out.println("Group position"+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.ls_head);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
if(groupPosition==0)
{
lblListHeader.setBackgroundColor(Color.parseColor("#FFD243"));
lblListHeader.setTextColor(Color.parseColor("#A07900"));
}
else if(groupPosition==1)
{
lblListHeader.setBackgroundColor(Color.parseColor("#00BFAC"));
lblListHeader.setTextColor(Color.parseColor("#067B6F"));
}
else if(groupPosition==2)
{
lblListHeader.setBackgroundColor(Color.RED);
lblListHeader.setTextColor(Color.BLACK);
}
else
{
lblListHeader.setBackgroundColor(Color.BLUE);
lblListHeader.setTextColor(Color.WHITE);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
< /code>
} < /p>
list_item mayout < /p>
< /code>
< /p>
list_group остается такой же, как и ссылка < /p>
В этом списке дочерних людей есть только текстовый обзор. Я хочу добавить ImageView и TextView в список детей. Я могу добавить это, изменив List_item.xml, чтобы сдержать представления. Как я могу добавить данные в этот представление ребенка, в котором есть несколько представлений?
Подробнее здесь: [url]https://stackoverflow.com/questions/19044216/how-to-add-data-to-the-expandable-listview-with-child-having-multiple-views[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия