请看这个屏幕:
java - getChildView项目onClickListener会影响ExpandableListView Android中的所有子项目-LMLPHP
当我点击accept changed to another按钮时:
java - getChildView项目onClickListener会影响ExpandableListView Android中的所有子项目-LMLPHP
如果我在可展开列表视图中单击一个getChildView列表项,则在滚动屏幕时也会影响其他子项。在这里,我在单击getChildview项时更改了按钮文本
在这里,我点击一个子项的accept按钮,然后它应该会变为另一个按钮,但是在滚动所有子项时,按钮也会变

public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final Context context;

        private final List<String> headings;

        private List<DriverOrders> order;

        GroupHolder holder;

        public ExpandableListAdapter(final FragmentActivity fragmentActivity,
                                      final List<String> lidtDataHeaders, final List<DriverOrders> orders
        ) {
            this.context = fragmentActivity;
            this.headings = lidtDataHeaders;
            this.order = orders;
        }

        @Override
        public Object getChild(final int groupPosition, final int childPosition) {

            return this.headings.get(childPosition);
        }

        @Override
        public long getChildId(final int groupPosition, final int childPosition) {

            return childPosition;
        }

        @Override
        public View getChildView(final int groupPosition,
                                 final int childPosition, final boolean isLastChild,
                                 View convertView, final ViewGroup parent) {

            final String childName = (String) getChild(groupPosition, childPosition);



            // GroupHolder holder;
            holder = null;

            if (convertView == null) {
                final LayoutInflater inflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.driver_child_layout, null);
                holder = new GroupHolder();

                holder.orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
                holder.taskType = (TextView) convertView.findViewById(R.id.taskType);
                holder.detaisl = (TextView) convertView.findViewById(R.id.detaisl);
                holder.accept = (Button) convertView.findViewById(R.id.accpet);
                holder.decline = (Button) convertView.findViewById(R.id.decline);
                holder.started = (Button) convertView.findViewById(R.id.started);
                convertView.setTag(holder);

            } else {
                holder = (GroupHolder) convertView.getTag();
            }

           /* TextView orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
            TextView taskType = (TextView) convertView.findViewById(R.id.taskType);
            TextView detaisl = (TextView) convertView.findViewById(R.id.detaisl);
            accept = (Button) convertView.findViewById(R.id.button);
            decline = (Button) convertView.findViewById(R.id.decline);

            started = (Button) convertView.findViewById(R.id.started);*/

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("","inside convertview click"+parent.getChildAt(childPosition));
                }
            });


            holder.started.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag) {
                        AsyncHttpClient client = new AsyncHttpClient();
                        client.get(null, CommonUtility.SERVER_URI + "person-started/" + Long.parseLong(order.get(groupPosition).getOrderId().trim()), new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                super.onSuccess(statusCode, headers, response);

                                try {
                                    if (response.getString("STATUS").equalsIgnoreCase("true")) {

                                        mListener.onStarttrip(order.get(groupPosition).getOrderId(), order.get(groupPosition).getCustomerName(), order.get(groupPosition).getCustomerContact());
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                                super.onFailure(statusCode, headers, throwable, errorResponse);

                                if (statusCode == 404) {
                                    Toast.makeText(getActivity(), "Requested resource not found", Toast.LENGTH_LONG).show();
                                }
                                // When Http response code is '500'
                                else if (statusCode == 500) {
                                    Toast.makeText(getActivity(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                                }
                                // When Http response code other than 404, 500
                                else {
                                    Toast.makeText(getActivity(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
                                }
                            }

                        });


                    } else {
                        Toast.makeText(getActivity(), "Before started you have to accept the order", Toast.LENGTH_LONG).show();
                    }

                }

            });

            holder.orderNumber.setText(order.get(groupPosition).getCustomerName());
            holder.taskType.setText(order.get(groupPosition).getCustomerContact());
            holder.detaisl.setText(order.get(groupPosition).getOrderId());

            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Log.e("", "group positioins" + groupPosition);
                    String associateId = sessionManager.getUserDetails().get("associate_id");
                    String oredeerid = order.get(groupPosition).getOrderId();

                    RequestParams params = new RequestParams();
                    params.put("oid", oredeerid);
                    params.put("did", associateId);
                    requestForAcceptOrder(params, groupPosition);


                }

            });
            holder.decline.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    holder.decline.setFocusable(false);
                    holder.decline.setEnabled(false);
                    holder.decline.setClickable(false);
                    sessionManager.setDclineId(order.get(groupPosition).getOrderId());
                    Toast.makeText(getActivity(), "Order is declined", Toast.LENGTH_LONG).show();
                    Intent myIntent = new Intent(getActivity(), DeclineReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                    Long timeInMillis = new GregorianCalendar().getTimeInMillis() + 1000;
                    Log.d("pass", "!!!!!!!");
                    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, 5400000, pendingIntent);
                    notifyDataSetChanged();

                }
            });

            return convertView;
        }

        @Override
        public int getChildrenCount(final int groupPosition) {

            return 1;
        }

        @Override
        public Object getGroup(final int groupPosition) {

            return this.headings.get(groupPosition);
        }

        @Override
        public int getGroupCount() {

            return this.headings.size();
        }

        @Override
        public long getGroupId(final int groupPosition) {

            return groupPosition;
        }

        @SuppressLint("NewApi")
        @Override
        public View getGroupView(final int groupPosition,
                                 final boolean isExpanded, View convertView,
                                 final ViewGroup parent) {
            final String headerTitle = (String) this.getGroup(groupPosition);
           /* final ExpandableListView mExpandableListView = (ExpandableListView) parent;
           mExpandableListView.expandGroup(0);*/

            // to display indicator of Expandable view from left to right
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

                CabOrderFragment.this.expnListView
                        .setIndicatorBounds(
                                CabOrderFragment.this.expnListView
                                        .getWidth() - 50,
                                CabOrderFragment.this.expnListView
                                        .getWidth());
            } else {
                CabOrderFragment.this.expnListView
                        .setIndicatorBoundsRelative(
                                CabOrderFragment.this.expnListView
                                        .getWidth() - 50,
                                CabOrderFragment.this.expnListView
                                        .getWidth());

            }
            final LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.expandlistview_heading_group, null);
            // final ImageView image = (ImageView) convertView
            // .findViewById(R.id.ivHeadeImage);
            final TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.tvListHeader);
            // image.setImageResource(this.images[groupPosition]);

            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle.toUpperCase(Locale.getDefault()));
            lblListHeader.setTextSize(10);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(final int groupPosition,
                                         final int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }


        private void requestForAcceptOrder(RequestParams params, final int groupPosition) {
            AsyncHttpClient client = new AsyncHttpClient();
            client.get(null, CommonUtility.SERVER_URI + "accept", params, new JsonHttpResponseHandler() {


                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject obj) {
                    try {
                        if (obj.getInt("code") == 0) {

                            holder.accept.setVisibility(View.GONE);
                            holder.decline.setVisibility(View.INVISIBLE);
                            holder.started.setVisibility(View.VISIBLE);
                            Toast.makeText(getActivity(), "Oeder is  Placed", Toast.LENGTH_LONG);
                            Intent myIntent = new Intent(getActivity(), DriverOrdersReceiver.class);
                            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                            Log.d("stop alarm", "!!!!!!!");
                            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                            alarmManager.cancel(pendingIntent);
                            flag = true;

                            Log.e("", "object data" + obj.toString());

                        } else {

                            Toast.makeText(getActivity(), "Oeder is Not Placed", Toast.LENGTH_LONG);
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }


                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);

                    if (statusCode == 404) {
                        Toast.makeText(getActivity(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code is '500'
                    else if (statusCode == 500) {
                        Toast.makeText(getActivity(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code other than 404, 500
                    else {
                        Toast.makeText(getActivity(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
                    }
                }


            });


        }

        /*private view holder class*/
        private class GroupHolder {
            TextView orderNumber;
            TextView taskType;
            TextView detaisl;
            Button accept;
            Button decline;
            Button started;

        }


    }

最佳答案

您的问题是由于Recycling mechanism引起的,您需要在getChildView()方法中重置文本颜色。
这可以通过if{}else{}案例来完成。
如:

if(yourTextMustTakeTheDefaultColor){
   // set the default color in your text
} else {
   // set the other color
}

更新:
1)首先不要使用holder作为类变量。
从适配器中删除类变量GroupHolder holder;
然后像这样更改getChildView()代码:
GroupHolder holder;
if (convertView == null) {
    final LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.driver_child_layout, null);
    holder = new GroupHolder();
    //the rest of code...
    convertView.setTag(holder);
} else {
    holder = (GroupHolder) convertView.getTag();
}
//the rest of code...

2)就像我已经告诉过你要做的:根据子数据在if else中添加getChildView()case以更新你的视图(我认为你想更新accept/denied/started可见性)。
如:
if(order.get(groupPosition).isAcceptedVisible()){
   holder.accept.setVisibility(View.VISIBLE);
} else {
   holder.accept.setVisibility(View.GONE);
}

3)更改requestForAcceptOrder()以通过子位置。
private void requestForAcceptOrder(RequestParams params, final int groupPosition, final int childPosition)

4)在onSuccess()方法中,您拥有groupPositionchildPosition,更新列表的数据/模型并调用notifyDataSetChanged();,系统将负责更新您的视图。

07-27 15:28