Android DragLinearLayout dosn无法正常工作,拖放效果差,或者几乎不拖动图像

时间:2018-04-11 00:18:05

标签: android android-linearlayout

我正在使用this library

我以编程方式添加ImageViews,但图像几乎无法拖动。它需要很长时间的按压,有时它可以工作,有时它不会。

这是添加功能

爪哇

private ScrollView llCanvusContainer;
private DragLinearLayout llCanvus;
llCanvus = (DragLinearLayout)findViewById(R.id.llCanvus);
llCanvusContainer = (ScrollView)findViewById(R.id.llCanvusContainer);
llCanvus.setContainerScrollView(llCanvusContainer);

private void appendImageToLinearLayout(DragLinearLayout layout,
                                           ParseFile parseFileImage,
                                           int w, int h,
                                           boolean fullWidth,
                                           boolean isPostImage,
                                           final int position)
            throws ParseException, IOException {

        ImageView imageView= new ImageView(this);
        //imageView.setBackgroundResource(R.drawable.ic_action_search);
        imageView.setImageBitmap(

                ImageTools.
                        getBitmapFromUri(Uri.fromFile(
                                parseFileImage.getFile()),
                                this, fullWidth
                        )
        );

        // params
        //DragLinearLayout.LayoutParams viewParamsCenter =
                new DragLinearLayout.LayoutParams(
                        w,
                        h);
        //viewParamsCenter.gravity = Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;

        //imageView.setLayoutParams(viewParamsCenter);
        imageView.setId(position);
        imageView.setContentDescription(imageList.get(position).getObjectId());
        layout.addDragView(imageView, imageView);

        //layout.setViewDraggable(imageView, imageView);

        if(isPostImage){

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //showImageFullScreen(position);
                    Toast.makeText(EditPostActivity.this, "clicked " + v.getId(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

以下是我添加图片视图的方法

        for (ParseObject imgObj : imageList) {

            // add the image to the linear layout
            appendImageToLinearLayout(
                    llCanvus,
                    imgObj.getParseFile("image"),
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    true,
                    true,
                    i
            );

XML

<ScrollView
    android:id="@+id/llCanvusContainer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/switchIsPublic">

    <com.jmedeisis.draglinearlayout.DragLinearLayout
        android:id="@+id/llCanvus"
        style="@style/Gallery19crollbarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/switchIsPublic" />

</ScrollView>

图像显示正确,但拖动无法正常工作。

修改

当我删除ScrollView时,拖放工作正常。

1 个答案:

答案 0 :(得分:0)

如果您不介意使用recyclerView然后使用linearlayout,您可以愉快地使用ItemTouchHelper

我使用ItemTouchHelper重新排序我的列表,它运行良好。

以下是我找到的blog post