2011-01-03 39 views
15

Vì vậy, tôi đã theo chủ đề cụ thể này (How to stop scrolling in a Gallery Widget?) nhưng tôi không thể làm cho nó hoạt động bình thường.Tạo Thư viện tùy chỉnh - ghi đè trênFling

Tôi đã tạo Thư viện mở rộng lớp MyGallery tùy chỉnh. Tôi đã thêm mã trong liên kết ở trên ... tôi có phải thêm <com.example.mygallery vào tệp XML không? Nếu vậy, tôi cũng thêm nhập khẩu vào tập tin java hoặc điều này là không cần thiết vì các tập tin XML? Tôi rất bối rối.

Tôi muốn chỉ đơn giản là làm cho thư viện di chuyển một hình ảnh cùng một lúc mỗi lần trượt.

tập tin 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: gói 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; 
     } 
    } 
} 

Tôi rất muốn được trợ giúp. Cảm ơn!!

~ Rick

Trả lời

23

Chỉ cần thêm param attrSet để các nhà xây dựng của bộ sưu tập tùy chỉnh của bạn:

super(ctx, attrSet); 

này đã làm việc cho tôi. Leo Vannucci

+0

Nó hoạt động! Cảm ơn mọi người. – user560837

+3

Có lẽ bạn có thể chấp nhận điều này làm câu trả lời cuối cùng? –

+0

đã giúp tôi quá .......... cảm ơn – viv

3

Có. Bạn chỉ nên sử dụng com.gallerytest.mygallery thay vì Thư viện trong XML. Mọi thứ sẽ hoạt động tốt bởi vì mygallery là một phân lớp của Gallery. Không cần nhập khẩu trong XML.

+0

Ok. Sau đó, nó xuất hiện rằng mã setAdapter của tôi không còn hoạt động chính xác với mã trên. Tất cả những gì tôi đã làm là thay đổi từ chế độ xem Thư viện thành Thư viện tùy chỉnh. Bất cứ ai cũng cảm thấy muốn thử mã và xem bạn có thể xác định được sự cố không? Tôi có lẽ nhìn cái gì đó đơn giản. THANKS! ... mặc dù có vẻ như tôi đã bỏ qua mã AddImgAdp ... nó được tìm thấy trong liên kết ở trên. – user560837

1

Thay đổi XML là chìa khóa ... tôi đã nhận được TypeCastException đôi khi và không thể tìm thấy lý do trong mã của tôi. Cuối cùng được tìm thấy trong bài đăng này "Bạn chỉ nên sử dụng com.gallerytest.mygallery thay vì Thư viện trong XML" và giải quyết được sự cố của tôi. cảm ơn rất nhiều.

Các vấn đề liên quan