I have an issue. Since the Card class is deprecated and the CardBuilder.EMBED_INSIDE is fairly limited. The only option is to use a custom View. I'd also like to use the CardScrollView and the CardScrollAdapter.
visit Google Glass Immersion Custom Layout without CardBuilder.Layout.EMBED_INSIDE
But my problem is, I can't have multiple views.
Here is MyCustomViewClass:
public class MyCustomView extends FrameLayout{
public MyCustomView (Context context) {
super(context);
initView();
}
private void initView()
{
View view = inflate(getContext(), R.layout.imageview, null);
addView(view);
View view2 = inflate(getContext(), R.layout.secondview, null);
addView(view2);
}
And thats my main activity class:
public class InspectionActivity extends Activity {
private CardScrollView mCardScroller;
private GestureDetector mGestureDetector;
private View mView;
private CardScrollView _cardScroller;
private ArrayList<View> _cardsList;
private MyCustomView _myView;
protected List<CardBuilder> mCards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createCards();
_cardsList = new ArrayList<View>();
_myView= new MyCustomView (this);
_cardsList.add(_myView);
_cardScroller = new CardScrollView(this) ;
MainCardsScrollAdapter adapter = new MainCardsScrollAdapter(_cardsList);
_cardScroller.setAdapter(adapter);
_cardScroller.activate();
setContentView(_cardScroller);
}
private void createCards() {
mCards = new ArrayList<CardBuilder>();
}
public class MainCardsScrollAdapter extends CardScrollAdapter
{
ArrayList<View> _cardsList;
public MainCardsScrollAdapter(ArrayList<View> cardsList)
{
_cardsList = cardsList;
}
#Override
public int getCount() {
return _cardsList.size();
}
#Override
public Object getItem(int i) {
return _cardsList.get(i);
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
return _cardsList.get(i);
}
#Override
public int getPosition(Object o) {
return _cardsList.indexOf(o);
}
#Override
public int getViewTypeCount() {
return CardBuilder.getViewTypeCount();
}
#Override
public int getItemViewType(int position){
return 2;//should be changed, it's just an example
}
} }
Alright so from your post I'm guessing it's only inflating one of your layouts into the CardScrollView try the following.
Create a adapter class that looks something like this
public class mainAdapter extends CardScrollAdapter {
private List<CustomCard> mCards;
private LayoutInflater inflater;
public mainAdapter(List<CustomCard> cards, LayoutInflater inf)
{
this.mCards = cards;
this.inflater = inf;
}
#Override
public int getCount() {
return mCards.size();
}
#Override
public Object getItem(int i) {
return mCards.get(i);
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
int card = mCards.get(i).getLayout();
view = inflater.inflate(card, viewGroup, false);
return view;
}
#Override
public int getPosition(Object o) {
return this.mCards.indexOf(o);
}
}
my CustomCard class looks like this, you could just use a List<Integer> instead tho
public class CustomCard {
public int getLayout() {
return layout;
}
public int layout;
public CustomCard(int layout)
{
this.layout = layout;
}
}
In your activity class create and fill a list with the desired layouts and pass them to your adapter as follows.
in the onCreate()
CreateCards();
mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(new mainAdapter(mCards, getLayoutInflater()));
and the CreateCards() method would look something like this
public void CreateCards() {
mCards.add(new CustomCard(R.layout.firstview));
mCards.add(new CustomCard(R.layout.secondview));
mCards.add(new CustomCard(R.layout.thirdview));
}
Hope this is of use for you
Related
I have a RecyclerView in which the user can drag and drop an item to a different position in the RecyclerView.
So far everything works fine.
My problem begins when the RecyclerView has more items than it can display. So when it recycle his content. When the user drag and drop an item to a different position and than scroll away from this part the position changes are not saved. The user just see the old positions for the items.
You can see this issue in the .gif below.
I already tried several things out like:
recyclerViewItem.getRecycledViewPool().setMaxRecycledViews(0,0);
recyclerViewItem.setItemViewCacheSize(30);
and
holder.setIsRecyclable(false);
inside of my onBindViewHolder.
But nothing seems to work for me.
Does anyone know a solution for this problem?
My Fragment class:
public class ShoppinglistFragment extends Fragment {
private ViewModel viewModel;
private ShoppinglistAdapter adapterShoppingItem = new ShoppinglistAdapter();
private RecyclerView recyclerViewItem;
private Boolean fragmentStarted = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shoppinglist_layout, container, false);
recyclerViewItem = view.findViewById(R.id.recyclerViewShoppinglist);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel = ViewModelProviders.of(getActivity()).get(ViewModel.class);
fragmentStarted = true;
recyclerViewItem.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewItem.setHasFixedSize(false);
recyclerViewItem.getRecycledViewPool().setMaxRecycledViews(0,0);
recyclerViewItem.setItemViewCacheSize(30);
recyclerViewItem.setAdapter(adapterShoppingItem);
ItemTouchHelper.Callback callback =
new ItemMoveCallback(adapterShoppingItem);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerViewItem);
//fetch all Shoppinglist Items from local storage
viewModel.getAllShoppinglistItems().observe((LifecycleOwner) this, new Observer <List<Shoppinglist>>() {
#Override
public void onChanged(#Nullable List<Shoppinglist> items) {
if (fragmentStarted){
adapterShoppingItem.submitList(items);
fragmentStarted = false;
}
}
});
adapterShoppingItem.setOnPositionChanged(new ShoppinglistAdapter.onPositionChanged() {
#Override
public void onPositionChanged(Shoppinglist fromItem, Shoppinglist toItem, int fromPosition, int toPosition) {
int fromPositionServer = fromItem.getPosition();
int toPositionServer = toItem.getPosition();
fromItem.setPosition(toPositionServer);
toItem.setPosition(fromPositionServer);
//updating Shoppinglist Item locally
viewModel.updateItem(fromItem);
viewModel.updateItem(toItem);
}
});
}
}}
and my Adapter class:
public class ShoppinglistAdapter extends ListAdapter<Shoppinglist, ShoppinglistAdapter.NoteHolder> implements ItemMoveCallback.ItemTouchHelperContract {
public Integer ressourceType;
private onPositionChanged onPositionChanged;
private ArrayList<Shoppinglist> globalItemList = new ArrayList<Shoppinglist>();
public ShoppinglistAdapter() {
super(DIFF_CALLBACK);
Integer valueOf = Integer.valueOf(0);
this.ressourceType = valueOf;
}
private static final DiffUtil.ItemCallback<Shoppinglist> DIFF_CALLBACK = new DiffUtil.ItemCallback<Shoppinglist>() {
#Override
public boolean areItemsTheSame(Shoppinglist oldItem, Shoppinglist newItem) {
return oldItem.getSqlid() == newItem.getSqlid();
}
#Override
public boolean areContentsTheSame(Shoppinglist oldItem, Shoppinglist newItem) {
return oldItem.getTitle().equals(newItem.getTitle());
}
};
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shoppinglist_item, parent, false);
return new NoteHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position) {
Shoppinglist currentItem = getItem(position);
holder.setIsRecyclable(false);
holder.tv.setText(currentItem.getTitle());
...
}
public Shoppinglist getNoteAt(int position) {
return getItem(position);
}
public class NoteHolder extends RecyclerView.ViewHolder {
private TextView tv;
public NoteHolder(View itemView) {
super(itemView);
tv= itemView.findViewById(R.id.tv);
globalItemList .add(currentNote);
}
}
public interface onPositionChanged {
void onPositionChanged(Shoppinglist fromItem, Shoppinglist toItem, int fromPosition, int toPosition);
}
public void setOnPositionChanged(ShoppinglistAdapter.onPositionChanged listener) {
this.onPositionChanged = listener;
}
#Override
public void onRowMoved(int fromPosition, int toPosition) {
//send switched Items to Fragment
onPositionChanged.onPositionChanged(globalItemList.get(fromPosition), globalItemList.get(toPosition), fromPosition, toPosition);
//switch Items inside of the global Item List
Collections.swap(globalItemList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}
#Override
public void onRowSelected(NoteHolder myViewHolder) {}
#Override
public void onRowClear(NoteHolder myViewHolder) {}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
The problem is that you should swap items in ListAdapter.mDiffer which holds the current showing list. So, globalItemList in your code is unused. Replace onRowMoved of your adapter with the following one:
#Override
public void onRowMoved(int fromPosition, int toPosition) {
ArrayList<Shoppinglist> list = new ArrayList(getCurrentList());
Collections.swap(list, fromPosition, toPosition);
submitList(list);
}
Here is what I was trying to do. addNewQRCard() is onclick event fired by a button to add new images to viewpager. I have to always set the adapter to new items. But why in my case onStart() doesn't do that job. Please give me the best practice that I should use to add and delete views dynamically from viewPager2.
public class AddPage extends AppCompatActivity {
private ViewPager2 viewPager2;
private ArrayList<ModelItem> items;
private AdapterSlider adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_qrcode);
viewPager2 = findViewById(R.id.viewPagerImageSlider);
items = new ArrayList<>();
items.add(new ModelItem(R.drawable.image1));
adapter = new AdapterSlider(items,viewPager2);
viewPager2.setAdapter(adapter);
viewPager2.setClipToPadding(false);
viewPager2.setClipChildren(false);
viewPager2.setOffscreenPageLimit(3);
viewPager2.getChildAt(0).setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
compositePageTransformer.addTransformer(new MarginPageTransformer(40));
compositePageTransformer.addTransformer(new ViewPager2.PageTransformer() {
#Override
public void transformPage(#NonNull View page, float position) {
float r = 1 - Math.abs(position);
page.setScaleY(0.85f + r * 0.15f);
}
});
viewPager2.setPageTransformer(compositePageTransformer);
}
#Override
protected void onStart() {
super.onStart();
adapter = new AdapterQRSlider(sliderItems,viewPager2);
viewPager2.setAdapter(adapter);
}
public void addNewQRCard(View view) { //this is the button which i used to add new childs
items.add(new ModelSlider(R.drawable.image2));
adapter = new AdapterSlider(items,viewPager2);
viewPager2.setAdapter(adapter);
}
}
Adapter class
public class AdapterSliderextends RecyclerView.Adapter<AdapterSlider.ViewHolder>{
private ArrayList<ModelSlider> items;
private ViewPager2 viewPager2;
public AdapterSlider(ArrayList<ModelSlider> items, ViewPager2 viewPager2) {
this.items= items;
this.viewPager2 = viewPager2;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.slide_item_container,
parent,
false
)
);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.setImage(items.get(position));
}
#Override
public int getItemCount() {
return items.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageSlide);
}
void setImage(ModelSlider items){
imageView.setImageResource(sliderItem.getImage());
}
}
}
Model Class
public class ModelSlider {
private int image;
public ModelSlider(int image){
this.image = image;
}
public int getImage() {
return image;
}
}
You can notify adapter when you adding new items just like the way you do it with RecyclerView. Something like this
public void addNewQRCard(View view) {
items.add(new ModelSlider(R.drawable.image2));
// instead of of resetting adapter to ViewPager,
// just notify its adapter of item inserted
adapter.notifyItemInserted(items.size - 1)
}
I'm trying to make a tab layout with each page having a RecyclerView inside of it. I'm using fragments with RecyclerView in them, then i put them inside of ViewPager. The problem is that even though tabs are being shown it's content is always empty.
My Activity class
public class Spells_Act extends AppCompatActivity {
private ViewPager pager;
private Spells_Slide slider;
private TabLayout tabLayout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spells);
tabLayout = findViewById(R.id.tab);
pager = findViewById(R.id.pager);
slider = new Spells_Slide(getSupportFragmentManager());
slider.AddFragment(new Fragment1(), "one");
slider.AddFragment(new Fragment1(), "two");
slider.AddFragment(new Fragment1(), "three");
pager.setAdapter(slider);
tabLayout.setupWithViewPager(pager);
}
}
My ViewPager adapter
public class Spells_Slide extends FragmentPagerAdapter {
private Context context;
LayoutInflater inflater;
private List<RecyclerView> pages;
private List<String> list = new LinkedList<>();
private final List<Fragment> stFragment = new ArrayList();
public Spells_Slide(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return stFragment.get(position);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
#Override
public int getCount() {
return list.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object
object) {
return false;
}
public void AddFragment(Fragment fragment, String title) {
list.add(title);
stFragment.add(fragment);
}
My Fragment class
public class Fragment1 extends Fragment {
View v;
private RecyclerView recycler;
Adapter_Rec adapter;
private List<Spells> Spells = new LinkedList<>();
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spells.add(new Spells("Ghost Sound", "Figment sounds", 0));
Spells.add(new Spells("Disrupt Undead", " Deals 1d6 damage to one undead", 0));
Spells.add(new Spells("Resistance", "Subject gains +1 on saving throws", 0));
Spells.add(new Spells("Ray of Frost", "Ray deals 1d3 cold damage", 0));
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.frag1, container, false);
recycler = v.findViewById(R.id.frag_recycler);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new Adapter_Rec(Spells, getContext());
recycler.setAdapter(adapter);
return v;
}
}
And finally my RecyclerViewAdapter
public class Adapter_Rec extends
RecyclerView.Adapter<Adapter_Rec.MyViewHolder> {
private List<Spells> list = new LinkedList<>();
Context context;
public Adapter_Rec(List<Spells> list, Context context)
{
this.context=context;
this.list=list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(context).inflate
(R.layout.spells_recycle,parent,false);
MyViewHolder vholder = new MyViewHolder(v);
return vholder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.check.setOnCheckedChangeListener(null);
holder.name.setText(list.get(position).getName());
holder.benefits.setText(list.get(position).getDescription());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
TextView name;
TextView benefits;
CheckBox check ;
public MyViewHolder(View item)
{ super(item);
name = item.findViewById(R.id.namee);
benefits = item.findViewById(R.id.desc);
check =item.findViewById(R.id.check);
}
}
}
delete isViewFromObject method, or return super.isViewFromObject(view, object);
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object
object) {
//return false; // return false will never show this fragment
return super.isViewFromObject(view, object);
}
I am trying to implement simple recycler view in my Activity based on the following tutorial from official AndroidAnnotations site: adapter link
However when I finish all the steps, and I try to implement #ItemClick method, it gives me the following error:
Error:(84, 36) error: incompatible types: RecyclerView cannot be converted to AdapterView
with the following code highlighted under my generated activity code:
...((AdapterView<?> ) this.miestnostiList).setOnItemClickListener(new OnItemClickListener() {...
As per guide, I have created the following base classes:
public abstract class RecyclerViewAdapterBase<T, V extends View> extends RecyclerView.Adapter<ViewWrapper<V>> {
protected List<T> items = new ArrayList<T>();
#Override
public int getItemCount() {
return items.size();
}
#Override
public final ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewWrapper<V>(onCreateItemView(parent, viewType));
}
protected abstract V onCreateItemView(ViewGroup parent, int viewType);
}
public class ViewWrapper<V extends View> extends RecyclerView.ViewHolder {
private V view;
public ViewWrapper(V itemView) {
super(itemView);
view = itemView;
}
public V getView() {
return view;
}
}
Then I created my row view item as following:
#EViewGroup(R.layout.activity_list_miestnosti_row)
public class MiestnostItemView extends LinearLayout{
#ViewById(R.id.txt_miestnost_row_nazov)
TextView nazov;
public MiestnostItemView(Context context) {
super(context);
}
public void bind(Miestnost miestnost){
nazov.setText(miestnost.getNazov());
}
}
and finally my adapter class:
public class ListMiestnostiAdapter extends RecyclerViewAdapterBase<Miestnost, MiestnostItemView> {
#RootContext
Context context;
#Override
protected MiestnostItemView onCreateItemView(ViewGroup parent, int viewType) {
return MiestnostItemView_.build(context);
}
#Override
public void onBindViewHolder(ViewWrapper<MiestnostItemView> holder, int position) {
MiestnostItemView view = holder.getView();
Miestnost miestnost = items.get(position);
view.bind(miestnost);
}
}
Under my Activity I initialize my adapter in #AfterViews section:
#ViewById
RecyclerView miestnostiList;
ListMiestnostiAdapter adapter;
#AfterViews
void bindAdapter() {
GetData();//fetch data
adapter = new ListMiestnostiAdapter();
adapter.addData(miestnosti);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
miestnostiList.setLayoutManager(layoutManager);
miestnostiList.setItemAnimator(new DefaultItemAnimator());
miestnostiList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
miestnostiList.setAdapter(adapter);
}
Here is my #ItemClick method:
#ItemClick
void miestnostiListItemClicked(Miestnost miestnost){
Log.i("", "iteClicked: ");
}
What am I missing here?
I think #ItemClick is not available with RecyclerView. You can proceed in two ways:
1- Changing your RecyclerViewAdapterBase adding onClick support, then overriding that method into child adapter like so:
public abstract class RecyclerViewAdapterBase<T, V extends View & ViewWrapper.Binder<T>> extends RecyclerView.Adapter<ViewWrapper<T, V>> {
protected List<T> mItems = new ArrayList<T>();
#Override
public final ViewWrapper<T, V> onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewWrapper<T, V>(onCreateItemView(parent, viewType));
}
protected abstract V onCreateItemView(ViewGroup parent, int viewType);
#Override
public final void onBindViewHolder(ViewWrapper<T, V> viewHolder, int position) {
V view = viewHolder.getView();
T data = mItems.get(position);
/*************************************************************/
* AT THIS POINT IT TRY TO SET A CLICK LISTENER */
/*************************************************************/
setOnClickListener(view, data);
view.bind(data);
}
#Override
public int getItemCount() {
return null == mItems ? 0 : mItems.size();
}
public void add(T item) {
mItems.add(item);
notifyDataSetChanged();
}
public void addAll(Collection<T> collection) {
mItems.addAll(collection);
notifyDataSetChanged();
}
public void clear() {
mItems.clear();
}
/**
* override this into child adapter to manage click event
*/
public void setOnClickListener(View v, T item) {}
}
This is the child adapter
#EBean
public class ChildAdapter extends RecyclerViewAdapterBase<Child, ChildView> {
#RootContext
Context ctx;
#Bean(ChildFinder.class)
IChildFinder finder;
#Bean
OttoBus mOttoBus;
/**
* get data by finder
*/
public void initAdapter() {
mItems = finder.findAll();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
protected ChildView onCreateItemView(ViewGroup parent, int viewType) {
return ChildView_.build(ctx);
}
#Override
public void setOnClickListener(View v, final Child item) {
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mOttoBus.post(new RequestChildEvent(item));
}
});
}
}
2- Otherwise if you are using custom views you can follow an event driven approach binding you click directly on the view (for example on the root view of your custom view) and then sending an event to the component that must react to the click
I am trying to use a RecyclerView with a vertical LinearLayoutManager in order to display a list items. This list can contain several different item types (differents layouts), and it can be reordered by the user using drag and drop.
For the item types, as documented in Android documentation, I have overridden the getItemType method in order to handle different types of views in the recycler and handle it in the onCreateViewHolder and onBindViewHolder. This works like a charm.
For the drag and drop reorder, I have used a ItemTouchHelper.Callback (inspired by this sample project). This also works well.
The problem happen when I try to use different items types AND the drag and drop behaviour. As long as the drag occurs between items of the same type, this works well, but when i'm draggin a view of type A over a view of type B, the drag stop and the view returned to it's original position.
Here is my code:
MyFragment.java
public class MyFragment extends Fragment implements MyAdapter.Listener {
private MyViewModel mViewModel;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private ItemTouchHelper mItemTouchHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment, container, false);
mRecyclerView = root.findViewById(R.id.recyclerView);
mAdapter = new MyAdapter(this);
mRecyclerView.setAdapter(mAdapter);
mItemTouchHelper = new ItemTouchHelper(new MyDragHelperCallback(mAdapter));
mItemTouchHelper.attachToRecyclerView(mRecyclerView);
mViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
mViewModel.addObserver(this, new Observer<List<Item>>() {
#Override
public void onChanged(#Nullable List<Item> items) {
mAdapter.updateList(items);
}
});
mAdapter.updateList(mViewModel.getList());
return root;
}
#Override
public void onStartDragRequest(#NonNull RecyclerView.ViewHolder viewHolder) {
mItemTouchHelper.startDrag(viewHolder);
}
}
MyDragHelperCallback.java
public class MyDragHelperCallback extends ItemTouchHelper.Callback {
private static final int DRAG_MOVEMENT_FLAGS = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
#NonNull
private MyDragListener mListener;
public MyDragHelperCallback(#NonNull MyDragListener listener) {
mListener = listener;
}
#Override
public boolean isLongPressDragEnabled() {
return true;
}
#Override
public boolean isItemViewSwipeEnabled() {
return false;
}
#Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (!(recyclerView.getLayoutManager() instanceof LinearLayoutManager)) {
throw new IllegalArgumentException("Should only be used with a LinearLayoutManager");
}
return makeMovementFlags(DRAG_MOVEMENT_FLAGS, 0);
}
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mListener.onItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Nothing to do
}
}
MyDragListener.java
public interface MyDragListener {
void onItemMoved(int from, int to);
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyHolder> implements MyDragListener {
#Nullable
private List<Item> mList;
public void updateList(#Nullable List<Item> list) {
mList = list;
notifyDataSetChanged();
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new MyHolderBis(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bis, parent, false));
}
return new MyHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
#Override
public void onBindViewHolder(final MyHolder holder, int position) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
mListener.onStartDragRequest(holder);
return true;
}
});
// bind views to data
}
#Override
public int getItemCount() {
return mList != null ? mList.size() : 0;
}
#Override
public int getItemViewType(int position) {
return mList.get(position).getType();
}
#Override
public void onItemMoved(int from, int to) {
notifyItemMoved(from, to);
}
}
Is there something I missing ?
Or is this simply to possible to achieve ?
Thanks for all your answers !