我正在开发一个Android项目,其中为CardView设置了一个recyclerView。
在这里,我有一个3点图像,其中包括一个菜单,可以在其中添加或删除recyclerView的项目。看起来像这样:
由于某种原因,当我删除一个项目,然后再添加项目时。然后有时会显示已删除的项目。当我将标题更改为“ Room 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;
}
}
答案 0 :(得分:0)
我认为您应该使用一个接口进行添加,并且应该从片段中监听该接口,并且只有从那里您才可以添加或删除项目
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();
}