RecyclerView ga onClickListener ulashim kerak, umumiy usuli bormi?

+4 ovoz
53 marta ko‘rilgan
so‘radi 09 fevral, 17 Bilmasvoy (904 bal)

Assalomu alaykum,

Androidda RecyclerView ishlatib ko`rganlar bormi? 

RecyclerView ga onClickListener ulashim kerak. Lekin har bir elementiga ulash juda nomaqul ish bo`lsa kerak, chunki 300-500 talik ro`yxat bo`lsa, qotib ishlashni boshlidi. 

Bilishimcha qanaqadur universal, bitta OnClick bilan qilsa bo`ladigan usuli bor. Biladiganlar bo`lsa etib yuboring, rahmat.

2 Javoblar

+2 ovoz
javob berdi 09 fevral, 17 Axrorxo'ja (733 bal)

Men bunday qilgan bo'lardim

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        @BindView(R.id.circleImageView) CircleImageView circleImageView;
        ViewHolder(View itemView)
            {
                super(itemView);
                ButterKnife.bind(this, itemView);
                circleImageView.setOnClickListener(this);
            }

        @Override
        public void onClick(View v)
            {
               // bu yerda click eventizni yozasiz
            }
}
RecycleView ni adapterida bind qilish quyidagicha bo'ladi
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.your_view, parent, false);
        return new ViewHolder(view);
    }
findviewId o'rniga Butterknife ishlatganman 
+1 ovoz
javob berdi 09 fevral, 17 Saidolim (3,516 bal)

Men ishlatib ko`rgan usul quyidagicha. Man Fragment da ishlatgan edim, o`shani kodini beraman.

public class MyObjectFragment extends Fragment implements View.OnClickListener {
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_list);
        // Mana shu joy sizga kerak
        MyObjectListAdapter adapter = new MyObjectListAdapter(this);
        
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return rootView;
    }

    @Override
    public void onClick(View v) {
        int pos = recyclerView.getChildAdapterPosition(v); // Position shu yerda
    }
}

endi Adapterda nima qilishimiz

public class MyObjectListAdapter extends RecyclerView.Adapter<MyObjectHolder> {

    private final View.OnClickListener mOnClickListener;

    public MobjectListAdapter(View.OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener; // bu yerda tashqaridagi onClickni olamiz
    }

    @Override
    public MobjectHolder onCreateViewHolder(ViewGroup parent, int pos) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment, parent, false);
        view.setOnClickListener(mOnClickListener); // bu yerda View ga ulaymiz
        return new MobjectHolder(view);
    }

....

Endi bemalol ishlatvursak bo`ladi.

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...