skip to Main Content
WordAdapter.java
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.wordViewHolder> {

    public ArrayList<WordClass> mWordList;

    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(int posistion);
        void onDeleteClick(int posistion);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public WordAdapter(ArrayList<WordClass> wordList) {
        mWordList = wordList;
    }

    public static class wordViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageview;
        public TextView mText1;
        public TextView mText2;
        public ImageView mDeleteImage;

        public wordViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            mImageview = itemView.findViewById(R.id.image_view);
            mText1 = itemView.findViewById(R.id.text1);
            mText2 = itemView.findViewById(R.id.text2);
            mDeleteImage = itemView.findViewById(R.id.delete_image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onItemClick(position);
                        }
                    }
                }
            });

            mDeleteImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onDeleteClick(position);
                        }
                    }
                }
            });
        }
    }


    @NonNull
    @Override
    public wordViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
        //call custom item and connect it with holder
        wordViewHolder v = new wordViewHolder(view, mListener);
        return v;
    }

    @Override
    public void onBindViewHolder(@NonNull wordViewHolder wordViewHolder, int position) {
        //pass the values into the views
        WordClass currentWord = mWordList.get(position);
        wordViewHolder.mImageview.setImageResource(currentWord.getmImageView());
        wordViewHolder.mText1.setText(currentWord.getmTextView1());
        wordViewHolder.mText2.setText(currentWord.getmTextview2());
    }

    @Override
    public int getItemCount() {
        //count the number items in the RV
        return mWordList.size();
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    private WordAdapter mAdapter;

    private RecyclerView.LayoutManager mLayoutManager;
    private ArrayList<WordClass> mWordList;

    private EditText mEditText1, mEditText2;
    private Button mInseartButton, mRemoveButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEditText1 = findViewById(R.id.insert_text);
        mEditText2 = findViewById(R.id.remove_text);
        mInseartButton = findViewById(R.id.insert_button);
        mRemoveButton = findViewById(R.id.remove_button);

        CreateArrayList();

        buildRecyclerView();

        mInseartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = Integer.parseInt(mEditText1.getText().toString());
                insertValues(position);
            }
        });

        mRemoveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = Integer.parseInt(mEditText2.getText().toString());
                removeValues(position);
            }
        });
    }

    public void CreateArrayList() {
        mWordList = new ArrayList<>();

        mWordList.add(new WordClass(R.drawable.ic_face, "Text1", "Text2"));
        mWordList.add(new WordClass(R.drawable.ic_face, "Text1", "Text2"));
        mWordList.add(new WordClass(R.drawable.ic_face, "Text1", "Text2"));

    }

    public void buildRecyclerView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new WordAdapter(mWordList);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new WordAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int posistion) {
                String list1Text = mWordList.get(posistion).getmTextView1();
                Intent i = new Intent(MainActivity.this, DetailActivity.class);
                i.putExtra("name", list1Text);
                startActivity(i);
            }

            public void onDeleteClick(int posistion)
            {
                removeValues(posistion);
            }
        });
    }

    public void insertValues(int position) {
        mWordList.add(position, new WordClass(R.drawable.ic_face, "insert new item in index: " + position, "Text2"));
        mAdapter.notifyItemInserted(position);
    }

    public void removeValues(int position) {
        mWordList.remove(position);
        mAdapter.notifyItemRemoved(position);
    }
}

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *