0

Суть такова. Я создал кастомный layout.

public class CustomAdapterView extends LinearLayout {

private int postiton;
private View mGlobalView;
ArrayList<SquareLayout> containerIds = new ArrayList<>();
ArrayList<LinearLayout> rows = new ArrayList<>();
private BaseAdapter adapter;
private ImageView mFindButton = new ImageButton(getContext());

private final DataSetObserver observer = new DataSetObserver() {

    @Override
    public void onChanged() {
        refreshViewsInAdapter();
    }

    @Override
    public void onInvalidated() {
        removeAllViews();
    }
};


private void initContainers(){
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_one));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_two));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_three));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_four));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_five));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_six));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_seven));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_eight));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_nine));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_ten));
    containerIds.add((SquareLayout) mGlobalView.findViewById(R.id.container_eleven));

    rows.add((LinearLayout) mGlobalView.findViewById(R.id.row_one));
    rows.add((LinearLayout) mGlobalView.findViewById(R.id.row_two));
    rows.add((LinearLayout) mGlobalView.findViewById(R.id.row_three));


}

private void createInflater(){
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mGlobalView = inflater.inflate(R.layout.drag_and_drop_layout, this,true);
    initContainers();

}

public CustomAdapterView(Context context) {
    super(context);
    createInflater();

}

public CustomAdapterView(Context context, AttributeSet attrs) {
    super(context, attrs);
    createInflater();
}

public CustomAdapterView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    createInflater();
}

public BaseAdapter getAdapter() {
    return adapter;
}


public void setAdapter(BaseAdapter adapter) {
    if (this.adapter != null) {
        this.adapter.unregisterDataSetObserver(observer);
    }
    this.adapter = adapter;
    if (this.adapter != null) {
        this.adapter.registerDataSetObserver(observer);
    }
    initViewsFromAdapter();
}


private void checkPosition(){
    if(postiton >= 5) rows.get(1).setVisibility(View.VISIBLE);
    else rows.get(1).setVisibility(View.GONE);
    if(postiton >= 8) rows.get(2).setVisibility(View.VISIBLE);
    else rows.get(2).setVisibility(View.GONE);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

private boolean enableFlagForFindButton = false;

private void moveFindImage(){
    if (postiton != containerIds.size() ) {
        if (enableFlagForFindButton) {
            if (mFindButton.getParent() != null) {
                ((SquareLayout) mFindButton.getParent()).removeView(mFindButton);
                containerIds.get(postiton).addView(mFindButton);
            } else {
                containerIds.get(postiton).addView(mFindButton);
            }
        }
    }
}


public void onEnableFindButton(boolean chose){
    enableFlagForFindButton = chose;
    if (enableFlagForFindButton && postiton != containerIds.size()) {
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mFindButton.setLayoutParams(layoutParams);
        mFindButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.notifyDataSetChanged();
            }
        });
        containerIds.get(postiton).addView(mFindButton);
    }
    else {
        if(mFindButton.getParent() != null){
            ((SquareLayout) mFindButton.getParent()).removeView(mFindButton);
        }
    }
}

public void setImageForFindButton(int res){
    mFindButton.setImageResource(res);
}


public void setOnClickForButtonFind(OnClickListener onClick){
    mFindButton.setOnClickListener(onClick);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




protected void initViewsFromAdapter() {
    if (adapter != null) {
        int adapterSize = adapter.getCount();
        if (adapterSize > containerIds.size()){
            adapterSize = containerIds.size();
        }
        postiton = adapterSize;
        checkPosition();
        for (int i = 0; i < adapterSize; i++) {
            RelativeLayout rl = (RelativeLayout) adapter.getView(i, null, this);
            (containerIds.get(i)).addView(rl);
        }

    }

}

protected void refreshViewsInAdapter() {
    int adapterSize = adapter.getCount();
    Log.i("ADAPTER_SIZE " ,""+ adapterSize);
    postiton = adapterSize;
    checkPosition();
        for (int i = 0; i < adapterSize; i++) {
           containerIds.get(i).removeAllViewsInLayout();
        }
    adapterSize = adapter.getCount();
    for (int i = 0; i < adapterSize; i++) {
        containerIds.get(i).addView(adapter.getView(i,null,this));
    }
    moveFindImage();
}

}

И сделал для него адаптер, который будет наследоваться от BaseAdapter.

  public class MyDragNDropAdapter extends BaseAdapter {

private ArrayList<Items> items = new ArrayList<>();
private Context context;
private int itemLayout;

public MyDragNDropAdapter(Context context, int itemLayout) {
    this.context = context;
    this.itemLayout = itemLayout;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewholder;

    if (convertView == null)
    {

        convertView = LayoutInflater.from(context).inflate(itemLayout, null);
        viewholder = new ViewHolder(convertView,position);
        convertView.setTag(viewholder);

    }
    else
    {
        viewholder = (ViewHolder)convertView.getTag();
    }
    viewholder.image.setImageResource(items.get(position).image);
    viewholder.btn.setTag(position);
    viewholder.btn.setOnClickListener(
            new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    items.remove(position);
                    ArrayList<Items> arr = items;
                    items.clear();
                    for(int i = 0; i < arr.size();i++){
                        items.add(arr.get(i));
                    }
                    notifyDataSetChanged();
                }
            }
    );
    return convertView;

}





public void addItem(Object object) {
    items.add((Items) object);

}

private static class ViewHolder{
    View context;
    int position;

    ImageView image ;
    TextView text;
    Button btn;

    ViewHolder(View context,int position){
        this.context = context;
        this.position = position;
        text = (TextView) context.findViewById(R.id.product_description);
        image = (ImageView) context.findViewById(R.id.product_image_in_item);
        btn = (Button) context.findViewById(R.id.close_button_in_item);

    }


}

}

Хочу сделать удаление любого элемента по кнопке. Но при нажатии на кнопку элементы из ArrayList удаляются, но при этом экран не обновляется.

Сам метод удаления:

        viewholder.btn.setOnClickListener(
            new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    items.remove(position);
                    ArrayList<Items> arr = items;
                    items.clear();
                    for(int i = 0; i < arr.size();i++){
                        items.add(arr.get(i));
                    }
                    notifyDataSetChanged();
                }
            }
    );

Метод notifyDataSetChanged для моего кастомного Layout-a

    protected void refreshViewsInAdapter() {
    int adapterSize = adapter.getCount();
    for (int i = 0; i < adapterSize; i++) {
           containerIds.get(i).removeAllViewsInLayout();
        }
    adapterSize = adapter.getCount();
    for (int i = 0; i < adapterSize; i++) {
        containerIds.get(i).addView(adapter.getView(i,null,this));
    }
}

1 ответ 1

0

Решение оказалось предельно простым. Количество элементов уменьшалось до того, как очистится layout. Из-за этого последний элемент не удалялся.

for (int i = 0; i <= adapterSize; i++) {
        containerIds.get(i).removeAllViewsInLayout();
        removeDragListener(i);
        }

добавил <= вместо <

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.