翻译完recyclerview后,它将停止监听触摸事件

时间:2018-11-08 10:15:32

标签: android android-recyclerview android-animation

可能我在这里丢失了一些东西,但我正在将recyclerview设置为在其容器外部进行动画处理,当它处于最终位置时,不会触发click事件,滚动也不会起作用...

我正在使用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>

在这种情况下会发生什么?

更新

我在进行调试,我注意到问题是因为结束位置在列表容器之外。如果我在容器内翻译,它可以正常工作。所以我的新问题是:如何在仍然触发点击事件的同时转换视图到容器之外?

1 个答案:

答案 0 :(得分:0)

基本上,我只是放弃了这种方法。我更改了布局,使其列表在其容器内有空间显示。