Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Update the position of the dragged file #53

Open
aloklearning opened this issue Nov 2, 2017 · 1 comment
Open

Cannot Update the position of the dragged file #53

aloklearning opened this issue Nov 2, 2017 · 1 comment

Comments

@aloklearning
Copy link

Since my drag and drop is working fine but still the item which has been moved is not updating its position. The code is like this :

public class EditVideoActivity extends AppCompatActivity implements OnStartDragListener{

private ImageButton filterButton,cutButton;
//audioButton;

ArrayList<File> mList;
ArrayList<File> files;
private static int position = 0;

RecyclerView recyclerView;
private String selectedVideo;

private int selectedPos;

ViewAdapter viewAdapter;

RecyclerView.LayoutManager layoutManager;

private VideoView mVideoView;

private MediaController mController;

private Project project;

private ItemTouchHelper itemTouchHelper;

//private static final int CustomizeAudio = 1;
private static final int CustomizeFilter = 2;
private static final int CustomCutvideo = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_edit_video);

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.edit_header_layout);
    getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_keyboard_arrow_left_black_24dp);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mVideoView = (VideoView) findViewById(R.id.editVideoPlayer);

    filterButton = (ImageButton) findViewById(R.id.filterVideo);
    cutButton = (ImageButton) findViewById(R.id.cutVideo);
    //audioButton = (ImageButton) findViewById(R.id.musicalNotes);

    //change the color of the imagebutton explicitely
    filterButton.setColorFilter(Color.BLACK);
    cutButton.setColorFilter(Color.BLACK);
    //audioButton.setColorFilter(Color.BLACK);

    recyclerView = (RecyclerView) findViewById(R.id.videoGallery);
    recyclerView.setHasFixedSize(true);

    mList = new ArrayList<>();

    int view = getIntent().getIntExtra("view", 0);

    //this is for admin activity else addfragment
    if(view == 1){
        project = (Project) getIntent().getSerializableExtra("groupVideoData");
        Log.e("PROJECT_ID", String.valueOf(project.getId()));
        Log.e("PROJECT_DATA", String.valueOf(project.getVideoData().size()));

        for(String adminPath : project.getVideoData()){
            File files = new File(adminPath);
            mList.add(files);
        }


        //inflating the layout horizontally
        layoutManager = new LinearLayoutManager(getParent(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        viewAdapter = new ViewAdapter(mList);
        Log.e("LIST====", mList.toString());
        recyclerView.setAdapter(viewAdapter);
        recyclerView.setVisibility(View.VISIBLE);

        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallBack(viewAdapter);
        itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(recyclerView);

    }else {
        //getting the value from project class object
        project = (Project) getIntent().getSerializableExtra("passedData");
        Log.e("project id====", String.valueOf(project.getId()));
        Log.e("project data =====", String.valueOf(project.getVideoData().size()));
        //Log.e("RECEIVED DATA=====", receivedData.toString());

        for (String path : project.getVideoData()) {
            //initialising path with file
            File file = new File(path);
            Log.e("FILENAME===", path);
            mList.add(file); //adding file in array list<file> object
        }

        //inflating the layout horizontally
        layoutManager = new LinearLayoutManager(getParent(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        viewAdapter = new ViewAdapter(mList);
        Log.e("LIST====", mList.toString());
        recyclerView.setAdapter(viewAdapter);
        recyclerView.setVisibility(View.VISIBLE);

        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallBack(viewAdapter);
        itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

}
//just for a change
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.edit_video, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;

        case R.id.moveNext:
            Intent intent = new Intent(EditVideoActivity.this,PreviewActivity.class);
            //sending the file in string array list form to avoid mismatch of the file to string
            //conversion
            ArrayList<String> files = new ArrayList<>();
            for(File file : mList){
                files.add(file.getAbsolutePath());
            }
            intent.putStringArrayListExtra("videosList",files);
            intent.putExtra("videos", project);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    moveTaskToBack(false);
}

@Override
public void onDragStart(RecyclerView.ViewHolder viewHolder) {
    itemTouchHelper.startDrag(viewHolder);
}`

and my RecyclerViewAdapter is here . :

public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.ViewHolder> implements 
  ItemTouchHelperAdapter{
```


        private View rootView;

        private Bitmap bitmap;

        //calling the mList in constructor's argument item
        public ViewAdapter(ArrayList<File> items) {
            files = items;
            Log.e("ITEMS====",items.toString());
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                Log.e("ADAPTER","VIEW HOLDER CREATED");
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);

            rootView = inflater.inflate(R.layout.custom_listview,parent,false);
            ViewHolder viewholder =  new ViewHolder(rootView);
            return viewholder;
        }

        @Override
        public void onBindViewHolder( ViewHolder holder, final int position) {
            Log.e("ADAPTER",files.get(position).toString());
            EditVideoActivity.position = position;
            if (files != null) {

                bitmap = ThumbnailUtils.createVideoThumbnail(files.get(position).toString(),1);
                holder.mImageView.setImageBitmap(bitmap);
                //click event from the holder
                holder.mImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //getting the details of the selected video
                        updatedVideo();
                        Log.e("POS_SELECTED", String.valueOf(files.indexOf(files.get(position))));
                    }
                });

            }

        }

        @Override
        public int getItemCount() {
            return files.size();
        }


        @Override
        public boolean onItemMove(int fromPosition, int toPosition) {
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(files, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(files, i, i - 1);
                }
            }
            notifyItemMoved(fromPosition, toPosition);
            return true;
        }

        @Override
        public void onItemDismiss(int position) {

        }


        //holder holding the image object from the custom listView
          class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView mImageView;
            public String videoPosition;
            public int videoPos;

              public ViewHolder(View view) {
                  super(view);

                  mImageView = (ImageView) view.findViewById(R.id.imageView);

                 //  -----------  FILTER EDITING   -----------------
                  filterButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          videoPosition = selectedVideo;
                          videoPos = selectedPos;

                          if(videoPosition != null){
                              Intent intent = new Intent(EditVideoActivity.this, FilterActivity.class);
                              intent.putExtra("videoData",videoPosition);
                              intent.putExtra("position",selectedPos); //sending the data of the position
                              Log.e("VIDEO_SENT_DATA=======", videoPosition);
                              Log.e("POSITION===",String.valueOf(selectedPos));
                              startActivityForResult(intent,CustomizeFilter);
                          }else
                              Toast.makeText(EditVideoActivity.this,"You have not selected any video",Toast.LENGTH_LONG)
                                      .show();
                      }
                  });

                  // --------------- CUT VIDEO  ----------------
                  cutButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          videoPosition = selectedVideo;
                          videoPos = selectedPos;

                          if(videoPosition != null){
                              Intent intent = new Intent(EditVideoActivity.this,CutVideoActivity.class);
                              intent.putExtra("videoData",videoPosition);
                              intent.putExtra("position",selectedPos); //sending the data of the position
                              Log.e("VIDEO_SENT_DATA=======", videoPosition);
                              Log.e("POSITION===",String.valueOf(selectedPos));
                              startActivityForResult(intent,CustomCutvideo);
                          }else
                              Toast.makeText(EditVideoActivity.this,"You have not selected any video",Toast.LENGTH_LONG)
                                      .show();
                      }
                  });
              }

        }
    }

    private void updatedVideo() {

        //getting video position for passing the data
        selectedVideo = files.get(position).toString();

        //getting the position of the selected one
        selectedPos = files.indexOf(files.get(position));

        //for starting the video activity
        mVideoView.setVideoPath(files.get(position).toString());
        mController = new MediaController(EditVideoActivity.this);
        mController.setMediaPlayer(mVideoView);
        mVideoView.setMediaController(mController);
        mVideoView.requestFocus();
        mVideoView.start();
    }`

In my logcat if I select the first one that is at pos = 0 then after dragging to pos = 1 and selecting the same again it will give the selected pos = 0 only.
@sahilgargkis
Copy link

try changing onItemMove like this,

@Override
public boolean onItemMove (int fromPosition, int toPosition) {
    File item = files.get(fromPosition);
    files.remove(fromPosition);
    files.add(toPosition, item);

    notifyItemMoved(fromPosition, toPosition);
    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants