简体   繁体   中英

ExpandableListView does not expanding when clicked

I am using ExpandableListView to create my custom list with child items. My first list is created successfully but It is not clickable and therefore it can not be expanded.

Here is my code ...

private ExpandableListView listview;
private TheaterListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    setContentView(R.layout.theater_list);

    this.m_movieList = "A list of Movie (Custom Object)"

    listview = (ExpandableListView) findViewById(R.id.theater_listview);
    adapter = new TheaterListAdapter(getApplicationContext());
    listview.setAdapter(adapter);


private class TheaterListAdapter extends BaseExpandableListAdapter {

    public TheaterListAdapter(Context c) {
        mContext = c;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String MovieId = m_movieList.getMovies().get(groupPosition)
                .getShowTimes().get(childPosition).getMovieId();
        for (MovieInfo movie : m_movieList.getMovies()) {
            if (MovieId.equalsIgnoreCase(movie.getId())) {
                return movie;
            }
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        ExpandedHolder holder;
        View rowView = convertView;

        if (rowView == null) {
            holder = new ExpandedHolder();
            LayoutInflater li = getLayoutInflater();
            rowView = li.inflate(R.layout.theater_expanded_view, null);

            holder.img_movie_poster = (ImageView) rowView
                    .findViewById(R.id.imgMoviePoster);
            holder.img_rating_star = (ImageView) rowView
                    .findViewById(R.id.imgRatingStar);
            holder.txt_movie_name = (TextView) rowView
                    .findViewById(R.id.txtMovieName);
            holder.txt_pg_duration = (TextView) rowView
                    .findViewById(R.id.txtPgDuration);
            holder.txt_showtimes = (TextView) rowView
                    .findViewById(R.id.txtShowTimes);

            rowView.setTag(holder);

        } else {
            holder = (ExpandedHolder) rowView.getTag();
        }

        holder.txt_movie_name.setText("Twilight");
        holder.txt_pg_duration.setText("PG 90 min");
        holder.txt_showtimes.setText("7:00  8:00");

        return rowView;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String thid = m_movieList.getThreaters().get(groupPosition).getId();
        int cnt = 5;
        for (ShowTimes st : m_movieList.getMovies().get(groupPosition)
                .getShowTimes()) {
            if (thid.equalsIgnoreCase(st.getTheaterId())) {
                cnt = cnt + 1;
            }
        }

        return cnt;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return m_movieList.getThreaters().get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return m_movieList.getThreaters().size();
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        final ParentHolder holder;
        View rowView = convertView;
        try {

            if (rowView == null) {
                holder = new ParentHolder();
                LayoutInflater li = getLayoutInflater();
                rowView = li.inflate(R.layout.theater_list_item, null);

                holder.img_star = (ImageView) rowView
                        .findViewById(R.id.imgTheaterStar);
                holder.txt_title = (TextView) rowView
                        .findViewById(R.id.txtTheaterTitle);
                holder.txt_address1 = (TextView) rowView
                        .findViewById(R.id.txtTheaterAddress1);
                holder.txt_address2 = (TextView) rowView
                        .findViewById(R.id.txtTheaterAddress2);
                holder.txt_distance = (TextView) rowView
                        .findViewById(R.id.txtTheaterDistance);
                holder.btn_map = (Button) rowView
                        .findViewById(R.id.btnMapIcon);
                rowView.setTag(holder);

            } else {
                holder = (ParentHolder) rowView.getTag();
            }


            holder.txt_title
            .setText(((TheaterInfo)getGroup(groupPosition)).getName());
            holder.txt_address1.setText("3003 North Thanksgiving way");
            holder.txt_address2.setText("Lehi, UT, United States");
            holder.txt_distance.setText("15.0 mi");
        } catch (Exception e) {

        }

        return rowView;
    }

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

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

}

/**
 * Create Viewholder to use for inflating imageviews and textview
 */
static class ParentHolder {
    Button btn_map;
    ImageView img_star;
    TextView txt_title;
    TextView txt_address1;
    TextView txt_address2;
    TextView txt_distance;
}

/**
 * Create Viewholder to use for inflating imageviews and textview
 */
static class ExpandedHolder {
    ImageView img_movie_poster;
    ImageView img_rating_star;
    TextView txt_movie_name;
    TextView txt_pg_duration;
    TextView txt_showtimes;
}
}

尝试使组视图中的按钮在xml布局中不可聚焦(如果尚未调整)

Can you paste the whole code - the Adapter and the ListView. The layout also is needed. Meanwhile there are two possibilities - either you set a onGroupClickListener that always returns true (so that the click does not reach the super class) or something in the XML layout prevents the click event (clickable, etc.)

ChildView getView() this method, when your judge convertview equals null, you must convertview.setTag(viewHolder) . When your clicked parent view second, the convertview is not equals null, your must use this viewholder= (ViewHolder)convertview.getTag() ;

 public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View view = convertView;
        ChildViewHolder viewHolder = null;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.member_childitem2, null);
            viewHolder = new ChildViewHolder();



            viewHolder.checkBox1 = (CheckBox)view.findViewById(R.id.checkbox1);
            viewHolder.checkBox2 = (CheckBox)view.findViewById(R.id.checkbox2);
            viewHolder.checkBox3 = (CheckBox)view.findViewById(R.id.checkbox3);
            viewHolder.checkBox4 = (CheckBox)view.findViewById(R.id.checkbox4);
            viewHolder.checkBox5 = (CheckBox)view.findViewById(R.id.checkbox5);
            viewHolder.checkBox6 = (CheckBox)view.findViewById(R.id.checkbox6);


            view.setTag(viewHolder);
        }else{
            viewHolder = (ChildViewHolder) view.getTag();
        }

        Button btn_submit = (Button)view.findViewById(R.id.btn_submit);


        View groupView = cachedGroupView.get(childPosition);
        final GroupViewHolder groupViewHolder = (GroupViewHolder) groupView.getTag();
        btn_submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,
                        groupPosition+ ":"
                                + childPosition,
                        Toast.LENGTH_SHORT).show();

                groupViewHolder.groupStatus.setImageResource(R.drawable.indicator_close);
            }
        });


        return view;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM