2

我正在为当地报纸和 atm 开发 android 应用程序,我正在尝试实现文章之间的滑动。我的主要活动是 HNappActivity,文章展示活动是 AcrticleActivity。我的问题是,刷卡不起作用,我很绝望,因为我不知道出了什么问题。这是实现刷卡的代码:

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;
            }
}   

}

4

2 回答 2

1

我什至没有阅读代码,问题是您正试图在活动之间滑动。您应该尝试在同一活动中的所有片段之间滑动。

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

编辑:

    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;
    }
于 2012-08-02T10:54:12.280 回答
1

另一种方法是使用 ViewPager http://developer.android.com/reference/android/support/v4/view/ViewPager.html

这是一个指南:http ://android-developers.blogspot.se/2011/08/horizo​​ntal-view-swiping-with-viewpager.html

于 2012-08-02T10:59:09.927 回答