本文介绍了如何添加图像expandablelistivew?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用我的项目expandablelistview,我要为组视图中添加不同的图像,并在子视图我想要做的一样,但它不工作,以下是我的code ..

MainActivity.java

 公共类MainActivity延伸活动{

ExpandableTests listAdapter;
ExpandableListView expListView;
名单<字符串> listDataHeader;
HashMap的<字符串列表与LT;字符串>> listDataChild;

 私人列表<整数GT;组;

    私人的HashMap<整数,列表和LT;整数GT;>儿童;


Expandableimageadapter imgsadadapter;


公开名单<整数GT; groupImages;
公众的HashMap<整数,列表和LT;整数GT;> childImages;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    //获取列表视图
    expListView =(ExpandableListView)findViewById(R.id.lvExp);

    // preparing列表数据
    prepareListData();


    expListView.setGroupIndicator(空);

    // imgsadadapter =新Expandableimageadapter(这一点,groupImages,childImages);

// listAdapter =新ExpandableTests(这一点,listDataHeader,组,listDataChild,儿童);

    listAdapter =新ExpandableTests(这一点,listDataHeader,groupImages,listDataChild,childImages);

    //设置列表适配器
    expListView.setAdapter(listAdapter);
    //expListView.setAdapter(imgsadadapter);

    //列表视图组单击监听器
    expListView.setOnGroupClickListener(新OnGroupClickListener(){

        @覆盖
        公共布尔onGroupClick(ExpandableListView父,视图V,
                INT groupPosition,长ID){
            // Toast.makeText(getApplicationContext(),
            //集团已点击+ listDataHeader.get(groupPosition)
            // Toast.LENGTH_SHORT).show();
            返回false;
        }
    });

    //列表视图集团扩大监听器
    expListView.setOnGroupExpandListener(新OnGroupExpandListener(){

        @覆盖
        公共无效onGroupExpand(INT groupPosition){
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition)+膨胀,
                    Toast.LENGTH_SHORT).show();
        }
    });

    //列表视图组collasped监听器
    expListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){

        @覆盖
        公共无效onGroupCollapse(INT groupPosition){
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition)+坍塌,
                    Toast.LENGTH_SHORT).show();

        }
    });

    //列表视图儿童点击监听器
    expListView.setOnChildClickListener(新OnChildClickListener(){

        @覆盖
        公共布尔onChildClick(ExpandableListView父,视图V,
                INT groupPosition,诠释childPosition,长ID){
            // TODO自动生成方法存根
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            +:
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition))。得到(
                                    childPosition),Toast.LENGTH_SHORT)
                    。显示();
            返回false;
        }
    });
}

/ *
 * preparing列表数据
 * /
私人无效prepareListData(){



    groupImages =新的ArrayList<整数GT;();
    groupImages.add(R.drawable.adminsetting);
    groupImages.add(R.drawable.productcategory);
   // groupImages.add(R.drawable.drinks);
   // groupImages.add(R.drawable.deserts);

    childImages =新的HashMap<整数,列表和LT;整数GT;>();
    名单<整数GT; Maincat =新的ArrayList<整数GT;();
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
   // vegetablesi.add(R.drawable.Onions);
   // vegetablesi.add(R.drawable.Garlic);

    名单<整数GT; subcat =新的ArrayList<整数GT;();
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
   // 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),Maincat);
  // childImages.put(groupImages.get(1),subcat);
    childImages.put(0,Maincat);
    childImages.put(1,subcat);


    listDataHeader =新的ArrayList<字符串>();
    listDataChild =新的HashMap<字符串列表与LT;字符串>>();

    //添加子数据
    listDataHeader.add(管理设置);
    listDataHeader.add(产品&安培;类别);
    listDataHeader.add(客户和订单);

    //添加子数据
    名单<字符串> TOP250 =新的ArrayList<字符串>();
    top250.add(国城);
    top250.add(销售人员);
    top250.add(城销售人员);
    top250.add(产品Boxtype);
    top250.add(船承运人);
    //top250.add("The黑暗骑士);
// top250.add(12怒汉);

    名单<字符串> nowShowing =新的ArrayList<字符串>();
    nowShowing.add(产品);
    nowShowing.add(类别);
    nowShowing.add(类产品);
    nowShowing.add(产品分类);
    //nowShowing.add("Red 2);
    //nowShowing.add("The金刚狼);

    名单<字符串> comingSoon =新的ArrayList<字符串>();
    comingSoon.add(客户);
    comingSoon.add(客户端产品);
    comingSoon.add(客户订单);
    comingSoon.add(客户订单交货);
    //comingSoon.add("Europa报告);

    listDataChild.put(listDataHeader.get(0),TOP250); //头,子数据
    listDataChild.put(listDataHeader.get(1),nowShowing);
    listDataChild.put(listDataHeader.get(2),comingSoon);
}
 

ExpandableTests.java

 公共类ExpandableTests扩展BaseExpandableListAdapter {


      私人语境_context;

私人列表<整数GT; _组;

私人的HashMap<整数,列表和LT;整数GT;> _儿童;



私人列表<字符串> _listDataHeader; //标题标题
//子数据的标题标题格式,子标题
私人的HashMap<字符串列表与LT;字符串>> _listDataChild;

公共ExpandableTests(上下文的背景下,列表与LT;字符串> listDataHeader,名单,其中,整数基团,
        HashMap的<字符串列表与LT;字符串>> listDataChild,HashMap的<整数,列表和LT;整数GT;>儿童) {
    this._context =背景;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listDataChild;
    this._group =组;
    this._Child =儿童;
}


@覆盖
公共对象getChild(INT groupPosition,诠释childPosititon){
    返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
            获得(childPosititon);
    // this._Child.get(this._group.get(groupPosition))得到(childPosititon)。
}

@覆盖
众长getChildId(INT groupPosition,诠释childPosition){
    返回childPosition;
}

@覆盖
公共查看getChildView(INT groupPosition,最终诠释childPosition,
        布尔isLastChild,查看convertView,ViewGroup中父){

    最后弦乐childText =(字符串)getChild(groupPosition,childPosition);
//最终诠释childimg =(整数)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.childsid);

    最终诠释childimg = _Child.get(groupPosition)获得(childPosition);
    imgListChild.setImageResource(childimg);
    //imgListChild.setImageResource(childimg);

    / *的ImageView ImageView的=(ImageView的)convertView.findViewById(R.id.childsid);
    最终诠释imageId = this.childImages.get(this.groupImages.get(groupPosition))得到(childPosition)。
    imageView.setImageResource(imageId); * /

    返回convertView;
}

@覆盖
公众诠释getChildrenCount(INT groupPosition){
    返回this._listDataChild.get(this._listDataHeader.get(groupPosition))
            。尺寸()​​;
}

@覆盖
公共对象getGroup(INT groupPosition){
    返回this._listDataHeader.get(groupPosition);
}

@覆盖
公众诠释getGroupCount(){
    返回this._listDataHeader.size();
}

@覆盖
众长getGroupId(INT groupPosition){
    返回groupPosition;
}



@覆盖
公共查看getGroupView(INT groupPosition,布尔isExpanded,
        查看convertView,ViewGroup中父){
    字符串headerTitle =(字符串)getGroup(groupPosition);
    // INT headerimg =(整数)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(空,Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    ImageView的imgsListHeader =(ImageView的)convertView
            .findViewById(R.id.gropids);

    INT headerimg = _group.get(groupPosition);
    imgsListHeader.setImageResource(headerimg);
// lblListHeader.setTypeface(空,Typeface.BOLD);
    //imgsListHeader.setImageResource(R.drawable.ic_launcher);

    / *的ImageView ImageView的=(ImageView的)convertView.findViewById(R.id.gropids);
    INT imageId = this.groupImages.get(groupPosition);
    imageView.setImageResource(imageId); * /
    返回convertView;
}

@覆盖
公共布尔hasStableIds(){
    返回false;
}

@覆盖
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
    返回true;
}
 

解决方案

更​​换遵循getChildView与previous一行

 最终诠释childimg = _Child.get(groupPosition)获得(childPosition);
imgListChild.setImageResource(childimg);
 

更换遵循getGroupView与previous一行

  INT headerimg = _group.get(groupPosition);
imgsListHeader.setImageResource(headerimg);
 

替换以下行MainActivity

  listAdapter =新ExpandableTests(这一点,listDataHeader,groupImages,listDataChild,childImages);
 

再添加一个形象

  groupImages在prepareListData方法的ArrayList
 

替换以下行

  childImages.put(groupImages.get(0),Maincat);
 childImages.put(groupImages.get(1),subcat);
 

  childImages.put(0,Maincat);
 childImages.put(1,subcat);
 

对于指示器图标

添加下面的ExpandableTests在getGroupView()行

  ImageView的imgIndicator =(ImageView的)convertView.findViewById(R.id.indicator);
        如果(isExpanded){
            imgIndicator.setImageResource(R.drawable.minus);
        } 其他 {
            imgIndicator.setImageResource(R.drawable.plus);
        }
 

添加下面的ImageView在list_group.xml

 < ImageView的
        机器人:ID =@ + ID /指示器
        机器人:layout_width =24sp
        机器人:layout_height =24sp
        机器人:layout_alignParentRight =真
       />
 

I am using expandablelistview in my project,and I want to add different images for group view and also in child view I want to do same,but its not working,following is my code..

MainActivity.java

public class MainActivity extends Activity {

ExpandableTests listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

 private List<Integer> group;

    private HashMap<Integer, List<Integer>> Child;


Expandableimageadapter imgsadadapter;


public List<Integer> groupImages;
public HashMap<Integer, List<Integer>> childImages;

@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();


    expListView.setGroupIndicator(null);

    //imgsadadapter=new Expandableimageadapter(this, groupImages, childImages);

//  listAdapter = new ExpandableTests(this, listDataHeader, group, listDataChild, Child);

    listAdapter = new ExpandableTests(this, listDataHeader, groupImages, listDataChild, childImages);

    // setting list adapter
    expListView.setAdapter(listAdapter);
    //expListView.setAdapter(imgsadadapter);

    // 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() {



    groupImages= new ArrayList<Integer>();
    groupImages.add(R.drawable.adminsetting);
    groupImages.add(R.drawable.productcategory);
   // groupImages.add(R.drawable.drinks);
   // groupImages.add(R.drawable.deserts);

    childImages = new HashMap<Integer, List<Integer>>();
    List<Integer> Maincat = new ArrayList<Integer>();
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
   // vegetablesi.add(R.drawable.Onions);
   // vegetablesi.add(R.drawable.Garlic);

    List<Integer> subcat = new ArrayList<Integer>();
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
   // 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), Maincat);
  //  childImages.put(groupImages.get(1), subcat);
    childImages.put(0, Maincat);
    childImages.put(1, subcat);


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Admin Setting");
    listDataHeader.add("Product & Category");
    listDataHeader.add("Client & Orders");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("State City");
    top250.add("Sales Person");
    top250.add("City Sales Person");
    top250.add("Product Boxtype");
    top250.add("Shipping Carrier");
    //top250.add("The Dark Knight");
//  top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("Product");
    nowShowing.add("Category");
    nowShowing.add("Category Products");
    nowShowing.add("Product Categories");
    //nowShowing.add("Red 2");
    //nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("Client");
    comingSoon.add("Client Products");
    comingSoon.add("Client Orders");
    comingSoon.add("Client Orders Delivery");
    //comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}

ExpandableTests.java

public class ExpandableTests extends BaseExpandableListAdapter{


      private Context _context;

private List<Integer> _group;

private HashMap<Integer, List<Integer>> _Child;



private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableTests(Context context, List<String> listDataHeader,List<Integer> group,
        HashMap<String, List<String>> listDataChild, HashMap<Integer, List<Integer>> Child) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listDataChild;
    this._group=group;
    this._Child=Child;
}


@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
    // this._Child.get(this._group.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);
//  final int childimg = (Integer) 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.childsid);

    final int childimg = _Child.get(groupPosition).get(childPosition);
    imgListChild.setImageResource(childimg);
    //imgListChild.setImageResource(childimg);

    /*ImageView imageView = (ImageView) convertView.findViewById(R.id.childsid);
    final  int imageId = this.childImages.get(this.groupImages.get(groupPosition)).get(childPosition);
    imageView.setImageResource(imageId);*/

    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);
    //int headerimg = (Integer) 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 imgsListHeader = (ImageView) convertView
            .findViewById(R.id.gropids);

    int headerimg = _group.get(groupPosition);
    imgsListHeader.setImageResource(headerimg);
//  lblListHeader.setTypeface(null, Typeface.BOLD);
    //imgsListHeader.setImageResource(R.drawable.ic_launcher);

    /*ImageView imageView = (ImageView) convertView.findViewById(R.id.gropids);
    int imageId = this.groupImages.get(groupPosition);
    imageView.setImageResource(imageId);*/
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
解决方案

Replace following line with previous one in getChildView

final int childimg = _Child.get(groupPosition).get(childPosition);
imgListChild.setImageResource(childimg);

Replace following line with previous one in getGroupView

int headerimg = _group.get(groupPosition);
imgsListHeader.setImageResource(headerimg);

Replace following line MainActivity

   listAdapter = new ExpandableTests(this, listDataHeader, groupImages, listDataChild, childImages);

Add one more image in

  groupImages arraylist in prepareListData method

Replace following lines

 childImages.put(groupImages.get(0), Maincat);
 childImages.put(groupImages.get(1), subcat);

To

 childImages.put(0, Maincat);
 childImages.put(1, subcat);

For Indicator icon

Add following line in getGroupView() in ExpandableTests

ImageView imgIndicator = (ImageView) convertView.findViewById(R.id.indicator);
        if (isExpanded) {
            imgIndicator.setImageResource(R.drawable.minus);
        } else {
            imgIndicator.setImageResource(R.drawable.plus);
        }

Add following imageview in list_group.xml

<ImageView
        android:id="@+id/indicator"
        android:layout_width="24sp"
        android:layout_height="24sp"
        android:layout_alignParentRight="true"
       />   

这篇关于如何添加图像expandablelistivew?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:33