0

I have a listview that contains some items, i want to allow the user to preview the data of a specific item by pressing and holding on the item. i want the preview window/popup to stay showed as long as the user is pressing.

I am trying to achivie the same preview functionality in IOS and instagram

i already implemented on longpress but not sure what to is the best thing to show to get a the desired result

 lv.setAdapter(arrayAdapter);
            lv.setLongClickable(true);
            lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                               int pos, long id) {


                    Log.v("long clicked", "pos: " +pos);

                    return true;
                }
            });

any hints on how to implement that or best way to implement it ?

NorthernLights
  • 370
  • 3
  • 16

4 Answers4

4

Well I'm using recycler view with images.

To show the image I use the long press listener calling this method:

public void publicationQuickView(Post post){
    View view = getLayoutInflater().inflate( R.layout.quick_view, null);

    ImageView postImage = (ImageView) view.findViewById(R.id.ivFeedCenter);
    ImageView profileImage = (ImageView) view.findViewById(R.id.ivUserProfile);
    TextView tvUsername = (TextView) view.findViewById(R.id.txtUsername);
    tvUsername.setText(post.user.name);

    Picasso.with(this).load(post.picture).priority(Picasso.Priority.HIGH).noPlaceholder().into(postImage);
    Picasso.with(this).load(post.user.picture).noPlaceholder().into(profileImage);

    builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));
    builder.setContentView(view);
    builder.show();
}

I inflate the layout and inject into a Dialog.

To dismiss the dialog I'm using the RecyclerView.OnItemTouchListener() like this:

rvUserProfile.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            if(e.getAction() == MotionEvent.ACTION_UP)
                hideQuickView();
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent event) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }});

And finally:

public void hideQuickView(){
    if(builder != null) builder.dismiss();
}
1

You could add a custom OnTouchListener to the view that represents a given item in you ListView (or RecyclerView or whatever). This allows you to detect when a gesture starts (i.e. first finger down) end ends (i.e. last finger up) or is canceled (e.g. gesture was actually a scroll and has been intercepted by the ListView).

The code you need to do that would look something like that:

itemView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int actionMasked = event.getActionMasked();
        switch (actionMasked) {

        case MotionEvent.ACTION_DOWN:
            // show preview
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // hide preview
            break;

            default:

        }

        return true;
    }
});

Edit: You may need to include some logic to detect a simple tap (e.g. measure if the whole gesture lasted not longer than ViewConfiguration.getTapTimeout()) and call v.performClick().

toKrause
  • 512
  • 1
  • 4
  • 13
  • Thanks for the answer, it's working fine here I added a reference [link](https://stackoverflow.com/a/60999450/5995648) for handle the click event with touch event – Arbaz.in Jul 29 '21 at 11:59
0

See the following answer on how to handle long press with recycler view: RecyclerView onClick

Once you have that, you can show a layout to display your data.

Community
  • 1
  • 1
Distwo
  • 11,569
  • 8
  • 42
  • 65
0

Check this out...

Implement a listener on long click listener

how to implement a long click listener on a listview

and then enable the visibiity of a hidden view. While doing that of course check this link out on how to enable dynamic position of the preview pane

How can I dynamically set the position of view in Android?

Community
  • 1
  • 1
Azlam
  • 2,052
  • 3
  • 24
  • 28