Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid Swipe event in listview inside tab widget
    primarykey
    data
    text
    <p>I have created an Activity that shows a listview and on swipe action another list is shown using ViewFlipper. The code is attached below:</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.ViewFlipper; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gestureDetector; View.OnTouchListener gestureListener; private Animation slideLeftIn; private Animation slideLeftOut; private Animation slideRightIn; private Animation slideRightOut; private ViewFlipper viewFlipper; private ListView lv; private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain", "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad", "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" }; private String[] country = { "India", "Bhutan", "Kuwait", "USA", }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); lv = (ListView) findViewById(R.id.List01); ListView lv2 = (ListView) findViewById(R.id.List02); viewFlipper = (ViewFlipper) findViewById(R.id.flipper); slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in); slideLeftOut = AnimationUtils .loadAnimation(this, R.anim.slide_left_out); slideRightIn = AnimationUtils .loadAnimation(this, R.anim.slide_right_in); slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out); gestureDetector = new GestureDetector(new MyGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }; lv.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, city)); lv2.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, country)); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View view, int position, long id) { // user clicked a list item, make it "selected" Toast.makeText(getBaseContext(), "Item Clicked", Toast.LENGTH_SHORT).show(); // selectedAdapter.setSelectedPosition(position); } }); lv2.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View view, int position, long id) { // user clicked a list item, make it "selected" Toast.makeText(getBaseContext(), "Item List2 Clicked", Toast.LENGTH_SHORT).show(); // selectedAdapter.setSelectedPosition(position); } }); } class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MAX_OFF_PATH) return false; // right to left swipe if (e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideLeftIn); viewFlipper.setOutAnimation(slideLeftOut); viewFlipper.showNext(); } else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideRightIn); viewFlipper.setOutAnimation(slideRightOut); viewFlipper.showPrevious(); } } catch (Exception e) { // nothing } return false; } } @Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) return true; else return false; } } </code></pre> <p>It's working f9...</p> <p>But when I place this within the another tab activity with the following code <strong>The View Flipper is not working when placed as content(intent) in tab of another tabactivity (AppStart)</strong></p> <pre><code>import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.TabHost; public class AppStart extends TabActivity { /** Called when the activity is first created. */ static Drawable myImage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); myImage = res.getDrawable(R.drawable.club); TabHost tabs = getTabHost(); tabs.setup(); TabHost.TabSpec spec = tabs.newTabSpec(null); Intent i = new Intent().setClass(this, MainActivity.class); spec = tabs.newTabSpec(null); spec.setContent(i); spec.setIndicator("Tab"); tabs.addTab(spec); } } </code></pre> <p>The Layout is as follows: main_layout.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/List01"/&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/List02" /&gt; &lt;/LinearLayout&gt; &lt;/ViewFlipper&gt; </code></pre> <p>And main.xml as shown below:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>Please suggest why the swipe gesture is not working when the same activity is placed in tab.</p> <p>Thank You</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload