mercredi 13 mai 2015

How can I detect and utilize left and right swipe gestures on a RelativeLayout to call functions?

So far I have created a RelativeLayout as below, which holds 6 Text-views (but only 3 are visible at any one time). Upon button press of either of the two circular Image-views those Text-views are made visible or invisible appropriately.

What I would also like to happen, but as yet I don't know how is: for when the use swipes left anywhere on the RelativeLayout holding the textviews it would be as though the first circular Image-view had been pressed and it the user swipes right anywhere on the Image-view, it would be as though the user had selected the second circular Image-view.

I have not used gestures in this fashion on RelativeLayouts before, so help would be much appreciated. Thanks

My code so far:

MainActivity.Java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView firstCircle = (ImageView) findViewById(R.id.firstCircle);
        final ImageView  secondCircle = (ImageView) findViewById(R.id.secondCircle);
        final TextView  textView1 = (TextView) findViewById(R.id.textView1);
        final TextView  textView2 = (TextView) findViewById(R.id.textView2);
        final TextView  textView3 = (TextView) findViewById(R.id.textView3);
        final TextView  textView4 = (TextView) findViewById(R.id.textView4);
        final TextView  textView5 = (TextView) findViewById(R.id.textView5);
        final TextView  textView6 = (TextView) findViewById(R.id.textView6);

        firstCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView1.setVisibility(View.VISIBLE);
                textView2.setVisibility(View.VISIBLE);
                textView3.setVisibility(View.VISIBLE);
                textView4.setVisibility(View.INVISIBLE);
                textView5.setVisibility(View.INVISIBLE);
                textView6.setVisibility(View.INVISIBLE);

            }
        });

        secondCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView1.setVisibility(View.INVISIBLE);
                textView2.setVisibility(View.INVISIBLE);
                textView3.setVisibility(View.INVISIBLE);
                textView4.setVisibility(View.VISIBLE);
                textView5.setVisibility(View.VISIBLE);
                textView6.setVisibility(View.VISIBLE);
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/resultsBox"
        android:layout_width="fill_parent"
        android:layout_height="500dp"
        android:layout_alignParentLeft="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="true"
        android:background="#2afd24">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tables"
            android:visibility="visible"
            android:textSize="40dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tables"
            android:visibility="visible"
            android:layout_marginTop="40dp"
            android:textSize="40dp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tables"
            android:visibility="visible"
            android:layout_marginTop="80dp"
            android:textSize="40dp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chairs"
            android:visibility="invisible"
            android:layout_marginTop="120dp"
            android:textSize="40dp" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chairs"
            android:visibility="invisible"
            android:layout_marginTop="160dp"
            android:textSize="40dp" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chairs"
            android:visibility="invisible"
            android:layout_marginTop="200dp"
            android:textSize="40dp" />

        <ImageView
            android:id="@+id/firstCircle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/circle"
            android:layout_marginTop="310dp"
            android:layout_marginLeft="170dp"/>

        <ImageView
            android:id="@+id/secondCircle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/circle"
            android:layout_marginTop="310dp"
            android:layout_marginLeft="200dp"/>

    </RelativeLayout>

</RelativeLayout>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://ift.tt/nIICcg"
    android:shape="oval">

    <solid
        android:color="#fff"/>

    <size
        android:width="18dp"
        android:height="18dp"/>
</shape>

What app looks like so far:

enter image description here

Aucun commentaire:

Enregistrer un commentaire