首页 > 解决方案 > recyclerView 添加和删除

问题描述

我正在开发一个 Android 项目,在那里我为 CardView 设置了一个 recyclerView。

这里我有一个 3 点图像,其中包含一个菜单,我可以在其中添加或删除 recyclerView 的项目。它看起来像这样:

在此处输入图像描述

出于某种原因,当我删除一个项目,然后添加项目。然后有时会显示已删除的项目。当我将标题更改为“房间 1”并删除它时,当我添加新项目时,它会再次出现。任何人都知道为什么会发生这种情况。为什么它不会删除该项目?

提前致谢!

* 代码在这里 *

回收适配器

public class lokationRecyclerAdapter extends RecyclerView.Adapter<lokationRecyclerAdapter.RecyclerViewHolder> {

List<String> cardList;
String test;

SensorOversigtFragment sensorOversigtFragment;

public interface listListener {
    void onAddListener( List<String> cardList, int position);
}

public lokationRecyclerAdapter(List<String> cardList) {
    this.cardList = cardList;
    notifyDataSetChanged();
}

@Override
public RecyclerViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.lokationcardview, viewGroup, false );
    return new RecyclerViewHolder( view );
}

public void addItem() {
    cardList.add( new String() );
    notifyDataSetChanged();
}

public void deleteItem(int position) {
    cardList.remove( position );
    notifyItemRemoved( position );
    notifyItemRangeChanged( position, cardList.size() );

}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
    recyclerViewHolder.cardView.setBackgroundResource( R.drawable.ic_circleshape );
    listListener.onAddListener(cardList.get( position ), position);
}

@Override
public int getItemCount() {
    return cardList.size();
}

class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public int nummer;
    private CardView cardView;
    private Button menuButton;
    private TextView lokationsName;

    public void alertDialog(View itemView) {

        new AlertDialog.Builder( itemView.getContext() );

        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder( itemView.getContext() );
        builder.setCancelable( true );
        builder.setTitle( "Tilføj eller ændre navnet på lokation" );
        builder.setMessage( "Hold styr på dine sensorer ved at oprette lokationer i form af eksempelvis lokaler." );

        final EditText input = new EditText( itemView.getContext() );
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
        input.setMaxLines( 1 );
        input.setInputType( InputType.TYPE_CLASS_TEXT );
        input.setLayoutParams( lp );
        builder.setView( input );

        builder.setPositiveButton( "Ja", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                test = input.getText().toString();
                lokationsName.setText( test );
            }
        } );

        builder.setNegativeButton( "Nej", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        } );

        android.support.v7.app.AlertDialog dialog = builder.create();
        dialog.show();

    }

    public RecyclerViewHolder(final View itemView) {
        super( itemView );

        cardView = (CardView) itemView.findViewById( R.id.lokationcard_view );
        menuButton = (Button) itemView.findViewById( R.id.menuButton );
        lokationsName = (TextView) itemView.findViewById( R.id.lokationsName );

        itemView.setOnClickListener( this );

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

                final PopupMenu popupMenu = new PopupMenu( itemView.getContext(), menuButton );
                popupMenu.getMenuInflater().inflate( R.menu.lokationpopmenu, popupMenu.getMenu() );

                popupMenu.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                        switch (item.getItemId()) {

                            case R.id.changeLokation:
                                System.out.println( "GET ADAPTER POSITION: " + getAdapterPosition() );
                                alertDialog( itemView );
                                break;

                            case R.id.addCardView:
                                String lokation = lokationsName.getText().toString();
                                System.out.println("LOKATIONS NAME: " + lokation );
                                if(lokation.equals( "TILFØJ LOKATION" )) {
                                   Toast.makeText( itemView.getContext(), "Du skal ændre navnet på lokation først!", Toast.LENGTH_LONG ).show();
                                } else {
                                    addItem();
                                }
                                break;

                            case R.id.deleteCardView:
                                if (cardList.size() > 1) {
                                    deleteItem( getAdapterPosition() );
                                } else {
                                    Toast.makeText( itemView.getContext(), "Du kan ikke slette den sidste CardView", Toast.LENGTH_LONG ).show();
                                }
                                break;

                        }
                        return true;
                    }
                } );
                popupMenu.show();
            }
        } );

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {

        nummer = getAdapterPosition();
        Intent intent = new Intent( itemView.getContext(), gridTabelActivity.class );
        Bundle bundle = ActivityOptions.makeCustomAnimation( v.getContext(), R.anim.slide_in_right, R.anim.slide_out_left ).toBundle();
        itemView.getContext().startActivity( intent, bundle );
    }



 }

}

片段初始化recyclerView

public class SensorOversigtFragment extends Fragment {

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

List<String> test = new ArrayList<>();

public SensorOversigtFragment() {
    // Required empty public constructor
}

public void onAddListener(List<String> cardList, int position) {
    test = cardList;
    cardList.add( new String() );
    adapter.notifyDataSetChanged();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate( R.layout.lokationcardlist, container, false );

    recyclerView = (RecyclerView) view.findViewById( R.id.lokationrecycler_view );

    test.add( "test" );

    adapter = new lokationRecyclerAdapter( test );
    recyclerView.setHasFixedSize( true );
    recyclerView.setAdapter( adapter );
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager( this.getActivity() );
    recyclerView.setLayoutManager( linearLayoutManager );

    adapter.notifyDataSetChanged();

    return view;
    }
}

标签: androidandroid-recyclerview

解决方案


我认为您应该使用一个接口进行添加,并且应该从片段中收听该接口,并且仅从那里您应该添加或删除项目

           makerInterface.onAddListener(cardList.get(position),position);

这应该进入 onBindViewHolder

TeamMakerInterface.java

public interface TeamMakerInterface {

    void onAddListener( List<String> cardList,int position);

}

而不是在片段本身中添加和删除它

    @Override
    public void onAddListener( List<String> cardList,int position) {
       cardList.add( new String() );
       notifyDataSetChanged();
    }

推荐阅读