Создаю приложение Anekdot Reader: http://4pda.ru/forum/index.php?showtopic=334581
Дело в том, что анекдоты выводятся в TextView (id=text), а этот TextView находится в ScrollView. Мне необходимо одновременно обрабатывать TouchListener (для слайда вперед - назад) и ScrollView (вверх - вниз), как мне это осуществить?
Вот layout единственного Activity:
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="bottom|center_horizontal|top"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@color/serii"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@color/serii"
android:gravity="right"
android:orientation="horizontal" >
<SeekBar
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_weight="1" >
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:fillViewport="true"
android:scrollbars="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:smoothScrollbar="true"
android:textColor="@color/BLACK"
android:text=""
android:textSize="15dp" />
</ScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/button"
android:src="@drawable/back"
android:text="<" >
</ImageButton>
<TextView
android:id="@+id/kolvo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="0 из 0"
android:background="@drawable/button"
android:textColor="@color/WHITE" />
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/button"
android:src="@drawable/forward"
android:text=">" >
</ImageButton>
</LinearLayout>
</LinearLayout>
Код: Выделить всё
public class Start extends Activity implements SeekBar.OnSeekBarChangeListener,
OnClickListener, OnTouchListener {
TextView text;
ScrollView mScrollView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);
text = (TextView) findViewById(R.id.text);
text.setOnTouchListener(this);
mainLayout.setOnTouchListener(this);
}
float x,y;
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_UP:
if (Math.abs(event.getX() - x) > Math.abs(event.getY() - y))
if (event.getX() < x)
moveToNext();
else
moveToPrevious();
break;}
return true;
}
}