android - ExpandableListView 在尝试在 Android 中显示 subview 时有错误的 groupPosition

标签 android expandablelistview android-viewholder

我遇到了一个我已经处理了一段时间的问题。我正在使用带有 ViewHolder 的 ExpandableListView。我知道 ExpandableListView 重用了 View ,我认为这就是我遇到的问题。

我在列表中有 3 个 View ,如果我选择 1,然后选择 2,然后选择 3,那么一切都很好,它会显示正确的 subview 。尽管当我首先选择 3 时,所有其他 subview 都具有相同的 groupPosition。如果我先选择2,那么3是正确的,但是1显示的是第三个 subview 。

我希望它们都固定在正确的 groupPosition 上,这样它们就不会改变。

这是我的 ExpandableListView 适配器:

public class MenuListAdapter extends BaseExpandableListAdapter 
{
private Context context;
private ArrayList<ListData> listData = new ArrayList<ListData>();

/**
 * Constructor for the class.
 * 
 * @param refContext This is a reference to the context of the ExpandableListAdapter being used in.
 * @param refListData This is a reference to the ArrayList of ListData that will display the item data of the menu in the list.
 */
public MenuListAdapter(Context refContext, ArrayList<ListData> refListData)
{
    context = refContext;
    listData = refListData;
}

private static class ViewHolder
{
    TextView title;
    TextView description;
    ImageView image;
    ImageButton addItem;
}

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

@Override
public int getChildrenCount(int groupPosition) 
{
    return 1; // There will only ever be 1 child in the group.
}

@Override
public Object getGroup(int groupPosition) 
{
    return groupPosition;
}

@Override
public Object getChild(int groupPosition, int childPosition) 
{
    return null;
}

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

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

@Override
public boolean hasStableIds() 
{
    return true; // Stops child views from resetting
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{
    final ViewHolder holder;

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_item, parent, false);

        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.titleTextView);
        holder.image = (ImageView) convertView.findViewById(R.id.imageImageView);
        holder.addItem = (ImageButton) convertView.findViewById(R.id.addItemImageButton);

        /* The contents of the item View is set for every View in the list. */
        holder.title.setText(listData.get(groupPosition).getTitle());
        holder.image.setImageBitmap(listData.get(groupPosition).getImageBitmap());
        holder.image.setTag(listData.get(groupPosition).getImageRes()); // Sets tag for image resource.

        holder.image.setFocusable(false);// Enables the group view to be in focus in order to still be able to select the image and group.

        holder.image.setOnClickListener(new OnClickListener() 
        {       
            @Override
            public void onClick(View v) 
            {
                Integer imageResource = (Integer) holder.image.getTag(); // Get image resource from tag.
                int resource = imageResource.intValue(); // Creates primitive image resource from object Integer.

                Intent imageIntent = new Intent(context, ImageActivity.class);
                imageIntent.putExtra("image_resource", resource); // Adds image resource as Extra to Intent.
                context.startActivity(imageIntent);
            }
        });

        holder.addItem.setFocusable(false); // Enables the group view to be in focus in order to still be able to select the button and group.

        holder.addItem.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {   
                /* AlertDialog that allows the user to select how many items they want of the item they selected. */
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                AlertDialog alertDialog;
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View customView = layoutInflater.inflate(R.layout.item_quantity, null);

                final NumberPicker quantityPicker = (NumberPicker) customView.findViewById(R.id.quantityPicker);
                quantityPicker.setMinValue(1);
                quantityPicker.setMaxValue(10);
                quantityPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); // Stops keyboard from appearing.

                alertBuilder.setTitle("Select quantity");
                alertBuilder.setView(customView);
                alertBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                        int quantityAmount = quantityPicker.getValue(); // Gets value from NumberPicker.

                        /* Update checkout data. */
                        MainActivity mainActivity = (MainActivity) context;
                        mainActivity.updateCheckout(holder.title.getText().toString(), quantityAmount); // Updates checkout with added order.

                        dialog.cancel(); // Cancels AlertDialog.

                        Toast.makeText(context, "Order added to checkout!", Toast.LENGTH_SHORT).show();
                    }
                });

                alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        dialog.cancel(); // Cancels AlertDialog.
                    }
                });

                alertDialog = alertBuilder.create(); // Creates the AlertDialog View.
                alertDialog.show(); // Shows the AlertDialog View.
            }

        });

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

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

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_desc_item, parent, false);

        holder = new ViewHolder();
        holder.description = (TextView) convertView.findViewById(R.id.descriptionTextView);

        holder.description.setText(listData.get(groupPosition).getDescription()); // Sets description of item View.

        System.out.println("Group position: " + groupPosition);
        System.out.println("Child position: " + childPosition);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

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

}

请有人帮我解决这个问题。

最佳答案

问题是 View 回收,你只是在第一次膨胀 View 时更新 View 试试这个

if(convertView == null)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.listview_desc_item, parent, false);

    holder = new ViewHolder();


    convertView.setTag(holder);
}


    holder = (ViewHolder) convertView.getTag();
    holder.description = (TextView) convertView.findViewById(R.id.descriptionTextView);

    holder.description.setText(listData.get(groupPosition).getDescription());
    System.out.println("Group position: " + groupPosition);
    System.out.println("Child position: " + childPosition);

关于android - ExpandableListView 在尝试在 Android 中显示 subview 时有错误的 groupPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752329/

相关文章:

java - RecyclerView.ViewHolder 是否必须始终是内部类?

android - 无法使用 "adb reverse"连接到开发服务器

android - AIR Native Extension 无法启动服务 Intent Android GCM

android - 将歌曲标题发送到 spotify 以从 android 应用程序开始播放

android - ExpandableListView 安卓+过滤EditText

android - 如何使用 LinearLayoutManager 实现可扩展项(Android L 引入)

android - 使用 SimpleCursorTreeAdapter 单击 ExpandableListView subview 中按钮的监听器

android - 在 RecyclerView 中单击时使用 Picasso 或任何图像加载器重新加载图像

java - If/else 条件与 firebase 值

java - 选择单选按钮后,平滑滚动到 ListView 中的下一项