Android Gallery Infinite Loop

Source: Internet
Author: User

Package gongzibai. co. cc;
Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Import android. widget. Toast;
Public class Gallery1Activity extends
Activity {
Public static final int CHANGE_BANNER = 1;
Private int cur_index = 0;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (
Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final DetialGallery gallery = (DetialGallery) findViewById (R. id. widget1 );
ImageAdapter imageAdapter = new ImageAdapter (
Gallery1Activity. this );
Gallery. setAdapter (imageAdapter );
Final Handler handler = new Handler (){
Public void handleMessage (
Message msg ){
Switch (msg. what ){
Case CHANGE_BANNER:
Gallery. onKeyDown (
KeyEvent. KEYCODE_DPAD_RIGHT,
Null );
Break;
Default:
Break;
}
};
};
// New Thread (new Runnable (){
// Int flag = 1;
//
// Public void run (){
//
// While (true ){
//
// Handler. sendEmptyMessage (CHANGE_BANNER );
//
// Try {
//
// Thread. sleep (1000 );
//
//} Catch (InterruptedException e ){
//
// E. printStackTrace ();
//
//}
//
//}
//
//}
//
//}
//
//). Start ();
Gallery. setOnItemSelectedListener (new OnItemSelectedListener (){
@ Override
Public void onItemSelected (
AdapterView <?> Arg0,
View arg1,
Int arg2, long arg3 ){
// TODO Auto-generated method stub
Toast. makeText (
Gallery1Activity. this,
"This is the first" + arg2
+ "Images ",
2). show ();
}
@ Override
Public void onNothingSelected (
AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
});
}
Class ImageAdapter extends
BaseAdapter {
Context context;
Int [] imgResId = new int [] {
R. drawable. icon,
R. drawable. icon2,
R. drawable. icon3,
R. drawable. icon,
R. drawable. icon2,
R. drawable. icon3,
};
Public ImageAdapter (
Gallery1Activity gallery1Activity ){
// TODO Auto-generated constructor stub
Super ();
This. context = gallery1Activity;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
// Return Integer. MAX_VALUE;
Return Integer. MAX_VALUE;
}
@ Override
Public Object getItem (
Int position ){
// TODO Auto-generated method stub
Return position;
//
// Return position;
// Return position;
}
@ Override
Public long getItemId (
Int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override www.2cto.com
Public View getView (int arg0,
View arg1,
ViewGroup arg2 ){
// TODO Auto-generated method stub
ImageView imageView = new ImageView (
Context );
ImageView
. SetImageResource (imgResId [arg0% imgResId. length]);
ImageView
. SetScaleType (ImageView. ScaleType. FIT_XY );
ImageView
. SetLayoutParams (new Gallery. LayoutParams (
175,200 ));
Return imageView;
}
}
}
Author: pingpang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.