2 years ago
#4757
Himanshu
Tried building a sample app where the fragments inside the Grid layout while drag and dropping would swap their place
This is my Activity.xml file.
' <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
android:layout_margin="10sp"
tools:context=".MainActivity"
android:id="@+id/gridView">
<fragment
android:id="@+id/frag1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:name="com.example.drag.Fragment1"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:layout_margin="20sp"
android:background="@color/teal_200"
>
</fragment>
<fragment
android:id="@+id/frag2"
android:name="com.example.drag.Fragment2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="fill"
android:background="@color/teal_200"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="20sp">
</fragment>
<fragment
android:id="@+id/frag3"
android:name="com.example.drag.Fragment3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_gravity="fill"
android:background="@color/teal_200"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="20sp">
</fragment>
<fragment
android:id="@+id/frag4"
android:name="com.example.drag.Fragment4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill"
android:background="@color/teal_200"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="20sp">
</fragment>
</GridLayout>'
This is my MainActivity.java file
`package com.example.drag;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.icu.text.Transliterator;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
(findViewById(R.id.frag1)).setOnDragListener(new MyDragListener());
(findViewById(R.id.frag2)).setOnDragListener(new MyDragListener());
(findViewById(R.id.frag3)).setOnDragListener(new MyDragListener());
(findViewById(R.id.frag4)).setOnDragListener(new MyDragListener());
MyDragListener myDragListener = null;
myDragListener = new MyDragListener();
(findViewById(R.id.frag1)).setOnDragListener(myDragListener);
(findViewById(R.id.frag2)).setOnDragListener(myDragListener);
(findViewById(R.id.frag3)).setOnDragListener(myDragListener);
(findViewById(R.id.frag4)).setOnDragListener(myDragListener);
(findViewById(R.id.frag1)).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
ClipData dragData = new ClipData(
(CharSequence) v.getTag(),
new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},
item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder((findViewById(R.id.frag1)));
v.startDrag(dragData, myShadow, v, 0);
return true;
}
});
(findViewById(R.id.frag2)).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
ClipData dragData = new ClipData(
(CharSequence) v.getTag(),
new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},
item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder((findViewById(R.id.frag2)));
v.startDrag(dragData, myShadow,v, 0);
return true;
}
});
(findViewById(R.id.frag3)).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
ClipData dragData = new ClipData(
(CharSequence) v.getTag(),
new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},
item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder((findViewById(R.id.frag3)));
v.startDrag(dragData, myShadow, v, 0);
return true;
}
});
(findViewById(R.id.frag4)).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
ClipData dragData = new ClipData(
(CharSequence) v.getTag(),
new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},
item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder((findViewById(R.id.frag4)));
v.startDrag(dragData, myShadow, v, 0);
return true;
}
});
}
class MyDragListener implements View.OnDragListener{
@Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
final FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (action){
case DragEvent.ACTION_DRAG_STARTED:
if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
v.setBackgroundColor(Color.BLUE);
return true;
}
return false;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(Color.GREEN);
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.BLUE);
return true;
case DragEvent.ACTION_DROP:
ClipData.Item item = event.getClipData().getItemAt(0);
CharSequence dragData = item.getText();
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container1 = (LinearLayout) v;
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
return true;
}
return false;
}
}
}`
Each fragments xml like this
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_200"
tools:context=".Fragment1"
>
<TextView
android:id="@+id/firstFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Slide 1"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</LinearLayout>`
When i tried to run it and start my dragging of fragments. the fragments start adding in the size of container rather than swapping between them. it supposedly work like when i drag one fragment and drop it to the other fragments. the fragments should swap their places.
java
android
android-fragments
android-gridlayout
0 Answers
Your Answer