单击时,ExpandableListView不会展开

时间:2011-04-25 11:14:00

标签: android listview expandablelistview

我使用ExpandableListView创建包含子项的自定义列表。我的第一个列表已成功创建,但它不可点击,因此无法展开。

这是我的代码......

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;
}
}

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

可以粘贴整个代码 - Adapter和ListView。布局也是必要的。同时有两种可能性 - 要么设置一个始终返回true的onGroupClickListener(这样点击不会到达超类),要么XML布局中的某些内容会阻止点击事件(可点击等)

答案 2 :(得分:0)

ChildView getView()此方法,当您的判断convertview等于null时,您必须convertview.setTag(viewHolder)。 单击父视图后,convertview不等于null,您必须使用此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;
    }