5

私のアプリ アクティビティの 1 つは DayGallery アクティビティ (無限の画像ギャラリー) と呼ばれ、

ギャラリーを開くと、最初の画像がランダムに表示され、DayGallery アクティビティ コードで指定した最初の画像から開始されません。

達成しようとしているのは次のとおりです。

FIRST:以下のように、DayGallery アクティビティ コードで指定された最初の画像から開始します。

Day1 ギャラリーを開くと、最初に表示される画像は次のとおりです。

R.drawable.day_one_1

Day2 ギャラリーを開くと、最初に表示される画像は次のとおりです。

R.drawable.day_two_1

同様に、すべての Days ギャラリーについて、両側で無限スクロールを維持します。

2番目: たとえばday_one_7という名前の画像で停止したギャラリーにいる場合、戻るを押して前のアクティビティに移動し、再びギャラリーに戻ります。ギャラリーを離れる前に見たのと同じ画像を見たいのですが、アプリを終了してからgallery 再び、DayGallery アクティビティ コードで指定された最初の画像を表示するようにリセットする必要があります。

ここに画像の説明を入力

実際、私はその目的でグーグルを検索しましたが、それについて役立つことは何もありません。

どんな助けでも大歓迎です。

DayGallery.java:

 @SuppressWarnings("deprecation")
public class DayGallery extends Activity {
TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    // Set the layout to use
    setContentView(R.layout.main);
    if (customTitleSupported) { 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
         tv = (TextView) findViewById(R.id.title_tv1); 
         tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
         }           
    InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
    galleryOne.setAdapter(initializeImages()); 
    galleryOne.setSelection(galleryOne.getCount()/2);  
    }  

private InfiniteGalleryAdapter initializeImages() {
    InfiniteGalleryAdapter galleryAdapter = null;

    String day = getIntent().getStringExtra("dayname");

    if(day.equalsIgnoreCase("Day1")){
        int[] tempimages = { R.drawable.day_one_1, R.drawable.day_one_2,R.drawable.day_one_3, 
                R.drawable.day_one_4, R.drawable.day_one_5,R.drawable.day_one_6,R.drawable.day_one_7,       
                R.drawable.day_one_8, R.drawable.day_one_9,R.drawable.day_one_10,R.drawable.day_one_11,
                R.drawable.day_one_12
        };  
        String[] name = { "00:35","00:35","00:35","1:07","2:00","2:01","2:09",
                          "2:12","2:15","6:13","6:13","6:13"
        };  
        tv.setText("Day one pictures");
        galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name); 
        }       
    else if(day.equalsIgnoreCase("Day2")){
        int[] tempimages = { R.drawable.day_two_1, R.drawable.day_two_2,R.drawable.day_two_3, 
                R.drawable.day_two_4, R.drawable.day_two_5,R.drawable.day_two_6,R.drawable.day_two_7,
                R.drawable.day_two_8, R.drawable.day_two_9,R.drawable.day_two_10,R.drawable.day_two_11,
                R.drawable.day_two_12
        };  
        String[] name = { "12:04","12:04", "12:04","12:05","12:06", "12:07",
                          "12:07","12:07","12:08","12:10","12:10","12:10"
        };  
        tv.setText("Day two pictures"); 
        galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name); 
        }

    // AND THE SAME FOR REST OF DAYS TILL Day10//

    return galleryAdapter; 
    }
}

 class InfiniteGalleryAdapter extends BaseAdapter { 
private Context mContext;
private int[] images;   
private String[] name;
public InfiniteGalleryAdapter(Context c, int[] imageIds,String[] names) { 
    this.mContext = c; 
    images = imageIds;
    name=names;
    inflater = (LayoutInflater)mContext.getSystemService ( Context.LAYOUT_INFLATER_SERVICE); 
    } 
public int getCount() { 
    return Integer.MAX_VALUE; 
    } 
public Object getItem(int position) { 
    return position; 
    } 
public long getItemId(int position) { 
    return position; 
    } 
private LayoutInflater inflater=null; 

public class ViewHolder{ 
    public TextView text; 
    public ImageView image; 
    } 

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView i = getImageView(); 

    int itemPos = (position % images.length); 

    try { i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable()).setAntiAlias(true); 
    } 
    catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of memory creating imageview. Using empty view.", e); 
    } 
    View vi=convertView; 
    ViewHolder holder; 
    if(convertView==null){ 
        vi = inflater.inflate(R.layout.gallery_items, null); 
        holder=new ViewHolder(); 
        holder.text=(TextView)vi.findViewById(R.id.textView1); 
        holder.image=(ImageView)vi.findViewById(R.id.image); 
        vi.setTag(holder); 
        } 
    else holder=(ViewHolder)vi.getTag(); 
    holder.text.setText(name[itemPos]); 

    final int stub_id=images[itemPos]; 
    holder.image.setImageResource(stub_id); 

    return vi; 
    } 

private ImageView getImageView() { 

    ImageView i = new ImageView(mContext); 

    return i; 
    } 
}

  @SuppressWarnings("deprecation")
 class InfiniteGallery extends Gallery {

public InfiniteGallery(Context context) {
    super(context);
    init(); 
    }

public InfiniteGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(); 
    }

public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(); 
    }

private void init(){
    // These are just to make it look pretty
    setSpacing(25);
    setHorizontalFadingEdgeEnabled(false); 
    }
}

アップデート:

次のコード行を追加します。

 galleryOne.setSelection(0);

この行の後:

galleryOne.setSelection(galleryOne.getCount()/2);  

私のコードでは、DayGallery アクティビティで指定されたとおりに最初の画像が表示されますが、左側のみに一方向の無限スクロールが発生し、両側にはスクロールされません。

DayGalleryアクティビティで指定された最初の画像を表示して、ギャラリー画像の双方向の無限スクロールを取得するにはどうすればよいですか?

助けてくれて本当にありがとう、ありがとう。

4

1 に答える 1