1

在此代码中,滚动从中间开始并在中间结束,左侧或右侧显示空白或空白,我该如何防止这种情况?滚动应从最后一张图像停止并从第一张图像开始。

    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;

      }
}
4

1 回答 1

0

使用此代码将 130 更改为您的图像宽度

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 );   
于 2011-05-18T09:33:35.450 回答