Skip to content

Commit 8f26ed3

Browse files
committed
Fixed Bug: Cannot Drag and drop to rearrange Items
In the Main Activity I implemented a simple call back ItemTouchHelper to allow movement then added Collection Swap from to position to allow new position to be taken
1 parent 7bce7a4 commit 8f26ed3

19 files changed

Lines changed: 146 additions & 25 deletions

File tree

.idea/misc.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ android {
2525
sourceCompatibility JavaVersion.VERSION_1_8
2626
targetCompatibility JavaVersion.VERSION_1_8
2727
}
28+
buildFeatures {
29+
viewBinding true
30+
}
2831
}
2932

3033
dependencies {
@@ -34,6 +37,8 @@ dependencies {
3437
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
3538
//Androidx room
3639
implementation "androidx.room:room-runtime:2.4.2"
40+
implementation 'androidx.navigation:navigation-fragment:2.4.2'
41+
implementation 'androidx.navigation:navigation-ui:2.4.2'
3742
annotationProcessor "androidx.room:room-compiler:2.4.2"
3843
testImplementation 'junit:junit:4.13.2'
3944
androidTestImplementation 'androidx.test.ext:junit:1.1.3'

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4-
package="com.btn.pronotes">
4+
package="com.btn.pronotes" >
55

66
<application
77
android:allowBackup="true"
@@ -12,14 +12,14 @@
1212
android:roundIcon="@mipmap/ic_launcher_round"
1313
android:supportsRtl="true"
1414
android:theme="@style/Theme.ProNotes"
15-
tools:targetApi="31">
15+
tools:targetApi="31" >
1616
<activity
1717
android:name=".NotesTakerActivity"
18-
android:theme="@style/Theme.ProNotes.NoActionBar"
19-
android:exported="false" />
18+
android:exported="false"
19+
android:theme="@style/Theme.ProNotes.NoActionBar" />
2020
<activity
2121
android:name=".MainActivity"
22-
android:exported="true">
22+
android:exported="true" >
2323
<intent-filter>
2424
<action android:name="android.intent.action.MAIN" />
2525

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.btn.pronotes.Adapters;
2+
3+
public interface ItemTouchHelperAdapter {
4+
}

app/src/main/java/com/btn/pronotes/Adapters/NotesListAdapter.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.btn.pronotes.Adapters;
22

3+
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
4+
35
import android.content.Context;
46
import android.os.Build;
5-
import android.text.Layout;
7+
import android.os.Bundle;
8+
import android.view.DragEvent;
69
import android.view.LayoutInflater;
710
import android.view.View;
811
import android.view.ViewGroup;
@@ -12,22 +15,24 @@
1215
import androidx.annotation.NonNull;
1316
import androidx.annotation.RequiresApi;
1417
import androidx.cardview.widget.CardView;
18+
import androidx.recyclerview.widget.ItemTouchHelper;
1519
import androidx.recyclerview.widget.RecyclerView;
1620

1721
import com.btn.pronotes.Models.Notes;
1822
import com.btn.pronotes.NotesClickListener;
1923
import com.btn.pronotes.R;
2024

2125
import java.util.ArrayList;
26+
import java.util.Collections;
2227
import java.util.List;
2328
import java.util.Random;
2429

2530
//Notes List Adapter Class
2631
public class NotesListAdapter extends RecyclerView.Adapter<NotesViewHolder>
27-
{
28-
Context context;
29-
List<Notes> list;
30-
NotesClickListener listener;
32+
implements ItemTouchHelperAdapter {
33+
Context context;
34+
List<Notes> list;
35+
NotesClickListener listener;
3136

3237
public NotesListAdapter(Context context, List<Notes> list, NotesClickListener listener) {
3338
this.context = context;
@@ -61,12 +66,14 @@ public void onBindViewHolder(@NonNull NotesViewHolder holder, int position) {
6166

6267
int color_code = getRandomColor();
6368
holder.notes_container.setCardBackgroundColor(holder.itemView.getResources().getColor(color_code, null));
69+
6470
holder.notes_container.setOnClickListener(new View.OnClickListener() {
6571
@Override
6672
public void onClick(View v) {
6773
listener.onClick(list.get(holder.getAdapterPosition()));
6874
}
6975
});
76+
7077
holder.notes_container.setOnLongClickListener(new View.OnLongClickListener() {
7178
@Override
7279
public boolean onLongClick(View v) {
@@ -104,9 +111,9 @@ public void filterList(List<Notes> filteredList){
104111

105112
//Notes View Holder Class
106113
class NotesViewHolder extends RecyclerView.ViewHolder {
107-
CardView notes_container;
108-
TextView textView_title,textView_notes, textView_date;
109-
ImageView imageView_pin;
114+
CardView notes_container;
115+
TextView textView_title, textView_notes, textView_date;
116+
ImageView imageView_pin;
110117

111118
public NotesViewHolder(@NonNull View itemView) {
112119
super(itemView);

app/src/main/java/com/btn/pronotes/MainActivity.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.btn.pronotes;
22

3+
import androidx.annotation.NonNull;
34
import androidx.annotation.Nullable;
45
import androidx.appcompat.app.AppCompatActivity;
56
import androidx.cardview.widget.CardView;
7+
import androidx.recyclerview.widget.ItemTouchHelper;
68
import androidx.recyclerview.widget.LinearLayoutManager;
79
import androidx.recyclerview.widget.RecyclerView;
810
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
911

1012
import android.app.Activity;
1113
import android.content.Intent;
1214
import android.os.Bundle;
15+
import android.view.Menu;
16+
import android.view.MenuInflater;
1317
import android.view.MenuItem;
1418
import android.view.View;
1519
import android.widget.PopupMenu;
@@ -22,6 +26,7 @@
2226
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2327

2428
import java.util.ArrayList;
29+
import java.util.Collections;
2530
import java.util.List;
2631
import java.util.Locale;
2732

@@ -56,6 +61,30 @@ public void onClick(View v) {
5661
}
5762
});
5863

64+
// Impliments the drag and drop movement of the notes
65+
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.START | ItemTouchHelper.END, 0) {
66+
@Override
67+
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
68+
69+
int fromPosition = viewHolder.getAdapterPosition();
70+
int toPosition = target.getAdapterPosition();
71+
72+
Collections.swap(notes, fromPosition, toPosition);
73+
74+
recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);
75+
76+
return false;
77+
}
78+
@Override
79+
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
80+
81+
}
82+
};
83+
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
84+
itemTouchHelper.attachToRecyclerView(recyclerView);
85+
86+
// End of drag and drop movement
87+
5988
//search box home code
6089
searchView_home.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
6190
@Override
@@ -70,7 +99,24 @@ public boolean onQueryTextChange(String newText) {
7099
}
71100
});
72101
}
73-
//note search filter code
102+
@Override
103+
public boolean onCreateOptionsMenu(Menu menu) {
104+
MenuInflater inflater = getMenuInflater();
105+
inflater.inflate(R.menu.info_menu, menu);
106+
return true;
107+
}
108+
109+
@Override
110+
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
111+
switch (item.getItemId()){
112+
case R.id.help:
113+
Toast.makeText(this, "Email: support@Brett-TechRepair.com", Toast.LENGTH_LONG).show();
114+
return true;
115+
}
116+
return super.onOptionsItemSelected(item);
117+
}
118+
119+
//note search filter code
74120
private void filter(String newText) {
75121
List<Notes> filteredList = new ArrayList<>();
76122
for (Notes singleNote : notes){
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M19,2L5,2c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h4l3,3 3,-3h4c1.1,0 2,-0.9 2,-2L21,4c0,-1.1 -0.9,-2 -2,-2zM13,18h-2v-2h2v2zM15.07,10.25l-0.9,0.92C13.45,11.9 13,12.5 13,14h-2v-0.5c0,-1.1 0.45,-2.1 1.17,-2.83l1.24,-1.26c0.37,-0.36 0.59,-0.86 0.59,-1.41 0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2L8,8c0,-2.21 1.79,-4 4,-4s4,1.79 4,4c0,0.88 -0.36,1.68 -0.93,2.25z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM15.1,8L8.9,8L8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2z"/>
5+
</vector>

app/src/main/res/layout/activity_main.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
<SearchView
1212
android:id="@+id/searchView_home"
13+
android:layout_width="match_parent"
14+
android:layout_height="?attr/actionBarSize"
1315
android:layout_alignParentTop="true"
14-
android:layout_margin="16dp"
16+
android:layout_marginStart="16dp"
17+
android:layout_marginTop="16dp"
18+
android:layout_marginEnd="16dp"
19+
android:layout_marginBottom="16dp"
1520
android:background="@color/grey"
16-
android:queryHint="Search Notes..."
1721
android:iconifiedByDefault="false"
18-
android:layout_width="match_parent"
19-
android:layout_height="?attr/actionBarSize"/>
22+
android:queryHint="Search Notes...">
23+
24+
</SearchView>
2025

2126
<androidx.recyclerview.widget.RecyclerView
2227
android:id="@+id/recycler_home"

app/src/main/res/layout/activity_notes_taker.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
android:background="@android:color/transparent"
2424
android:padding="4dp"
2525
android:src="@drawable/ic_save" />
26+
27+
<!--For back button-->
2628
<ImageView
2729
android:id="@+id/imageView_back"
2830
android:layout_width="?attr/actionBarSize"
2931
android:layout_height="?attr/actionBarSize"
3032
android:layout_alignParentStart="true"
3133
android:background="@android:color/transparent"
3234
android:padding="4dp"
33-
android:src="@drawable/ic_back" />
35+
android:src="@drawable/ic_back"
36+
android:visibility="gone" />
3437
</RelativeLayout>
3538

3639

@@ -51,6 +54,7 @@
5154
android:id="@+id/editText_notes"
5255
android:hint="Add Notes: "
5356
android:textColorHint="@color/white"
57+
android:layout_marginTop="15dp"
5458
android:padding="8dp"
5559
android:textColor="@color/white"
5660
android:textSize="18sp"

0 commit comments

Comments
 (0)