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);
    }

    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 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);

            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);
                        }
                    }
                }
            });
        }
    }


    @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;

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

        CreateArrayList();

        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 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"));
        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"));
        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"));
        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"));
        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"));
        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"));
    }
}

DetailsActivity

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent i = getIntent();
        String result = i.getStringExtra("name");

        Toast.makeText(DetailActivity.this, result, Toast.LENGTH_LONG).show();
    }

}
انتقل للدرس الخامس ,,

اترك تعليقاً

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