【问题标题】:Creating a custom Gallery - overriding onFling创建自定义库 - 覆盖 onFling
【发布时间】:2011-01-03 04:50:36
【问题描述】:

所以,我已经关注了这个特定的线程 (How to stop scrolling in a Gallery Widget?),但我无法让它正常工作。

我创建了一个自定义 MyGallery 类来扩展 Gallery。我已经在上面的链接中添加了代码...我应该将<com.example.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;
        }
    }
}

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

~瑞克

【问题讨论】:

    标签: android class gallery


    【解决方案1】:

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

    super(ctx, attrSet);
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 好的。然后看来我的 setAdapter 代码不再适用于上述代码。我所做的只是从图库视图更改为自定义图库。任何人都想尝试一下代码,看看你是否能找出问题所在?我可能忽略了一些简单的事情。谢谢!...虽然我似乎遗漏了 AddImgAdp 代码...它可以在上面的链接中找到。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-07-17
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多