Why does my Android RecyclerView duplicate items by scrolling them?
Here is my Code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class Fragment1 extends Fragment {
RecyclerView rv1;
TextView rvtvheadline;
TextView rvtvdescription;
ImageView rvivicon;
String[] headlinervtext = {"Eintrag 1", "Eintrag 2", "Eintrag 3"};
String[] descriptionrv = {"Beschreibung 1","Beschreibung 2","Beschreibung 3"};
int[] iconsrv = {R.drawable.ic_attach_file_grey600_24dp, R.drawable.ic_format_color_reset_grey600_24dp, R.drawable.ic_format_paint_grey600_24dp};
ArrayList<String> headlinerv = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// recyclerviewsetup();
super.onActivityCreated(savedInstanceState);
}
@Override
public void onHiddenChanged(boolean hidden) {
if(hidden == true){
// rv1= null;
}
super.onHiddenChanged(hidden);
}
public void recyclerviewsetup(){
headlinerv.addAll(Arrays.asList(headlinervtext));
rv1 = (RecyclerView) getView().findViewById(R.id.rv1);
rv1.setLayoutManager(new LinearLayoutManager(getActivity()));
rv1.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
rv1.setAdapter(new RecyclerView.Adapter() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View itemview1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rv_item_layout1, viewGroup, false);
rvtvheadline = (TextView) itemview1.findViewById(R.id.textViewrv1);
rvtvdescription = (TextView) itemview1.findViewById(R.id.textViewrv2);
rvivicon = (ImageView) itemview1.findViewById(R.id.imageViewrv1);
itemview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (rv1.getChildAdapterPosition(itemview1)) {
case 0: {
Toast.makeText(getActivity(), "Item 1", Toast.LENGTH_SHORT).show();
break;
}
case 1: {
Toast.makeText(getActivity(), "Item 2", Toast.LENGTH_SHORT).show();
break;
}
case 2: {
Toast.makeText(getActivity(), "Item 3", Toast.LENGTH_SHORT).show();
break;
}
}
}
});
return new RecyclerView.ViewHolder(itemview1) {
};
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
rvtvheadline.setText(headlinerv.get(i));
rvtvdescription.setText(descriptionrv[i]);
rvivicon.setImageResource(iconsrv[i]);
}
@Override
public int getItemCount() {
return headlinerv.size();
}
});
}
}
If I have many Items they just duplicate by scrolling. Do you have an idea why?
I have tried out this: public long getItemId(int position){ return position}
But this don't resolve my Problem.