Android Gesture is not working for child in its parent

363 Views Asked by At

I am developing an application with accessing gesture of android. I am not able to swipe over the layout where some texts are visible,but able to swipe in blank spaces of activity.

my java code

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tomcast.libin.anxiety.R;
import com.tomcast.libin.anxiety.models.Questions;
import com.tomcast.libin.anxiety.utils.OnSwipeTouchListener;
import com.tomcast.libin.anxiety.utils.SlideAnimationUtil;

import java.util.ArrayList;

public class DFragment extends Fragment {
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private ArrayList<Questions> questionlist = new ArrayList<Questions>();
private static int START_POSITION=0;
private static int FINAL_POSITION=6;
private static int CURRENT_POSTION =0;
ConstraintLayout llQuestion;
TextView question;
ImageView iconA;
ImageView iconB;
ImageView iconC ;
ImageView iconD;
float x1,y1,x2,y2,t1,t2;
`enter code here`int  CLICK_DURATION = 10;

private OnFragmentInteractionListener mListener;

public DFragment() {
    // Required empty public constructor
}


// TODO: Rename and change types and number of parameters
public static Fragment newInstance(String param1, String param2) {
    Fragment fragment = new Fragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.sliding_question_layout, container, false);
    init( view);
    return view ;
}
  void init(final View view){
  llQuestion=(ConstraintLayout)  view.findViewById(R.id.constraintLayout);
     iconA=  view.findViewById(R.id.ic_option_a);
     iconB=   view.findViewById(R.id.ic_option_b);
     iconC=  view.findViewById(R.id.ic_option_c);
     iconD=   view.findViewById(R.id.ic_option_d);

    Questions q0 = new Questions();
    q0.setQuestion("Feeling Nervous ");
    q0.setSelected_answer("");
    questionlist.add(q0);

    Questions q1= new Questions();
    q1.setQuestion("Anixious,or on edge");
    q1.setSelected_answer("");
    questionlist.add(q1);


    Questions q2= new Questions();
    q2.setQuestion("Not being able to stop or control worrying");
    q2.setSelected_answer("");
    questionlist.add(q2);

    Questions q3 = new Questions();
    q3 .setQuestion("Worry to much about differnt things ?");
    q3 .setSelected_answer("");
    questionlist.add(q3 );

    Questions q4= new Questions();
    q4.setQuestion("Troble relaxing");
    q4.setSelected_answer("");
    questionlist.add(q4);

    Questions q5= new Questions();
    q5.setQuestion("Being so restless that it's hard to set still");
    q5.setSelected_answer("");
    questionlist.add(q5);

    Questions q6= new Questions();
    q6.setQuestion("Becoming easily annyed or irritable");
    q6.setSelected_answer("");
    questionlist.add(q6);
    final TextView t= view.findViewById(R.id.question);
    t.setText(questionlist.get(CURRENT_POSTION).getQuestion());

    RelativeLayout rOptionA=(RelativeLayout) view.findViewById(R.id.rel_option_a);
    RelativeLayout rOptionB=(RelativeLayout) view.findViewById(R.id.rel_option_b);
    RelativeLayout rOptionC=(RelativeLayout) view.findViewById(R.id.rel_option_c);
    RelativeLayout rOptionD=(RelativeLayout) view.findViewById(R.id.rel_option_d);




    rOptionA.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Interpret MotionEvent data
            // Handle touch here

            questionlist.get(CURRENT_POSTION).setSelected_answer("A");
            iconA.setImageResource(R.drawable.ic_success);
            iconB.setImageResource(R.drawable.ic_dot);
            iconC.setImageResource(R.drawable.ic_dot);
            iconD.setImageResource(R.drawable.ic_dot);

            return false;

        }
    });
    rOptionB.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Interpret MotionEvent data
            // Handle touch heresuper.onTouchEvent(event);
            questionlist.get(CURRENT_POSTION).setSelected_answer("B");
            iconA.setImageResource(R.drawable.ic_dot);
            iconB.setImageResource(R.drawable.ic_success);
            iconC.setImageResource(R.drawable.ic_dot);
            iconD.setImageResource(R.drawable.ic_dot);


            return true;
        }
    });
    rOptionC.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View v, MotionEvent event) {
            // Interpret MotionEvent data
            // Handle touch here
           questionlist.get(CURRENT_POSTION).setSelected_answer("C");
           iconA.setImageResource(R.drawable.ic_dot);
           iconB.setImageResource(R.drawable.ic_dot);
           iconC.setImageResource(R.drawable.ic_success);
           iconD.setImageResource(R.drawable.ic_dot);

            return true;
       }
   });

   rOptionD.setOnTouchListener(new View.OnTouchListener() {
       @Override
        public boolean onTouch(View v, MotionEvent event) {

           questionlist.get(CURRENT_POSTION).setSelected_answer("D");
           iconA.setImageResource(R.drawable.ic_dot);
           iconB.setImageResource(R.drawable.ic_dot);
           iconC.setImageResource(R.drawable.ic_dot);
           iconD.setImageResource(R.drawable.ic_success);


           return true;
        }
    });

   ConstraintLayout cl= view.findViewById(R.id.constraintLayout);
    cl.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
        @Override
        public void onSwipeDown() {

        }

        @Override
        public void onSwipeLeft() {
            if(CURRENT_POSTION < FINAL_POSITION) {
                CURRENT_POSTION = CURRENT_POSTION + 1;
                iconA.setImageResource(R.drawable.ic_dot);
                iconB.setImageResource(R.drawable.ic_dot);
                iconC.setImageResource(R.drawable.ic_dot);
                iconD.setImageResource(R.drawable.ic_dot);

                Log.i("ggg", String.valueOf(CURRENT_POSTION));
                question = view.findViewById(R.id.question);

                question.setText(questionlist.get(CURRENT_POSTION).getQuestion());

                if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("")){
                    iconA.setImageResource(R.drawable.ic_dot);
                    iconB.setImageResource(R.drawable.ic_dot);
                    iconC.setImageResource(R.drawable.ic_dot);
                    iconD.setImageResource(R.drawable.ic_dot);
                }

                else{
                    Log.d("ELSE",( questionlist.get(CURRENT_POSTION).getSelected_answer()).toString());

                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("A")){
                        iconA.setImageResource(R.drawable.ic_success);
                    }
                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("B")){

                        iconB.setImageResource(R.drawable.ic_success);

                    } if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("C")){

                        iconC.setImageResource(R.drawable.ic_success);

                    }
                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("D")){

                        iconD.setImageResource(R.drawable.ic_success);
                    }
                }
                SlideAnimationUtil.slideInFromRight(getContext(),llQuestion);
            } else{

            }

        }

        @Override
        public void onSwipeUp() {

        }

        @Override
        public void onSwipeRight() {
            if(CURRENT_POSTION >START_POSITION) {
                CURRENT_POSTION = CURRENT_POSTION -1;
                Log.i("ggg", String.valueOf(CURRENT_POSTION));
                iconA.setImageResource(R.drawable.ic_dot);
                iconB.setImageResource(R.drawable.ic_dot);
                iconC.setImageResource(R.drawable.ic_dot);
                iconD.setImageResource(R.drawable.ic_dot);
                TextView t = view.findViewById(R.id.question);
                t.setText(questionlist.get(CURRENT_POSTION).getQuestion());
                if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("")){
                    iconA.setImageResource(R.drawable.ic_dot);
                    iconB.setImageResource(R.drawable.ic_dot);
                    iconC.setImageResource(R.drawable.ic_dot);
                    iconD.setImageResource(R.drawable.ic_dot);
                }
                else{
                    Log.d("ELSE",( questionlist.get(CURRENT_POSTION).getSelected_answer()).toString());

                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("A")){

                        iconA.setImageResource(R.drawable.ic_success);

                    }
                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("B")){

                        iconB.setImageResource(R.drawable.ic_success);

                    } if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("C")){

                        iconC.setImageResource(R.drawable.ic_success);
                    }
                    if((questionlist.get(CURRENT_POSTION).getSelected_answer()).equals("D")){

                        iconD.setImageResource(R.drawable.ic_success);
                    }
                }

                SlideAnimationUtil.slideInFromLeft(getContext(),llQuestion);
            }
        }
    });

}

 public void onButtonPressed(Uri uri) {
    if (mListener != null) {
       // mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
     }
    }

@Override
public void onDetach() {
    super.onDetach();
    CURRENT_POSTION=0;
    mListener = null;
  }
  } 

My xml code

<?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="match_parent">
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/three"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

<android.support.constraint.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginRight="0dp"
    android:background="#d4ffffff"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="0dp"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">


        <android.support.constraint.Guideline
            android:id="@+id/guideline4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.25"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="152dp" />

        <TextView
            android:id="@+id/question"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:text="TextView"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textSize="35sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline4"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.25" />

        <RelativeLayout
            android:id="@+id/rel_option_a"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/edge_rounded_border"
            android:clickable="true"
            android:padding="@dimen/option_relative_padding"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline4">

            <ImageView
                android:id="@+id/ic_option_a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:tag="im_optA"
                app:srcCompat="@drawable/ic_dot" />

            <TextView
                android:id="@+id/option_a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toEndOf="@+id/ic_option_a"
                android:text="@string/option_a"
                android:textAlignment="center"
                android:textColor="#000000"
                android:textSize="17sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_option_b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="14dp"
            android:background="@drawable/edge_rounded_border"
            android:padding="@dimen/option_relative_padding"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rel_option_a">

            <TextView
                android:id="@+id/option_b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toEndOf="@+id/ic_option_b"
                android:text="@string/option_b"
                android:textAlignment="center"
                android:textColor="#000000"
                android:textSize="17sp" />

            <ImageView
                android:id="@+id/ic_option_b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:tag="im_optB"
                app:srcCompat="@drawable/ic_dot" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_option_c"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="14dp"
            android:background="@drawable/edge_rounded_border"
            android:padding="@dimen/option_relative_padding"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rel_option_b">

            <ImageView
                android:id="@+id/ic_option_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:tag="im_optC"
                app:srcCompat="@drawable/ic_dot" />

            <TextView
                android:id="@+id/option_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toEndOf="@+id/ic_option_c"
                android:background="@drawable/edge_rounded"
                android:text="@string/option_c"
                android:textAlignment="center"
                android:textColor="#000000"
                android:textSize="17sp" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_option_d"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/edge_rounded_border"
            android:padding="@dimen/option_relative_padding"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rel_option_c">

            <ImageView
                android:id="@+id/ic_option_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:tag="im_optD"
                app:srcCompat="@drawable/ic_dot" />

            <TextView
                android:id="@+id/option_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toEndOf="@+id/ic_option_d"
                android:background="@drawable/edge_rounded"
                android:text="@string/option_d"
                android:textAlignment="center"
                android:textColor="#000000"
                android:textSize="17sp" />
        </RelativeLayout>

    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

Above is my full code of fragment.I need to swipe without selecting the layer.how can I code.this image show the selection of particular layer,I am not able to swipe over this layer without selection.

0

There are 0 best solutions below