Gallery實現快速拖動只滑動一頁的解決辦法,gallery一頁

來源:互聯網
上載者:User

Gallery實現快速拖動只滑動一頁的解決辦法,gallery一頁


這年頭用gallery的已經很少了,此文提供一個一次滑動只滑動到下一頁的方法(包括快速滑動)。


import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class RecommendGallery extends Gallery {
   
    public RecommendGallery(Context context) {
     super(context);
    }

    public RecommendGallery(Context context, AttributeSet attrs) {
     super(context, attrs);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
     // TODO Auto-generated method stub
     int kEvent;
           if(isScrollingLeft(e1, e2)){ //Check if scrolling left
             kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
           }
           else{ //Otherwise scrolling right
             kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
           }
           onKeyDown(kEvent, null);
           return true;
    
    }
    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
           return e2.getX() > e1.getX();
    }
   }


android 的Gallery怎實現同時左右與上下滑動

你如果是要在Gallery裡面去簡單的嵌套ScrollView是不可行的,橫向滑動和縱向滑動事件會衝突。
要麼你自己重寫gallery,將橫向滑動事件嵌在scrollView上;要麼你重寫ScrollView,將ScrollView縱向滑動事件在gallery裡面去處理。

忘記說一點,在ScrollView裡面嵌套gallery是可以的,但是也是僅限於小範圍的,比如在你顯示諸多內容的頂部有4,5張圖片的滑動展示。
 
android 在Gallery下面加入導航條視圖,當拖動Gallery時,導航條根據當前圖片在Gallery中的位置而顯示進度

Gallery是一個類似於ListView的視圖可以實現左右緩慢拖動螢幕切換圖片的功能

package com.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class SpinnerActivity extends Activity {
private Gallery gallery;
private List<Bitmap> list = new ArrayList<Bitmap>();;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.images);
init();
gallery.setAdapter(new MyAdapter());

}

private void init() {
// 通過圖片工廠得到圖片對象,並加入到集合中
list.add(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
list.add(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
list.add(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
list.add(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
list.add(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

}

class MyAdapter extends BaseAdapter...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.