Android更改单击,自定义可扩展列表适配器上的图像

时间:2014-04-07 11:24:53

标签: android expandablelistview expandablelistadapter

我将使用自定义可扩展列表适配器实现可扩展的列表视图 并且还有组扩展和组崩溃的图像更改。

我的可扩展列表视图效果很好但是当我在这些事件上设置图像更改时会出现问题。即当我展开一个时,另一个人的图像也会自动改变。我想我不能保留小组项目的正确地址。

这是我的代码。

public class HomeFrame extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    ImageView img_selection;
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
     public static int[] GalImages = new int[] {
                R.drawable.banner_one,
                R.drawable.banner,
                R.drawable.banner_three,
                R.drawable.banner_two,};
    public HomeFrame() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.home_frame, container, false);

        ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.banner);
        ImageAdapter adapter = new ImageAdapter(getActivity());
        viewPager.setAdapter(adapter);

        //img_selection=(ImageView)rootView.findViewById(R.id.img_selection);

        expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpListAdapter(getActivity(), listDataHeader,
                listDataChild);

        // 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(getActivity(),
                 "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(getActivity(),
                  groupPosition + " Expanded",
                  Toast.LENGTH_SHORT).show();

             img_selection=(ImageView)expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                img_selection.setImageResource(R.drawable.arrow_se);
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {

                 Toast.makeText(getActivity(),
                 groupPosition + " Collapsed",
                 Toast.LENGTH_SHORT).show();

                 img_selection=(ImageView) expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                //ImageView img_selection=(ImageView) get.findViewById(R.id.img_selection);

                img_selection.setImageResource(R.drawable.arrow);
            }
        });

        // 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( getActivity(),
                 * listDataHeader.get(groupPosition) + " : " +
                 * listDataChild.get( listDataHeader.get(groupPosition)).get(
                 * childPosition), Toast.LENGTH_SHORT) .show();
                 */
                return false;
            }
        });

        return rootView;
    }

适配器类是..

public class ExpListAdapter 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 ExpListAdapter(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.item_home, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.heading);
        //ImageView img_selection=(ImageView)convertView.findViewById(R.id.img_selection);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        //img_selection.setImageResource(R.drawable.arrow);

        return convertView;
    }

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

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

}

3 个答案:

答案 0 :(得分:11)

getGroupView()的自定义适配器中执行此操作:

@Override
 public View getGroupView(int groupPosition, boolean isExpanded, View view,
                ViewGroup parent) {

    ImageView img_selection=(ImageView) view.findViewById(R.id.img_selection);
    int imageResourceId = isExpanded ? R.drawable.group_closed
                        : R.drawable.group_open;
     img_selection.setImageResource(imageResourceId);
}

答案 1 :(得分:5)

在更改组扩展(折叠或扩展)的情况下更改父级图像的最佳方法是使用getGroupView()方法和属性isExpanded bolean。

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
  ...

    if (isExpanded) {
        groupHolder.img.setImageResource(R.drawable.group_down);
    } else {
        groupHolder.img.setImageResource(R.drawable.group_up);
    }
  ...

}

答案 2 :(得分:0)

 @BindView(R.id.expandableListView)
ExpandableListView expandableListView;



List<ExpandableGroupModel> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
ExpandableListAdapter expandableListAdapter;
private int lastExpandedPosition = -1;

在onCreate或您正在获取数据的位置添加以下代码

    expandableListDetail = new HashMap<String, List<String>>();

    List<String> Attractions = new ArrayList<String>();
    Attractions.add("Coachin");
    Attractions.add("Munnar");
    Attractions.add("Thekkadi");
    Attractions.add("Alleppy");

    List<String> football = new ArrayList<String>();
    football.add("Brazil");
    football.add("Spain");
    football.add("Germany");
    football.add("Netherlands");
    football.add("Italy");

    List<String> basketball = new ArrayList<String>();
    basketball.add("United States");
    basketball.add("Spain");
    basketball.add("Argentina");
    basketball.add("France");
    basketball.add("Russia");

    expandableListDetail.put("visit for", Attractions);
    expandableListDetail.put("Seasonality", football);
    expandableListDetail.put("Major Attractions", basketball);
    expandableListDetail.put("Famous Shopping", football);
    expandableListDetail.put("Cuisines", basketball);
    expandableListDetail.put("Famous Festivals", basketball);
    expandableListDetail.put("Hotels", basketball);
    expandableListDetail.put("Price starts from", basketball);


    // expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    expandableListTitle = new ArrayList<>();

    ExpandableGroupModel expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("visit for");
    expandableGroupModel.setImage(R.drawable.visit_for);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Seasonality");
    expandableGroupModel.setImage(R.drawable.seasonality);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Major Attractions");
    expandableGroupModel.setImage(R.drawable.major_attractions);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Famous Shopping");
    expandableGroupModel.setImage(R.drawable.famous_shopping);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Cuisines");
    expandableGroupModel.setImage(R.drawable.cuisines);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Famous Festivals");
    expandableGroupModel.setImage(R.drawable.famous_festivals);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Hotels");
    expandableGroupModel.setImage(R.drawable.hotels);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Price starts from");
    expandableGroupModel.setImage(R.drawable.price_start);
    expandableListTitle.add(expandableGroupModel);


    //Set adapter
    expandableListAdapter = new CustomExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail, "Dest_detail");
    expandableListView.setAdapter(expandableListAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getActivity(),
                    expandableListTitle.get(groupPosition).getName() + " List Expanded.",
                    Toast.LENGTH_SHORT).show();


            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;


        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getActivity(),
                    expandableListTitle.get(groupPosition).getName() + " List Collapsed.",
                    Toast.LENGTH_SHORT).show();

        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(
                    getActivity(),
                    expandableListTitle.get(groupPosition).getName()
                            + " -> "
                            + expandableListDetail.get(
                            expandableListTitle.get(groupPosition).getName()).get(
                            childPosition), Toast.LENGTH_SHORT
            ).show();
            return false;
        }
    });

    expandableListView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

适配器如下:

CustomExpandableListAdapter.java

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<ExpandableGroupModel> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
Typeface roboto;
String from;

public CustomExpandableListAdapter(Context context, List<ExpandableGroupModel> expandableListTitle,
                                   HashMap<String, List<String>> expandableListDetail, String from) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
    this.from = from;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(" - " + expandedListText);


    roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
    expandedListTextView.setTypeface(roboto);

    if (from.equals("Dest_detail")) {
        expandedListTextView.setBackgroundResource(R.color.white);

    } else {
        expandedListTextView.setBackgroundResource(R.color.screen_bg_color);

    }
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    ExpandableGroupModel listObj = (ExpandableGroupModel) getGroup(listPosition);
    String listTitle = listObj.getName();
    int listimage = listObj.getImage();
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    ImageView img_group_icon = (ImageView) convertView
            .findViewById(R.id.img_group_icon);
    ImageView img_group_arrow = (ImageView) convertView
            .findViewById(R.id.img_group_arrow);
    listTitleTextView.setText(listTitle);
    img_group_icon.setImageResource(listimage);


    roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
    listTitleTextView.setTypeface(roboto, Typeface.BOLD);

    if (isExpanded) {
        img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.up));
        listTitleTextView.setTextColor(context.getResources().getColor(R.color.colorPrimary));
    } else {
        img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.down));
        listTitleTextView.setTextColor(context.getResources().getColor(R.color.textcolor_package));

    }


    return convertView;
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}
}

ExpandableGroupModel模型如下:

public class ExpandableGroupModel {

String name;
int image;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}
}