团体和孩子的Expandablelistview不同的图像 [英] Expandablelistview different images for groups and childs
问题描述
我有基本的 expandablelistview
,并想知道如何让每个孩子和组有不同的图像。我是否需要以某种方式修改扩展列表适配器?任何帮助,想法,意见将AP preciated。感谢:)
MainActivity:
公共类MainActivity延伸活动{
.....
公共静态最终整数[]的图像= {
R.drawable.vegetables,
R.drawable.fruits,
R.drawable.drinks,
R.drawable.dessert
};
@覆盖
保护无效的onCreate(包savedInstanceState){
}
}
我的适配器
公共类ExpandableListAdapter扩展BaseExpandableListAdapter {
@覆盖
公共查看getChildView(INT groupPosition,最终诠释childPosition,布尔isLastChild,查看convertView,
ViewGroup中父){
最后弦乐childText =(字符串)getChild(groupPosition,childPosition);
如果(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);
ImageView的imgListChild =(ImageView的)convertView
.findViewById(R.id.IV_childImage);
//我可以在二维数组某种方式存储的图像,然后根据所得出的参考ID。
// imgListChild.setImageResource();
返回convertView;
}
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,ViewGroup中父){
字符串headerTitle =(字符串)getGroup(groupPosition);
如果(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);
ImageView的imgListGroup =(ImageView的)convertView
.findViewById(R.id.IV_group);
imgListGroup.setImageResource(MainActivity.images [groupPosition]);
返回convertView;
}
@覆盖
公共布尔hasStableIds(){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
// TODO自动生成方法存根
返回true;
}
}
添加的ImageView
在 list_group
和 list_item可
XML文件,并设置图像到您的适配器类的ImageViews。
像文本要设置为的TextView
,在你可以设置图片为的ImageView以同样的方式
TextView的txtListChild =(TextView中)convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
ImageView的imgListChild =(ImageView的)convertView
.findViewById(R.id.image);
imgListChild.setImageResource(R.drawable.imagename);
和XML文件应该是
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< CheckedTextView的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / lblListHeader
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =机器人:ATTR /列表preferredItemHeight
机器人:layout_marginLeft =8DP
机器人:drawableRight =@可绘制/ ic_launcher
机器人:重力=center_horizontal
机器人:以下属性来=32dp
机器人:paddingTop =8DP
机器人:文本=测试
机器人:TEXTSIZE =20SP
机器人:textAlignment =textEnd
机器人:TEXTSTYLE =黑体>
< / CheckedTextView>
< ImageView的
机器人:ID =@ + ID / IV_group
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:contentDescription =群体形象
/>
< / LinearLayout中>
编辑:
有很多方法来存储图像路径\资源IDs.The简单的方法是将它们存储在的ArrayList
和的HashMap
。
声明一个新的ArrayList和HashMap的存储资源的ID为集团和子项。
公开名单<整数GT; groupImages;
公众的HashMap<整数,列表和LT;整数GT;> childImages;
填充它,在你的 prepareListData()
的功能。
groupImages =新的ArrayList<整数GT;();
groupImages.add(R.drawable.vegetables);
groupImages.add(R.drawable.fruits);
groupImages.add(R.drawable.drinks);
groupImages.add(R.drawable.deserts);
childImages =新的HashMap<整数,列表和LT;整数GT;>();
名单<整数GT; vegetablesi =新的ArrayList<整数GT;();
vegetablesi.add(R.drawable.Tomatoes);
vegetablesi.add(R.drawable.Potatoes);
vegetablesi.add(R.drawable.Cucumbers);
vegetablesi.add(R.drawable.Pumpkins);
vegetablesi.add(R.drawable.Peppers);
vegetablesi.add(R.drawable.Onions);
vegetablesi.add(R.drawable.Garlic);
名单<整数GT; fruitsi =新的ArrayList<整数GT;();
fruitsi.add(R.drawable.Strawberries);
fruitsi.add(R.drawable.Blackcurrants);
fruitsi.add(R.drawable.Redcurrant);
fruitsi.add(R.drawable.Gooseberry);
fruitsi.add(R.drawable.Kiwifruit);
fruitsi.add(R.drawable.Grape);
名单<整数GT; drinksi =新的ArrayList<整数GT;();
drinksi.add(R.drawable.Vodka);
drinksi.add(R.drawable.Milk);
drinksi.add(R.drawable.Water);
drinksi.add(R.drawable.CocaCola);
drinksi.add(R.drawable.Sprite);
名单<整数GT; desertsi =新的ArrayList<整数GT;();
desertsi.add(R.drawable.Vodka);
desertsi.add(R.drawable.Milk);
desertsi.add(R.drawable.Water);
desertsi.add(R.drawable.CocaCola);
desertsi.add(R.drawable.Sprite);
childImages.put(groupImages.get(0),vegetablesi);
childImages.put(groupImages.get(1),fruitsi);
childImages.put(groupImages.get(2),drinksi);
childImages.put(groupImages.get(3),desertsi);
在 getGroupView
和 getChildView
您可以访问这些图片这样的功能:
@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,ViewGroup中父){
字符串headerTitle =(字符串)getGroup(groupPosition);
如果(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);
ImageView的ImageView的=(ImageView的)convertView.findViewById(R.id.IV_group);
INT imageId = this.groupImages.get(groupPosition);
imageView.setImageResource(imageId);
返回convertView;
}
@覆盖
公共查看getChildView(INT groupPosition,最终诠释childPosition,布尔isLastChild,查看convertView,
ViewGroup中父){
最后弦乐childText =(字符串)getChild(groupPosition,childPosition);
如果(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);
ImageView的ImageView的=(ImageView的)convertView.findViewById(R.id.IV_child);
INT imageId = this.childImages.get(this.groupImages.get(groupPosition))得到(childPosition)。
imageView.setImageResource(imageId);
返回convertView;
}
I have basic expandablelistview
and would like to know how to make each child and group have different images. Do I need to somehow modify expandable list adapter? Any help, thoughts, comments will be appreciated. Thanks :)
MainActivity:
public class MainActivity extends Activity{
.....
public static final Integer[] images = {
R.drawable.vegetables,
R.drawable.fruits,
R.drawable.drinks,
R.drawable.dessert
};
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}
My adapter
public class ExpandableListAdapter extends BaseExpandableListAdapter{
@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);
ImageView imgListChild= (ImageView) convertView
.findViewById(R.id.IV_childImage);
// I could store images somehow in two dimensional array and then depending on that draw their reference id.
// imgListChild.setImageResource();
return convertView;
}
@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);
ImageView imgListGroup= (ImageView) convertView
.findViewById(R.id.IV_group);
imgListGroup.setImageResource(MainActivity.images[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Add an ImageView
in list_group
and list_item
xml files and set image to those ImageViews in your Adapter Class.
Like you are setting the text to TextView
, in the same way you can set the image to ImageView
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
ImageView imgListChild = (ImageView) convertView
.findViewById(R.id.image);
imgListChild.setImageResource(R.drawable.imagename);
and your xml file should be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginLeft="8dp"
android:drawableRight="@drawable/ic_launcher"
android:gravity="center_horizontal"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="20sp"
android:textAlignment="textEnd"
android:textStyle="bold" >
</CheckedTextView>
<ImageView
android:id="@+id/IV_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="group image"
/>
</LinearLayout>
EDIT:
There are many ways to store image paths\ Resource IDs.The simplest way is to store them in ArrayList
and HashMap
.
Declare a new ArrayList and HashMap to store Resource IDs for Group and Child items.
public List<Integer> groupImages;
public HashMap<Integer, List<Integer>> childImages;
populate it in your prepareListData()
function.
groupImages= new ArrayList<Integer>();
groupImages.add(R.drawable.vegetables);
groupImages.add(R.drawable.fruits);
groupImages.add(R.drawable.drinks);
groupImages.add(R.drawable.deserts);
childImages = new HashMap<Integer, List<Integer>>();
List<Integer> vegetablesi = new ArrayList<Integer>();
vegetablesi.add(R.drawable.Tomatoes);
vegetablesi.add(R.drawable.Potatoes);
vegetablesi.add(R.drawable.Cucumbers);
vegetablesi.add(R.drawable.Pumpkins);
vegetablesi.add(R.drawable.Peppers);
vegetablesi.add(R.drawable.Onions);
vegetablesi.add(R.drawable.Garlic);
List<Integer> fruitsi = new ArrayList<Integer>();
fruitsi.add(R.drawable.Strawberries);
fruitsi.add(R.drawable.Blackcurrants);
fruitsi.add(R.drawable.Redcurrant);
fruitsi.add(R.drawable.Gooseberry);
fruitsi.add(R.drawable.Kiwifruit);
fruitsi.add(R.drawable.Grape);
List<Integer> drinksi = new ArrayList<Integer>();
drinksi.add(R.drawable.Vodka);
drinksi.add(R.drawable.Milk);
drinksi.add(R.drawable.Water);
drinksi.add(R.drawable.CocaCola);
drinksi.add(R.drawable.Sprite);
List<Integer> desertsi = new ArrayList<Integer>();
desertsi.add(R.drawable.Vodka);
desertsi.add(R.drawable.Milk);
desertsi.add(R.drawable.Water);
desertsi.add(R.drawable.CocaCola);
desertsi.add(R.drawable.Sprite);
childImages.put(groupImages.get(0), vegetablesi);
childImages.put(groupImages.get(1), fruitsi);
childImages.put(groupImages.get(2), drinksi);
childImages.put(groupImages.get(3), desertsi);
In getGroupView
and getChildView
functions you can access those images in this way:
@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);
ImageView imageView = (ImageView) convertView.findViewById(R.id.IV_group);
int imageId = this.groupImages.get(groupPosition);
imageView.setImageResource(imageId);
return convertView;
}
@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);
ImageView imageView = (ImageView) convertView.findViewById(R.id.IV_child);
int imageId = this.childImages.get(this.groupImages.get(groupPosition)).get(childPosition);
imageView.setImageResource(imageId);
return convertView;
}
这篇关于团体和孩子的Expandablelistview不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!