android - swiping between activities

萝らか妹 提交于 2019-12-11 02:27:59

问题


I'm developing android app for a local newspapers and atm I'm trying to implement swiping between articles. My main activity is HNappActivity and activity for article showing is AcrticleActivity. My problem is, that the swiping just wont work and I'm desperate because I have no idea whats wrong. Here's code, where swiping is implemented:

public class ArticleActivity extends SherlockActivity {

int hnCatIndex, hnArtIndex;
WebView hnWebView;

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;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.article);

    hnCatIndex = getIntent().getExtras().getInt("catIndex", 0);
    hnArtIndex = getIntent().getExtras().getInt("artIndex", 0);
    Article article = Source.getInstance().getCategory(hnCatIndex).getArticle(hnArtIndex);

    hnWebView=(WebView)findViewById(R.id.webview);
    hnWebView.loadData(article == null ? "" : article.getBody(), "text/html",
                    "utf-8");
    gestureDetector = new GestureDetector(new MyGestureDetector());
    View articleview=(View) findViewById(R.id.article_main);

    articleview.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    });
}

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                return false;
            }

            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            hnArtIndex++;
            Intent intent = new Intent(ArticleActivity.this.getBaseContext(), ArticleActivity.class);
            intent.putExtra("catIntext",hnCatIndex);
            intent.putExtra("artIndex",hnArtIndex);
            startActivity(intent);          
            ArticleActivity.this.overridePendingTransition(
            R.anim.slide_in_right,
            R.anim.slide_out_left
            );
            // right to left swipe
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                if (hnArtIndex==1) {
                    Intent intent = new Intent(ArticleActivity.this.getBaseContext(), HNappActivity.class);
                    startActivity(intent);
                    }
                else {
                    hnArtIndex--;
                    Intent intent = new Intent(ArticleActivity.this.getBaseContext(), ArticleActivity.class);
                    intent.putExtra("catIntext",hnCatIndex);
                    intent.putExtra("artIndex",hnArtIndex);
                    startActivity(intent);
                }
            ArticleActivity.this.overridePendingTransition(
            R.anim.slide_in_left, 
            R.anim.slide_out_right
            );
            }

            return false;
        }
            @Override
            public boolean onDown(MotionEvent e) {
                    return true;
            }
}   

}


回答1:


I didn't even read the code, the problem is that you're trying to swipe between activities. You should be trying to swipe between fragments, all fragments in the same activity.

see: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html http://developer.android.com/reference/android/support/v4/view/ViewPager.html

edit:

    private WebView hnWebView;
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.webViewFragment, null);
        hnWebView=(WebView)v.findViewById(R.id.webview);
        return v;
    }



回答2:


An alternative could be using a ViewPager http://developer.android.com/reference/android/support/v4/view/ViewPager.html

Here is a guide: http://android-developers.blogspot.se/2011/08/horizontal-view-swiping-with-viewpager.html



来源:https://stackoverflow.com/questions/11775757/android-swiping-between-activities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!