Android自定义expandeablelistivew没有xml布局
我按照下面的代码在 android 中使用 xml 布局创建 ExpandablistView。它工作正常,但我不知道如果我在子/组布局中使用图像,如何更改图像? 我已经完成了在子/组视图上添加 ImageView 的代码,但是如何刷新/更改在子/组视图上使用 ImageView 发布的图像。 。 预先感谢您的指导
公共类 GundamExpAdapter 扩展了 BaseExpandableListAdapter { /-------------------------- 字段 ------------------- -------/
private final HashMap<String, ArrayList<String>> myData = new HashMap<String, ArrayList<String>>();
private final HashMap<Integer, String> lookUp = new HashMap<Integer, String>();
private final Context context;
/*-------------------------- Public --------------------------*/
public GundamExpAdapter(final Context con)
{
context = con;
}
public boolean AddGroup(final String groupName, final ArrayList<String> list)
{
final ArrayList<String> prev = myData.put(groupName, list);
if (prev != null)
return false;
lookUp.put(myData.size() - 1, groupName);
notifyDataSetChanged();
return true;
}
@Override
public Object getChild(int groupPos, int childPos)
{
if (lookUp.containsKey(groupPos))
{
final String str = lookUp.get(groupPos);
final ArrayList<String> data = myData.get(str);
return data.get(childPos);
}
return null;
}
@Override
public long getChildId(int groupPos, int childPos)
{
return 0;
}
@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild,
View convertView, ViewGroup parent)
{
LinearLayout linear = new LinearLayout(context);
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
// Indent
final String str = "\t\t\t" + (String)getChild(groupPos, childPos);
linear = new LinearLayout(context);
linear.setOrientation(LinearLayout.VERTICAL);
text.setLayoutParams(params);
text.setText(str);
linear.addView(text);
return linear;
}
@Override
public int getChildrenCount(int groupPos)
{
if (lookUp.containsKey(groupPos))
return myData.get(lookUp.get(groupPos)).size();
return 0;
}
@Override
public Object getGroup(int groupPos)
{
if (lookUp.containsKey(groupPos))
return myData.get(lookUp.get(groupPos));
return null;
}
@Override
public int getGroupCount()
{
return myData.size();
}
@Override
public long getGroupId(int groupPos)
{
return 0;
}
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent)
{
LinearLayout linear = new LinearLayout(context);
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
// Push the group name slightly to the right for drop down icon
final String str = "\t\t" + lookUp.get(groupPos);
linear = new LinearLayout(context);
linear.setOrientation(LinearLayout.VERTICAL);
text.setLayoutParams(params);
text.setText(str);
text.setTextSize(18.0f);
linear.addView(text);
return linear;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos)
{
return false;
}
} 其活动如下
public class Main extends ExpandableListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
GundamExpAdapter gea = new GundamExpAdapter(this);
ArrayList<String> models = new ArrayList<String>();
models.add("NZ 666 Kshatriya");
models.add("Unicorn");
models.add("Sinanju");
gea.AddGroup("Unicorn", models);
models = new ArrayList<String>();
models.add("DeathScythe");
models.add("Altron");
models.add("HeavyArms");
models.add("SandRock");
models.add("Epyon");
models.add("ZERO");
gea.AddGroup("Wing", models);
setListAdapter(gea);
ExpandableListView elv = getExpandableListView();
elv.setTextFilterEnabled(true);
//listview.setOnItemClickListener(OnClickingListItem());
}
}
I am following below code to create expandablistView in android withour xml layout.its working fine,but I don't know how I can change an image If I use an image in child/group layout ?
I already done code to adding the ImageView on childes/group views but how I can refresh/change the image posted using ImageView on child/group view . .
Thanks in advance for guide
public class GundamExpAdapter extends BaseExpandableListAdapter
{
/-------------------------- Fields --------------------------/
private final HashMap<String, ArrayList<String>> myData = new HashMap<String, ArrayList<String>>();
private final HashMap<Integer, String> lookUp = new HashMap<Integer, String>();
private final Context context;
/*-------------------------- Public --------------------------*/
public GundamExpAdapter(final Context con)
{
context = con;
}
public boolean AddGroup(final String groupName, final ArrayList<String> list)
{
final ArrayList<String> prev = myData.put(groupName, list);
if (prev != null)
return false;
lookUp.put(myData.size() - 1, groupName);
notifyDataSetChanged();
return true;
}
@Override
public Object getChild(int groupPos, int childPos)
{
if (lookUp.containsKey(groupPos))
{
final String str = lookUp.get(groupPos);
final ArrayList<String> data = myData.get(str);
return data.get(childPos);
}
return null;
}
@Override
public long getChildId(int groupPos, int childPos)
{
return 0;
}
@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild,
View convertView, ViewGroup parent)
{
LinearLayout linear = new LinearLayout(context);
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
// Indent
final String str = "\t\t\t" + (String)getChild(groupPos, childPos);
linear = new LinearLayout(context);
linear.setOrientation(LinearLayout.VERTICAL);
text.setLayoutParams(params);
text.setText(str);
linear.addView(text);
return linear;
}
@Override
public int getChildrenCount(int groupPos)
{
if (lookUp.containsKey(groupPos))
return myData.get(lookUp.get(groupPos)).size();
return 0;
}
@Override
public Object getGroup(int groupPos)
{
if (lookUp.containsKey(groupPos))
return myData.get(lookUp.get(groupPos));
return null;
}
@Override
public int getGroupCount()
{
return myData.size();
}
@Override
public long getGroupId(int groupPos)
{
return 0;
}
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent)
{
LinearLayout linear = new LinearLayout(context);
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
// Push the group name slightly to the right for drop down icon
final String str = "\t\t" + lookUp.get(groupPos);
linear = new LinearLayout(context);
linear.setOrientation(LinearLayout.VERTICAL);
text.setLayoutParams(params);
text.setText(str);
text.setTextSize(18.0f);
linear.addView(text);
return linear;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos)
{
return false;
}
}
And its activity as below
public class Main extends ExpandableListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
GundamExpAdapter gea = new GundamExpAdapter(this);
ArrayList<String> models = new ArrayList<String>();
models.add("NZ 666 Kshatriya");
models.add("Unicorn");
models.add("Sinanju");
gea.AddGroup("Unicorn", models);
models = new ArrayList<String>();
models.add("DeathScythe");
models.add("Altron");
models.add("HeavyArms");
models.add("SandRock");
models.add("Epyon");
models.add("ZERO");
gea.AddGroup("Wing", models);
setListAdapter(gea);
ExpandableListView elv = getExpandableListView();
elv.setTextFilterEnabled(true);
//listview.setOnItemClickListener(OnClickingListItem());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要更改数据才能知道哪一行将具有不同的图片。
然后您可以调用 BaseExpandableListAdapter.notifyDataSetChanged() ,这将产生在每一行上再次调用 getChildView 的效果。然后,您将能够在要更改图像的每个图像视图上
setBackgroundDrawable()
。First you will need to change your data in order to know which row will have a different picture.
Then you can call
BaseExpandableListAdapter.notifyDataSetChanged()
which will have the effect of calling getChildView again on each row. Then you will be able tosetBackgroundDrawable()
on each image view you want to change the image.