RecyclerView layout click (not item click)

                In the recent development process, I encountered such a requirement: horizontal list click to jump.

              

               It can be regarded as a few years old development. The first thing that comes to mind is of course HorizontalListView .

         I also thought that after 5.0, a particularly useful RecyclerView was released , which supports various display effects of ListView.

         So I studied it and found that it was very simple to use, so I started to develop it with the RecyclerView control.

         First of all, of course, initialize RecyclerView , except for changing the default vertical layout to horizontal layout , the rest remain unchanged.

// recycleAdapter=new ProjectRecycleAdapter(this,);
         LinearLayoutManager layoutManager = new LinearLayoutManager( this ) ;
 // Set to horizontal layout . layoutManager.setOrientation(OrientationHelper.HORIZONTAL ) ;
 // Set layout manager
 recyclerView .setLayoutManager(layoutManager) ;
 // Set Adapter
 recycleAdapter = new ProjectRecycleAdapter( this ) ;
 recyclerView .setAdapter ( recycleAdapter ) ;
 //        
                                 Set the animation recyclerView to add or delete items
 .setItemAnimator( new DefaultItemAnimator() ) ;     
          The first problem encountered first, of course, is that this control does not have an OnItemClickListener listener.

       However, it can also be implemented by writing an OnClickListener monitor for the sub-layout in the adapter .

       The second question is that the requirement declares to jump when clicking on the entire layout of the horizontal list.

       The first thing I thought of was to add OnClickListener to RecyclerView , but I found that it was useless after adding it.

       The second thought is to add On ClickListener to the parent control of RecyclerView , and block the ontouch event of RecyclerView .

       But if its ontoch event is blocked, the horizontal sliding effect that comes with RecyclerView will also be lost.

       Then I started to rewrite RecyclerView's ontouch to simulate clicks .

       At first, I wanted to jump directly after listening to event.ACTION_UP or event.ACTION_DOWN, but found that the trigger was too sensitive and the experience was not good.

       Improvements were made afterwards. And realize clicking RecyclerView to make the following Text View change the pressing action according to the clicking state.

recyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Drawable drawable = getResources().getDrawable(R.drawable.back_default ) ; // Normal state
         Drawable drawable_pressed = getResources().getDrawable(R.drawable.back_pressed ) ; // Pressed state
 int w = drawable.getIntrinsicWidth() ; // Image original width
 int h = drawable.getIntrinsicHeight() ; // The original height of the image
 drawable.setBounds( 0 , 0 , w , h) ;
 drawable_pressed.setBounds( 0 , 0 , w , h) ;
                                        if (event.getAction() == event.ACTION_DOWN) {
            scrollX = event.getX();
            scrollY = event.getY();
            member_num.setTextColor(getResources().getColor(R.color.project_member_pressed));
            member_num.setCompoundDrawables(null, null, drawable_pressed, null);
        }
        if (event.getAction() == event.ACTION_UP) {
            member_num.setTextColor(getResources().getColor(R.color.project_member));
            member_num.setCompoundDrawables(null, null, drawable, null);
            if (Math.abs(scrollX - event.getX()) <= 5 && Math.abs(scrollY - event.getY()) <= 5) {
                Intent intent = new Intent(ProjectDetailActivity.this, ProjectMemberListActivity.class);
                startActivity(intent);
            }
        }
        return false;
    }
});

   I hope it will be helpful to students who encounter the same problem. Thanks.


Guess you like

Origin blog.csdn.net/u010351988/article/details/54290791