开发者

"snapping" horizontalscrollview like the home screen do

开发者 https://www.devze.com 2023-02-11 13:39 出处:网络
I would like to know how to make the horizontalscrollview \"snapping\" like the home screen do, meaning i have two pages in my horizontalscrollview and i can swipe between each pag开发者_运维问答e.You

I would like to know how to make the horizontalscrollview "snapping" like the home screen do, meaning i have two pages in my horizontalscrollview and i can swipe between each pag开发者_运维问答e.


You Have two options for that.

either use ViewFlipper or android Gallery.

I prefer you to use android Gallery as it will be controlled and better approach.

In the Default Android Gallery Example that you will find developer.android.com site In the adapter it used and in the getView() method an ImageView is returned. You manipulate your code in a manner that it return a inflated xml layout like the following code

  class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;



    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return 5;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.ownview, null);

        return convertView;
    }
}


There is no built in Android view/widget to achieve this, but there is an open source project that includes an excellent implementation called SwipeView which I have used successfully in a number of projects. It is a custom view that you use in a similar manner to many of the other ViewGroup implementations:

https://github.com/fry15/uk.co.jasonfry.android.tools


you might be looking for android.support.v4.view.ViewPager.

this thing sort of use Adapters with Fragments, and makes life quite easy. for a start, take a look here:

http://android-developers.blogspot.co.nz/2011/08/horizontal-view-swiping-with-viewpager.html

0

精彩评论

暂无评论...
验证码 换一张
取 消