简体   繁体   English

单击时,ExpandableListView不会展开

[英]ExpandableListView does not expanding when clicked

I am using ExpandableListView to create my custom list with child items. 我使用ExpandableListView创建包含子项的自定义列表。 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. 你可以粘贴整个代码 - 适配器和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.) 同时有两种可能性 - 要么设置一个始终返回true的onGroupClickListener(以便点击不到达超类),要么XML布局中的某些内容会阻止click事件(可点击等)

ChildView getView() this method, when your judge convertview equals null, you must convertview.setTag(viewHolder) . ChildView getView()这个方法,当你判断convertview等于null时,你必须convertview.setTag(viewHolder) When your clicked parent view second, the convertview is not equals null, your must use this viewholder= (ViewHolder)convertview.getTag() ; 当您单击父视图时,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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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