在androidstudio中处理嵌套的触摸事件?

zbq4xfa0  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(134)

我有一个recyclerview,我使用firebasecollector用imageview填充一个网格。我想能够在循环视图中左右滑动,在两类图像之间切换。如果我在categoryone中,向左滑动,我将切换到category2,并调用firebaserecycler用category2图像填充recyclerview。当我向右滑动时,反之亦然。所以本质上,我在处理刷卡的ontouchlistener中调用firebaserecycler。但是,我也希望能够在不同的活动中单击图像以打开特定的图像。为此,我在firebasecollercler中创建了另一个侦听器。基本上有嵌套的侦听器。我在一个片段中实现这个,所以我不能使用dispatchtouchevent。
explorefragment.java文件

package Fragments;

import ...

public class ExploreFragment extends Fragment {

// declare layout ids
    FrameLayout flExploreFragment;
    TextView tvExploreSearch;
    ImageView ivExploreFRMedia, ivExploreFRServices;
    FrameLayout flExploreFRMediaBar, flExploreFRServicesBar;
    ScrollView svExploreFRPosts;
    RecyclerView rvExploreFRPosts;

// local variables
    boolean isMedia;
    float x1, x2; // for swiping functionality

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_explore, container, false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // initialize layout ids
        flExploreFragment = getActivity().findViewById(R.id.flExplore_Fragment);
        tvExploreSearch = v.findViewById(R.id.tvExploreFR_Search);
        ivExploreFRMedia = v.findViewById(R.id.ivExploreFR_Media);
        ivExploreFRServices = v.findViewById(R.id.ivExploreFR_Services);
        flExploreFRMediaBar = v.findViewById(R.id.flExploreFR_MediaBar);
        flExploreFRServicesBar = v.findViewById(R.id.flExploreFR_ServicesBar);
        rvExploreFRPosts = v.findViewById(R.id.rvExploreFR_Posts);
        svExploreFRPosts = v.findViewById(R.id.svExploreFR_Posts);

    // initialize local variables
        isMedia = true;

    // set recycler adapter layout
        rvExploreFRPosts.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        explorePostsAdapter();

    // set listeners
        tvExploreSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Fragment frSearch = new SearchExploreFragment();
                getActivity().getSupportFragmentManager().beginTransaction().replace(flExploreFragment.getId(), frSearch).addToBackStack(null).commit();
            }
        });

        ivExploreFRMedia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isMedia) {
                    isMedia = true;
                    explorePostsAdapter();
                }
            }
        });

        ivExploreFRServices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMedia) {
                    isMedia = false;
                    explorePostsAdapter();
                }
            }
        });

        rvExploreFRPosts.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x1 = event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        x2 = event.getX();
                        // swipe left, if user swiped left & swipe was done in fragment
                        if(x1 > (x2 + 170)) {
                            if (isMedia) {
                                isMedia = false;
                                explorePostsAdapter();
                            }
                        }
                        // swipe right, ...
                        else if((x1 + 170) < x2) {
                            if (!isMedia) {
                                isMedia = true;
                                explorePostsAdapter();
                            }
                        }
                }
                return true;
            }
        });

        return v;
    }

// move bar and change post type based on typeAdapter
    private void explorePostsAdapter() {

        // set which post tab to underline and set the query
        Query postQuery;

        if (isMedia){
            isMedia = true;
            flExploreFRMediaBar.setPadding(0,0,0,6);
            flExploreFRServicesBar.setPadding(0,0,0,0);
            postQuery = artistlyDB.orderedByQuery("Media", "user");
        }
        else {
            isMedia = false;
            flExploreFRServicesBar.setPadding(0,0,0,6);
            flExploreFRMediaBar.setPadding(0,0,0,0);
            postQuery = artistlyDB.orderedByQuery("Services", "user");;
        }

        // implement FirebaseRecycler on recycler adapter
        FirebaseRecyclerOptions<postsAdapter> options = new FirebaseRecyclerOptions.Builder<postsAdapter>().setQuery(postQuery, postsAdapter.class).build();
        final FirebaseRecyclerAdapter<postsAdapter, ExploreFragment.postsAdapterViewHolder> adapter = new FirebaseRecyclerAdapter<postsAdapter, ExploreFragment.postsAdapterViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull ExploreFragment.postsAdapterViewHolder holder, final int position, @NonNull final postsAdapter model) {
                holder.setInfo(getActivity(), model.getPhoto());

                holder.mView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()) {
                            case MotionEvent.ACTION_UP:
                                float difference = x2 - x1;
                                if (difference > -50 && difference < 50) {
                                    final String postID = getRef(position).getKey();
                                    Intent intent = new Intent(getActivity(), PostActivity.class);
                                    intent.putExtra("postID", postID);
                                    intent.putExtra("postType", isMedia);
                                    startActivity(intent);
                                }
                        }
                        return true;
                    }
                });
            }
            @NonNull
            @Override
            public ExploreFragment.postsAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_posts, parent, false);
                return new ExploreFragment.postsAdapterViewHolder(v);
            }
        };
        adapter.startListening();
        rvExploreFRPosts.setAdapter(adapter);
    }

// view Holder Class need for recycler adapter
    public static class postsAdapterViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public postsAdapterViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public void setInfo(Context ctx, String photo){
            final Context ctxF = ctx;
            final ImageView ivPostsAdImage = mView.findViewById(R.id.ivPostsAd_Image);
            Glide.with(ctxF).load(photo).into(ivPostsAdImage);
        }
    }
}

片段\u explore.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="Fragments.ExploreFragment">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_height="0dp" android:id="@+id/hlExploreFR_ActionBar"
        app:layout_constraintHeight_percent=".07" app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent" app:srcCompat="@drawable/ic_search"
            android:id="@+id/ivExploreFR_SearchImage"
            />

        <TextView
            android:id="@+id/tvExploreFR_Search"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center|left"
            android:hint="Search"
            android:textSize="19sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hlExploreFR_PostTypes"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal"
        app:layout_constraintHeight_percent=".05"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/hlExploreFR_ActionBar">

        <ImageView
            android:id="@+id/ivExploreFR_Media"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:src="@drawable/ic_media" />

        <ImageView
            android:id="@+id/ivExploreFR_Services"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="1dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:src="@drawable/ic_services" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hlExploreFR_PostsBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@id/hlExploreFR_PostTypes">

        <FrameLayout
            android:id="@+id/flExploreFR_MediaBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:paddingBottom="3dp">

        </FrameLayout>

        <FrameLayout
            android:id="@+id/flExploreFR_ServicesBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black">

        </FrameLayout>
    </LinearLayout>

    <ScrollView
        android:id="@+id/svExploreFR_Posts"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/hlExploreFR_PostsBar">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvExploreFR_Posts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

问题
所以当片段第一次被加载时,我能够成功地点击一个图像并在一个新的活动中打开它。但是,只要我刷一次,然后重新加载回收视图,我就不能再点击图像了。刷卡从一开始就非常有效。只有在第一次调用firebasecollercler时,单击图像才起作用。当它没有在ontouchlister中调用以进行刷卡时。只有当它从刷卡侦听器内部调用时,它才不起作用。有人知道为什么会这样吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题