点击内部按钮

时间:2017-03-14 10:21:23

标签: android onclick onclicklistener

我的recylerView有自定义布局,由按钮组成。当我点击按钮,onClick fire on the button和recylerView.Is有任何方法可以在点击按钮时停止recyler点击。我不发布代码,因为它是非常大。

自定义布局

     <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:id="@+id/custom_results_adapter_layout2"
        android:visibility="visible"

        >


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:descendantFocusability="blocksDescendants"
            android:layout_height="wrap_content"
            >


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="New"
                    android:textSize="15sp"
                    android:textColor="#00f"
                    android:id="@+id/custom_result_new_old2"
                    android:fontFamily="sans-serif-thin"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2d"
                    android:id="@+id/custom_result_time2"
                    android:textSize="12sp"
                    android:fontFamily="sans-serif-thin"
                    android:textColor="#000"
                    android:layout_gravity="center_vertical"/>

            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/custom_results_result2"
                android:textColor="#000"

                android:layout_marginTop="5dp"
                android:text="Mid Term Result"
                android:gravity="center"
                android:textSize="25sp"
                android:layout_marginBottom="10dp"
                android:fontFamily="sans-serif-light"/>



            <Button
                android:layout_marginBottom="10dp"
                android:id="@+id/results_publish"
                android:layout_width="120dp"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_gravity="center_horizontal"
                android:layout_height="30dp"
                android:textAllCaps="false"

      android:background="@drawable/button_signup_people_boundary"
                android:fontFamily="sans-serif-light"
                android:text="Publish"
                android:onClick="publickClick"
                android:textColor="#000"
                android:textSize="15dp" />





        </LinearLayout>


    </LinearLayout>

主要布局

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  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="match_parent"
    tools:context="activities.Result.Results">

    <LinearLayout
        android:id="@+id/activity_results"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar 
   xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbarTitle"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="#fff"
            android:elevation="2dp"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:background="?
        attr/selectableItemBackgroundBorderless"
                    android:onClick="onBack"
                    android:padding="12dp"
                    app:srcCompat="@drawable/ic_back_arrow" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:layout_marginLeft="20dp"
                    android:fontFamily="sans-serif"
                    android:gravity="center"
                    android:text="Results"
                    android:textColor="#212d19"
                    android:textSize="20sp" />
            </LinearLayout>

        </android.support.v7.widget.Toolbar>


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/results_recyler_view"
                android:layout_width="match_parent"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_height="match_parent">

            </android.support.v7.widget.RecyclerView>
        </android.support.v4.widget.SwipeRefreshLayout>


    </LinearLayout>

    <android.support.design.widget.FloatingActionButton 
 xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/myFabResultsUpload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:onClick="onResultsFab"
        android:scaleType="center"
        android:visibility="gone"
        app:backgroundTint="@color/colorAccent"
        app:rippleColor="#44ffffff"
        app:srcCompat="@drawable/ic_plus_white" />
          <android.support.design.widget.FloatingActionButton 
           xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/myFabResultsAddEditor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:onClick="onAddEditorFab"
        android:scaleType="center"
        android:visibility="gone"
        app:backgroundTint="@color/colorAccent"
        app:rippleColor="#44ffffff"
        app:srcCompat="@drawable/ic_addeditoricon" />


    <ProgressBar
        android:id="@+id/results_progress_bar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="visible" />

    <TextView
        android:id="@+id/no_result_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:fontFamily="sans-serif-thin"
        android:text="No Result Found"
        android:textColor="#99000000"
        android:textSize="20sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/retry"
        android:layout_width="90dp"
        android:layout_height="35dp"
        android:paddingBottom="2dp"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:background="@drawable/edit_text_boundary"
        android:fontFamily="sans-serif-light"
        android:text="Retry"
        android:alpha="0.25"
        android:textColor="#000000"
        android:textSize="20sp"
        android:visibility="gone" />

</RelativeLayout>

适配器

           package Adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.espero.R;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import Model.ResultsModel;

import static Classes.RoundedCorner.getRoundedCornerBitmap;

/**
 * Created by parvesh_dhull on 27/1/17.
 */

public class Results_recyler_adapter extends RecyclerView.Adapter<Results_recyler_adapter.MyViewHolder> {


    private List<ResultsModel> resultsModels = new ArrayList<>();
    private Context context;
    private String TAG = "EVENT";
    private List<String> readedResults = new ArrayList<>();
    private Long schoolTime;
    private Boolean adminFlag = false;
    private FirebaseDatabase database;
    private DatabaseReference myRef;
    private String schoolID = "";

    public Results_recyler_adapter(List<ResultsModel> resultsModels, List<String> readedResults, Context context, Long schoolTime, Boolean adminFlag, FirebaseDatabase database, String schoolID) {
        this.resultsModels = resultsModels;
        this.readedResults = readedResults;
        this.context = context;
        this.schoolTime = schoolTime;
        this.adminFlag = adminFlag;
        this.database = database;
        this.schoolID = schoolID;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.custom_layout_result_adapter, parent, false);

        return new MyViewHolder(itemView);
    }

    public void refresh(List<ResultsModel> resultsModels, List<String> readedResults, Boolean adminFlag) {
        this.resultsModels = resultsModels;
        this.readedResults = readedResults;
        this.adminFlag = adminFlag;
//
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {




    }

    @Override
    public int getItemCount() {
        Log.d("Result",Integer.toString(resultsModels.size()));
        return resultsModels.size();

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView result1, result2, date1, date2, newOld1, newOld2;
        private LinearLayout layout1, layout2;
        private Button publish;


        public MyViewHolder(View view) {
            super(view);
            newOld1 = (TextView) view.findViewById(R.id.custom_result_new_old);
            newOld2 = (TextView) view.findViewById(R.id.custom_result_new_old2);
            result1 = (TextView) view.findViewById(R.id.custom_results_result);
            result2 = (TextView) view.findViewById(R.id.custom_results_result2);
            date2 = (TextView) view.findViewById(R.id.custom_result_time2);
            date1 = (TextView) view.findViewById(R.id.custom_result_time);
            layout1 = (LinearLayout) view.findViewById(R.id.custom_results_adapter_layout1);
            layout2 = (LinearLayout) view.findViewById(R.id.custom_results_adapter_layout2);
            publish = (Button) view.findViewById(R.id.results_publish);


            publish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("Result","clickInAdapter");
                }
            });




        }
    }


}

RecylerTouchListner Class

    package Classes;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by parvesh_dhull on 15/2/17.
 */

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{

    private GestureDetector gestureDetector;
    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        public void onClick(View view, int position);

        public void onLongClick(View view, int position);
    }

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final OnItemClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
    }

点击

的主要活动代码
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.OnItemClickListener() {
        @Override
        public void onClick(View view, int position) {


            Log.d("Result","recylerClicked");
      }

        @Override
        public void onLongClick(View view, int position) {

            if(schoolID.equals(UID)){
            deleteResult(resultsModels.size(),position);    }
        }

    }));

0 个答案:

没有答案