创建自定义图库 - 覆盖 onFling

发布于 2024-10-10 05:44:40 字数 4202 浏览 7 评论 0 原文

因此,我遵循了这个特定的线程(如何停止在Gallery Widget?)但我无法让它正常工作。

我创建了一个扩展 Gallery 的自定义 MyGallery 类。我已在上面的链接中添加了代码...我应该将 添加到 XML 文件吗?如果是这样,我是否还要将导入添加到 java 文件,或者由于 XML 文件而不需要这样做?我很困惑。

我只想让图库每次移动一张图像。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carlot_background"
    >
<com.gallerytest.mygallery
    android:id="@+id/thisgallery"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

mygallery.java:

package com.gallerytest;

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

public class mygallery extends Gallery {

    public mygallery(Context ctx, AttributeSet attrSet) {
        super(ctx);
        // TODO Auto-generated constructor stub
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
           return e2.getX() > e1.getX(); 
        }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
      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;  
    }

}

main.java: 包com.gallerytest;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class main extends Activity {
    /** Called when the activity is first created. */

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

         mygallery gallery = (mygallery) findViewById(R.id.thisgallery);

         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {

                Toast.makeText(main.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }

        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;


        private Integer[] Imgid = {
                R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5};

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typArray.recycle();
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);

            imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgView.setBackgroundResource(0x0106000d);
            imgView.setLayoutParams(new mygallery.LayoutParams(300, 240));

            return imgView;
        }
    }
}

我很想得到一些帮助。谢谢!!

~瑞克

So, I've followed this particular thread (How to stop scrolling in a Gallery Widget?) yet I am unable to get it to work properly.

I've created a custom MyGallery class extending Gallery. I've added the code in the link above...am I supposed to add <com.example.mygallery to the XML file? If so, do I also add the import to the java file or is this not needed because of the XML file? I'm so very confused.

I want to simply make the gallery move one image at a time per fling.

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carlot_background"
    >
<com.gallerytest.mygallery
    android:id="@+id/thisgallery"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

mygallery.java:

package com.gallerytest;

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

public class mygallery extends Gallery {

    public mygallery(Context ctx, AttributeSet attrSet) {
        super(ctx);
        // TODO Auto-generated constructor stub
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
           return e2.getX() > e1.getX(); 
        }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
      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;  
    }

}

main.java:
package com.gallerytest;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class main extends Activity {
    /** Called when the activity is first created. */

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

         mygallery gallery = (mygallery) findViewById(R.id.thisgallery);

         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {

                Toast.makeText(main.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }

        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;


        private Integer[] Imgid = {
                R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5};

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typArray.recycle();
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);

            imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgView.setBackgroundResource(0x0106000d);
            imgView.setLayoutParams(new mygallery.LayoutParams(300, 240));

            return imgView;
        }
    }
}

I'd love some help. Thanks!!

~Rick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十六岁半 2024-10-17 05:44:40

只需将 attrSet 参数添加到自定义图库的构造函数中:

super(ctx, attrSet);

这对我有用。
利奥·万努奇

Just add the attrSet param to the constructor of your custom gallery:

super(ctx, attrSet);

This worked for me.
Leo Vannucci

巨坚强 2024-10-17 05:44:40

是的。您应该只在 XML 中使用 com.gallerytest.mygallery 而不是 Gallery。一切都会正常工作,因为 mygallery 是 Gallery 的子类。无需以 XML 形式导入。

Yes. You should just use com.gallerytest.mygallery instead of Gallery in XMLs. Everything will be working fine becuase mygallery is a subclass of Gallery. No need for imports in XML.

千柳 2024-10-17 05:44:40

更改 XML 是关键...我已经收到 TypeCastException 一段时间了,但在我的代码中找不到原因。最后在这篇文章中找到“你应该只使用 com.gallerytest.mygallery 而不是 XML 中的 Gallery”并解决了我的问题。多谢。

Changing the XML was key...i had been getting TypeCastException for sometime and couldn't find the reason in my code. Finally found in this post " You should just use com.gallerytest.mygallery instead of Gallery in XMLs" and solved my problem. thanks a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文