简体   繁体   中英

Implement onclick in Scrolling Recycleview

I have recycleview which is set to auto scroll. I am looking to implement onclicklistener such that new activity will open.

Here is my Recycleview


final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
            top.smoothScrollBy(pixelsToMove, 0);
            mHandler.postDelayed(this, duration);
        }
};


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    top = (RecyclerView) view.findViewById(R.id.top);

    final LinearLayoutManager llm = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
    top.setLayoutManager(llm);
    top.setHasFixedSize(true);
    staggeredBooksAdapter = new TopAdapter(this, bookslist);

    top.setAdapter(staggeredBooksAdapter);

    //Recycleview
    top.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState); }

        @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastItem = llm.findLastCompletelyVisibleItemPosition();
            if(lastItem == llm.getItemCount()-1){
                mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                Handler postHandler = new Handler();
                postHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                            top.setAdapter(null);
                            top.setAdapter(staggeredBooksAdapter);
                            mHandler.postDelayed(SCROLLING_RUNNABLE, 6000);
                        }}, 6000);
                    }
                }
            });
            mHandler.postDelayed(SCROLLING_RUNNABLE, 6000);

and my adapter class is

public class TopAdapter extends RecyclerView.Adapter<TopAdapter.MyViewHolder> {
    ArrayList<location> bookslist;
    CardView cv;
    location g;
    private Home context;


    public TopAdapter(Home context, ArrayList<location> bookslist){
            this.bookslist = bookslist;
            this.context = context; // add this as a field in your adapter class.
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_top,parent,false);
            return new MyViewHolder(v);
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView teacher_name,teacher_location;
            LinearLayout profile_details;
            ImageView iv;

            MyViewHolder(final View itemView) {
                super(itemView);
                cv = (CardView)  itemView.findViewById(R.id.teacher_name);
                teacher_location = (TextView) 
            }
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {

            database = FirebaseDatabase.getInstance();

            g = bookslist.get(position);

            holder.teacher_name.setText(g.getSellername());

            holder.profile_details.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    g = bookslist.get(position);
                    Intent intent = new Intent(v.getContext(), gender_details.class);

                    intent.putExtra(KEY_NAME, g.getSellername());
                    v.getContext().startActivity(intent);
                }
            });


        @Override
        public int getItemCount() {
            return bookslist.size();
        }
    }

When I repetitively click, I am able to opens new activity with details based on position but I have no idea how It is happening and I can only reproduce same effect only after repeatedly clicking some item. But it doesn't happen often. I'm looking for permanent solution to opens new activity when I click any item from list of recycleview in one click.

Thanks in advance.

Your onClickListener setted for view with name profile_details and it is triggered only when you tap on (exactly) this view. Some of your elements may overlap this view, so your tap not work. It's explain what your startActivity triggered after some random number of taps in different parts of item.

Try to debug clicks on view and set this onItemClickListener to appropriate View/ViewGroup

PS: Also my advice is to not store any database related objects inside adapter (it's violation of single responsibility).

Also is bad practice to store activity start logic in adapter. See this answer: Can't resolve the Context or Application while navigating from Adapter of a fragment(A) to another Fragment (B)

Hope it will help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM