gpt4 book ai didi

java - Views Navigation 使用 Swipe Gesture android

转载 作者:行者123 更新时间:2023-11-29 20:16:33 28 4
gpt4 key购买 nike

基于 json ArrayList 大小,我正在创建 TextView

通过使用 Display 类,使每个 TextView 的高度和宽度覆盖整个屏幕。

座右铭

Only 1 TextView should be visible on the screen. By swiping it should move to next view which will again occupy the entire screen.

Swipe down and Swipe up will move the screens i.e., views... swipe left and swipe right should do some other tasks,such as changing activity

通过使用 GestureDetector.SimpleOnGestureListener 启用滑动

到目前为止,我已经尝试使用 ViewFlipperTextView 数组来启用 TextView 之间的切换。但是失败了:(

代码 fragment :

        for(int i=0;i<name.size();i++)
{
text = new TextView(MainActivity.this);
text.setText(name.get(i));
text.setId(i);
text.setTextColor(Color.parseColor("#000000"));
text.setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
text.setGravity(Gravity.CENTER);
text.setTextSize(40);
text.setClickable(true);
vf.addView(text);

/*
//I've tried the following code while using TextView array
myTextViews[i] = text;
myTextViews[i].setId(i);
myTextViews[i].setTextColor(Color.BLACK);
myTextViews[i].setText(name.get(i));
myTextViews[i].setGravity(Gravity.CENTER);
myTextViews[i].setTextSize(40);
myTextViews[i].setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
myTextViews[i].onWindowFocusChanged(false);
LL.addView(myTextViews[i]);
*/

View lines = new View(getApplicationContext());
lines.setBackgroundColor(Color.parseColor("#000000"));
lines.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
vf.addView(lines);

final int finalI = i;


text.setOnTouchListener(new MainActivity()
{
@Override
public void onSwipeLeft()
{
if (vf.getDisplayedChild() == 0)
Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
else
vf.showNext();
}

@Override
public void onSwipeRight()
{
if (vf.getDisplayedChild() == 0)
Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
else
vf.showPrevious();
}
});
}

错误:

  1. 使用 ViewFlipper

    E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback

  2. 数组:

    E/InputEventReceiver﹕ Exception dispatching input event. -- java.lang.ArrayIndexOutOfBoundsException

编辑

我找到了这个 Questionios 相关。搜索相同的 android

I'm trying to develop a app similar to SimplEye which will be used by Visually disabled people.

为此,我需要控制屏幕上的滑动,以便整个应用程序只能通过滑动的帮助来处理。

ViewPager 、 ViewFlipper 、 SimpleOnGestureListener 不符合要求。

请建议应该使用什么技术。谢谢

最佳答案

基于这个问题,我可以建议使用 ViewPager

这是您的MOTTO的替代方案,而不是您问题的解决方案

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

ViewPagerActivity

    public class ViewPagerActivity extends Activity {
String text[] = {"A", "B",
"C", "D",
"E", "F",
"G", "H"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter(this, text);
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
//set Page Change Listner. to get callback on page changed or swiped
myPager .setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
Log.e("Page Changed ", " YES ");
/// here you can check & perform on changed
Log.e("Current TextView Text ", text[position]);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});

}
}

MyPagerAdapter

public class MyPagerAdapter extends PagerAdapter {

Activity activity;
int txtarray[];

public MyPagerAdapter(Activity act, int[] imgArra) {
txtarray = imgArra;
activity = act;
}

public int getCount() {
return txtarray.length;
}

public Object instantiateItem(View collection, int position) {
TextView view = new TextView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setText(txtarray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
return null;
}
}

关于java - Views Navigation 使用 Swipe Gesture android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757666/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com