简体   繁体   中英

How to delete items in horizontal scroll view using slide down gesture?

I have this project App demo video that needs 6 types of images to be added in a horizontal scroll view each will have dialog fragment. How can i find the position of images in horizontal scroll view and delete view when slide down.

final String[] AddCustomitems = {"Blink single message", "Blink double message", "Message", "Scroll", "Split", "Temp"}; //list of items that can be added in layout
int[] customviewsDrawable = new int[]{R.drawable.custom_blink, R.drawable.custom_blink_double, R.drawable.custom_message, R.drawable.custom_scroll, R.drawable.custom_split_double, R.drawable.temp};


public void CustomAnimationList(final Activity activity) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //set the title for alert dialog
    builder.setTitle("Dot Matrix Add view");
    //set items to alert dialog. i.e. our array , which will be shown as list view in alert dialog
    builder.setItems(AddCustomitems, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            // setting the button text to the selected item from the list
            AddItem(item);
        }
    });

    //Creating CANCEL button in alert dialog, to dismiss the dialog box when nothing is selected
    builder.setCancelable(false)
           .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
                    //When clicked on CANCEL button the dalog will be dismissed
                    dialog.dismiss();
                }
            });
    // Creating alert dialog
    AlertDialog alert = builder.create();
    //Showing alert dialog
    alert.show();
}

     void AddItem(final int itemNum) {
                if (countItem < 9) {
                    final float scale = getResources().getDisplayMetrics().density;
                    int dpWidthInPx = (int) (175 * scale); //rescalling views
                    int dpHeightInPx = (int) (250 * scale); //rescalling views
                    final LinearLayout sv = (LinearLayout) findViewById(R.id.horizontalLayout);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
                    layoutParams.setMargins(10, 0, 0, 10); //adding margin
                    final ImageView iv = new ImageView(this);
                    iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertView(itemNum); //this will send  itemNum to switch case for corresponding dialog fragment.
                        }
                    });
                    iv.setLayoutParams(layoutParams);
                    iv.setBackgroundResource(customviewsDrawable[itemNum]);
                    sv.addView(iv);
                    countItem++;
                } else {
                    alertItem(); //alert message if items are more then 8
                }
                System.out.println("count items :" + countItem);
            }

I have implemented this by using ItemTouchHelper class.I have also added some features

1)Custom multiple dialog fragment added in Horizontal Scroll View.

2)Drag and drop feature to move items or delete item when swiped up or down

3)List the Order of items when Button is pressed

4)Alert dialog if items are more then the 8 elements.

I have committed the code on github for anyone to try out.[ https://github.com/parmarravi/HorizontalRecyclerView ]

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