如何使用向下滑动手势删除水平滚动视图中的项目?

时间:2017-11-05 07:33:58

标签: java android listview horizontal-scrolling

我有这个项目App demo video需要在水平滚动视图中添加6种类型的图像,每个图像都有对话框片段。 如何在水平滚动视图中找到图像的位置,并在向下滑动时删除视图。

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);
            }

1 个答案:

答案 0 :(得分:0)

我已经使用ItemTouchHelper类实现了这一点。我还添加了一些功能

1)在水平滚动视图中添加自定义多个对话框片段。

2)拖放功能可在向上或向下滑动时移动项目或删除项目

3)按下按钮时列出项目顺序

4)如果项目超过8个元素,则为警告对话框。

我已经在github上提交了代码供任何人试用。[https://github.com/parmarravi/HorizontalRecyclerView]