После перевода реселлервью перестает слушать сенсорные события

1

Вероятно, я что-то здесь упускаю, но я анимирую просмотр повторного вызова снаружи его контейнера, и когда он находится в конечном положении, события щелчка не инициируются, ни прокрутка не работает...

Я использую ObjectAnimator для его перевода, поэтому я подумал, что интерактивные области также были переведены. Вот соответствующий код:

Фрагмент

public class SlideListFragment extends Fragment {

private Button slideButton;
private RecyclerView listToSlide;
private DummyListAdapter listAdapter;
private LinearLayout listContainer;

public static SlideListFragment newInstance() {
    return new SlideListFragment();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.slide_list_fragment, container, false);
    slideButton = root.findViewById(R.id.slide_up_btn);
    slideButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            slideUpRoomsList();
        }
    });

    listToSlide = root.findViewById(R.id.list_to_slide);
    listContainer = root.findViewById(R.id.list_container);
    setupList();
    return root;
}

private void setupList() {
    GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), 1);
    layoutManager.setReverseLayout(true);
    listToSlide.setLayoutManager(layoutManager);
    listAdapter = new DummyListAdapter(this.getContext());
    listToSlide.setAdapter(listAdapter);

    setListData(5);
}

private void setListData(int i) {
    ArrayList<DummyModel> items = new ArrayList<>();
    for (int j = 0; j < i; j++) {
        items.add(new DummyModel(j, "TEXT" + j));
    }
    listAdapter.refreshItems(items);
}

private void slideUpRoomsList() {
    float height = listContainer.getHeight();
    ObjectAnimator showAnimation = ObjectAnimator.ofFloat(listContainer, "translationY", -height);
    showAnimation.setDuration(500);
    showAnimation.start();
}
}

Макет фрагмента

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/slidelist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    tools:context=".ui.slidelist.SlideListFragment">

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.8" />

<android.support.constraint.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#FF0000"
    android:clipChildren="false"
    android:clipToPadding="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/guideline">

    <LinearLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_to_slide"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

<Button
    android:id="@+id/slide_up_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="SlideUp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Адаптер

public class DummyListAdapter extends RecyclerView.Adapter<DummyListAdapter.ViewHolder> {

private WeakReference<Context> mContext;

private ArrayList<DummyModel> data;

public DummyListAdapter(Context ctx) {
    mContext = new WeakReference<>(ctx);
}

@NonNull
@Override
public DummyListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.dummy_list_item, parent, false);
    DummyListAdapter.ViewHolder vh = new DummyListAdapter.ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(@NonNull DummyListAdapter.ViewHolder holder, int position) {
    // get element from your dataset at this position
    final DummyModel item = data.get(position);
    // replace the contents of the view with that element
    holder.labelTV.setText(item.getText());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext.get(), "Item Clicked: "+ item.getText(), Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public int getItemCount() {
    return data != null ? data.size() : 0;
}

public void refreshItems(ArrayList<DummyModel> items) {
    this.data = items;
    notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView labelTV;

    public ViewHolder(View itemView) {
        super(itemView);
        this.labelTV = itemView.findViewById(R.id.label_tv);
    }
}
}

Расположение элемента списка

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content">

    <TextView
        android:id="@+id/label_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        android:textSize="20sp"
        android:textColor="#0000FF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Что происходит в этом случае?

ОБНОВИТЬ

Я делаю некоторую отладку и заметил, что проблема в том, что конечная позиция находится вне контейнера списка. Если я переведу внутри контейнера, он работает правильно. Итак, мой новый вопрос: как я могу перевести представление за пределы контейнера, при этом вызывая события нажатия?

Теги:
android-recyclerview
android-animation

1 ответ

0

По сути, я просто отказался от этого подхода. Я изменил свой макет, чтобы в списке было место для отображения внутри его контейнера.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню