How to limit or control scrolling in Android Gallery?
In this code, the scrolling starts from the middle and ends in middle, which shows a blank or white space either in left开发者_运维知识库 or right, how can i prevent this? the scrolling should stop with the last image and start with the first image.
g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//Toast.makeText(Tasks.this, "" + position, Toast.LENGTH_SHORT).show();
((TabActivity) getParent()).getTabHost().setCurrentTab(1);
}
});
g.scrollTo(5, 0);
g.setSelection(1);
g.setVerticalFadingEdgeEnabled(false);
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.dishwash,
R.drawable.shower_01,
R.drawable.wash_01,
R.drawable.dishwash,
R.drawable.shower_01,
R.drawable.wash_01,
R.drawable.dishwash
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.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 i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(102, 150));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(R.drawable.dishwash);
return i;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onClick(View v) {
AppConstants.ACTIVE_TASK = (CurrentIndex + 1);
switch(v.getId())
{
case R.id.button_right:
if(CurrentIndex < (ItemsInGallery-2))
CurrentIndex++;
if(CurrentIndex>ItemsInGallery-1)
CurrentIndex=0;
g.setSelection(CurrentIndex,true);
break;
case R.id.button_left:
Log.d("Current index ", "--->" + CurrentIndex);
if(CurrentIndex>3)
CurrentIndex=CurrentIndex-1;
if(CurrentIndex<0)
CurrentIndex=ItemsInGallery-1;
if(CurrentIndex == 0)
g.setSelection(1,true);
else
g.setSelection(CurrentIndex,true);
break;
case R.id.button_add:
startActivity(new Intent(this, Form.class));
break;
case R.id.button_edit:
startActivity(new Intent(this, EditTasks.class));
break;
}
}
use this code change the 130 to ur image width
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams(); mlp.setMargins(-(metrics.widthPixels/2+130),mlp.topMargin,mlp.rightMargin, mlp.bottomMargin );
精彩评论