创建自定义库 - 压倒一切的onFling [英] Creating a custom Gallery - overriding onFling
问题描述
所以,我跟着这个特定的线程(如何制止滚动图库工具?),但我不能让它正常工作。
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.
我创建扩展库的自定义MyGallery类。我已经添加了code在上面...链接我应该添加< com.example.mygallery
XML文件?如果是这样,我也导入添加到Java文件或这不是因为XML文件所需要的?我很很困惑。
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文件:
<?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:
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;
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!!
〜里克
推荐答案
就在attrSet参数添加到自定义库的构造函数:
Just add the attrSet param to the constructor of your custom gallery:
super(ctx, attrSet);
这为我工作。 狮子座万努奇
This worked for me. Leo Vannucci
这篇关于创建自定义库 - 压倒一切的onFling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!