-
-
Save iPaulPro/2216ea5e14818056cfcc to your computer and use it in GitHub Desktop.
"Drag and swipe with RecyclerView" files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- | |
~ Copyright (C) 2015 Paul Burke | |
~ | |
~ Licensed under the Apache License, Version 2.0 (the "License"); | |
~ you may not use this file except in compliance with the License. | |
~ You may obtain a copy of the License at | |
~ | |
~ http://www.apache.org/licenses/LICENSE-2.0 | |
~ | |
~ Unless required by applicable law or agreed to in writing, software | |
~ distributed under the License is distributed on an "AS IS" BASIS, | |
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
~ See the License for the specific language governing permissions and | |
~ limitations under the License. | |
--> | |
<android.support.v7.widget.RecyclerView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- | |
~ Copyright (C) 2015 Paul Burke | |
~ | |
~ Licensed under the Apache License, Version 2.0 (the "License"); | |
~ you may not use this file except in compliance with the License. | |
~ You may obtain a copy of the License at | |
~ | |
~ http://www.apache.org/licenses/LICENSE-2.0 | |
~ | |
~ Unless required by applicable law or agreed to in writing, software | |
~ distributed under the License is distributed on an "AS IS" BASIS, | |
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
~ See the License for the specific language governing permissions and | |
~ limitations under the License. | |
--> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/text" | |
android:layout_width="wrap_content" | |
android:layout_height="?listPreferredItemHeight" | |
android:layout_gravity="center_vertical" | |
android:paddingLeft="16dp" | |
android:textAppearance="?android:attr/textAppearanceMedium" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2015 Paul Burke | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.support.v7.widget.RecyclerView; | |
import android.widget.TextView; | |
public class ItemViewHolder extends RecyclerView.ViewHolder { | |
public final TextView textView; | |
public ItemViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2015 Paul Burke | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class MainFragment extends Fragment { | |
public MainFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_main, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); | |
recyclerView.setHasFixedSize(true); | |
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); | |
RecyclerListAdapter adapter = new RecyclerListAdapter(); | |
recyclerView.setAdapter(adapter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2015 Paul Burke | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class RecyclerListAdapter extends RecyclerView.Adapter<ItemViewHolder> { | |
private static final String[] STRINGS = new String[]{ | |
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" | |
}; | |
private final List<String> mItems = new ArrayList<>(); | |
public RecyclerListAdapter() { | |
mItems.addAll(Arrays.asList(STRINGS)); | |
} | |
@Override | |
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false); | |
return new ItemViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(ItemViewHolder holder, int position) { | |
holder.textView.setText(mItems.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
return mItems.size(); | |
} | |
} |
rafagan
commented
Aug 10, 2020
package com.example.dragndropgrid
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class RecyclerListAdapter : RecyclerView.Adapter<ItemViewHolder>(), ItemTouchHelperAdapter {
private val mItems: MutableList<String> =
ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.item_main, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.textView!!.text = mItems[position]
}
override fun getItemCount(): Int {
return mItems.size
}
companion object {
private val STRINGS = arrayOf(
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
)
}
init {
mItems.addAll(listOf(*STRINGS))
}
override fun onItemMove(fromPosition: Int, toPosition: Int) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(mItems, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(mItems, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}
override fun onItemDismiss(position: Int) {
mItems.removeAt(position)
notifyItemRemoved(position)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment