Ошибка Card List With Drag-and-Drop
В Material Components есть такое меню как - Card List With Drag-and-Drop, при попытке интегрировать в моё приложение возникла ошибка, помогите разобраться и исправить ёё.
Fragmrnt java
public class MainFragment extends Fragment {
private static final int CARD_COUNT = 30;
public static MainFragment newInstance() {
return new MainFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState)
{
final RecyclerView recyclerView = view.findViewById(R.id.cat_card_list_recycler_view);
final CardAdapter cardAdapter = new CardAdapter(generateCardNumbers());
ItemTouchHelper itemTouchHelper =
new ItemTouchHelper(new CardItemTouchHelperCallback(cardAdapter));
// Provide an ItemTouchHelper to the Adapter; can't use constructor due to circular dependency.
cardAdapter.setItemTouchHelper(itemTouchHelper);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(cardAdapter);
recyclerView
.setAccessibilityDelegateCompat(new RecyclerViewAccessibilityDelegate(recyclerView) {
@NonNull
@Override
public AccessibilityDelegateCompat getItemDelegate() {
return new ItemDelegate(this) {
@Override
public void onInitializeAccessibilityNodeInfo(View host,
AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
int position = recyclerView.getChildLayoutPosition(host);
if (position != 0) {
info.addAction(new AccessibilityNodeInfoCompat.AccessibilityActionCompat(R.id.move_card_up_action,
host.getResources().getString(R.string.cat_card_action_move_up)));
}
if (position != (CARD_COUNT - 1)) {
info.addAction(new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.move_card_down_action,
host.getResources().getString(R.string.cat_card_action_move_down)));
}
}
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
int fromPosition = recyclerView.getChildLayoutPosition(host);
if (action == R.id.move_card_down_action) {
swapCards(fromPosition, fromPosition + 1, cardAdapter);
return true;
} else if (action == R.id.move_card_up_action) {
swapCards(fromPosition, fromPosition - 1, cardAdapter);
return true;
}
return super.performAccessibilityAction(host, action, args);
}
};
}
});
itemTouchHelper.attachToRecyclerView(recyclerView);
}
private static int[] generateCardNumbers() {
int[] cardNumbers = new int[CARD_COUNT];
for (int i = 0; i < CARD_COUNT; i++) {
cardNumbers[i] = i + 1;
}
return cardNumbers;
}
private static class CardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int[] cardNumbers;
private ItemTouchHelper itemTouchHelper;
private CardAdapter(int[] cardNumbers) {
this.cardNumbers = cardNumbers;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.cat_card_list_item, parent, /* attachToRoot= */ false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
((CardViewHolder) viewHolder).bind(cardNumbers[position], itemTouchHelper);
}
@Override
public int getItemCount() {
return cardNumbers.length;
}
private void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
this.itemTouchHelper = itemTouchHelper;
}
private static class CardViewHolder extends RecyclerView.ViewHolder {
private final TextView titleView;
private final View dragHandleView;
private CardViewHolder(View itemView) {
super(itemView);
titleView = itemView.findViewById(R.id.cat_card_list_item_title);
dragHandleView = itemView.findViewById(R.id.cat_card_list_item_drag_handle);
}
@SuppressLint("ClickableViewAccessibility")
private void bind(int cardNumber, final ItemTouchHelper itemTouchHelper) {
titleView.setText(String.format(Locale.getDefault(), "Card #%02d", cardNumber));
dragHandleView.setOnTouchListener(
(v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
itemTouchHelper.startDrag(CardViewHolder.this);
return true;
}
return false;
});
}
}
}
private static class CardItemTouchHelperCallback extends ItemTouchHelper.Callback {
private static final int DRAG_FLAGS = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
private static final int SWIPE_FLAGS = 0;
private final CardAdapter cardAdapter;
@Nullable
private MaterialCardView dragCardView;
private CardItemTouchHelperCallback(CardAdapter cardAdapter) {
this.cardAdapter = cardAdapter;
}
@Override
public int getMovementFlags(
@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(DRAG_FLAGS, SWIPE_FLAGS);
}
@Override
public boolean onMove(
@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder,
@NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
swapCards(fromPosition, toPosition, cardAdapter);
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG && viewHolder != null) {
dragCardView = (MaterialCardView) viewHolder.itemView;
dragCardView.setDragged(true);
} else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE && dragCardView != null) {
dragCardView.setDragged(false);
dragCardView = null;
}
}
}
private static void swapCards(int fromPosition, int toPosition, CardAdapter cardAdapter) {
int fromNumber = cardAdapter.cardNumbers[fromPosition];
cardAdapter.cardNumbers[fromPosition] = cardAdapter.cardNumbers[toPosition];
cardAdapter.cardNumbers[toPosition] = fromNumber;
cardAdapter.notifyItemMoved(fromPosition, toPosition);
}
}
Fragment xml
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cat_card_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
cat_card_list_item xml
<com.google.android.material.card.MaterialCardView
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:id="@+id/cat_card_list_item_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:minHeight="118dp">
<TextView
android:id="@+id/cat_card_list_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:textAppearance="?attr/textAppearanceHeadline6"/>
<ImageView
android:id="@+id/cat_card_list_item_drag_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:padding="16dp"
android:src="@drawable/ic_launcher_foreground"
tools:targetApi="jelly_bean"/>
</com.google.android.material.card.MaterialCardView>
Часть ошибки
2020-08-04 22:49:44.149 21537-21537/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 21537
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.myapplication.ui.main.MainFragment$CardAdapter.onCreateViewHolder(MainFragment.java:125)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:17787)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17787)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17787)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:17787)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)