您好我是编程和Android开发的新手。我正在做一个项目,并希望有人可以帮助我。
我有两个活动,一个是MonthSelect,它包含一年中每个月的按钮。另一个活动是QuoteDisplay,它包含动态ImageView,图像数组列表和滑动手势检测器。
我试图让每个按钮在数组列表中的不同图像上打开图像视图,然后允许我使用滑动手势正常滚动图像。
例如,如果按下二月按钮,图像视图将显示二月的第一张图像,然后我可以在二月向左滑动到第二张图像。或者,如果按下May按钮,imageview将显示May的第一张图像,并允许我按照自己的喜好滚动图像。
Iv尝试使用各自具有不同信息的意图来制作那么独特的我在某个地方读它说可以工作但是它没有活动将继续打开相同的图像。不幸的是,老实说我不知道任何其他方法,因为我只是学习并认为这只是一个简单的任务。
我已经提供了代码的简化版本,如果有人可以提供帮助,我会非常感激。
MonthSelect类
public class MonthSelect extends Activity {
//ImageView images;
//ImageView imagesTest;
Button jan;
Button feb;
Button march;
Button april;
Button may;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.month_select);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
findViewById(R.id.januaryButton).setOnClickListener(buttonClickListener);
findViewById(R.id.februaryButton).setOnClickListener(buttonClickListener);
findViewById(R.id.marchButton).setOnClickListener(buttonClickListener);
findViewById(R.id.aprilButton).setOnClickListener(buttonClickListener);
findViewById(R.id.mayButton).setOnClickListener(buttonClickListener);
}
final OnClickListener buttonClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
//images = (ImageView)findViewById(R.id.monthImageDisplay);
//imagesTest = (ImageView)findViewById(R.id.imagesTest);
switch(v.getId()){
case R.id.januaryButton:
Intent janIntent = new Intent(MonthSelect.this,MonthDisplay.class);
//testing to see if pressed
Toast.makeText(getApplicationContext(),"January", Toast.LENGTH_SHORT).show();
break;
case R.id.februaryButton:
//testing to see if pressed
Toast.makeText(getApplicationContext(), "February", Toast.LENGTH_SHORT).show();
break;
case R.id.marchButton:
//testing to see if pressed
Toast.makeText(getApplicationContext(), "March", Toast.LENGTH_SHORT).show();
break;
case R.id.aprilButton:
//testing to see if pressed
Toast.makeText(getApplicationContext(), "April", Toast.LENGTH_SHORT).show();
break;
case R.id.mayButton:
//testing to see if pressed
Toast.makeText(getApplicationContext(), "May", Toast.LENGTH_SHORT).show();
break;
}//switch
}//onClick
};//buttonClickListener
}
QuoteDisplay
public class QuoteDisplay extends Activity implements OnGestureListener {
RelativeLayout r1;
private ImageView imageView;
TextView textView;
GestureDetector detector;
Calendar cal = Calendar.getInstance();
@SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("DDD");
String strDate = sdf.format(cal.getTime());
int parseStrDate = Integer.parseInt(strDate);
int imageArrayCorrection = (parseStrDate -1);
private int currentImage = imageArrayCorrection;
//list of images
public int[] imageList =
{R.drawable.j_1, R.drawable.j_2, R.drawable.j_3, R.drawable.j_4,
R.drawable.j_5, R.drawable.j_6, R.drawable.j_7, R.drawable.j_8,
R.drawable.j_9, R.drawable.j_10, R.drawable.j_11, R.drawable.j_12,
R.drawable.j_13, R.drawable.j_14, R.drawable.j_15, R.drawable.j_16,
R.drawable.j_17, R.drawable.j_18, R.drawable.j_19, R.drawable.j_20,
R.drawable.j_21, R.drawable.j_22, R.drawable.j_23, R.drawable.j_24,
R.drawable.j_25, R.drawable.j_26, R.drawable.j_27, R.drawable.j_28,
R.drawable.j_29, R.drawable.j_30, R.drawable.j_31,
R.drawable.f_1, R.drawable.f_2, R.drawable.f_3, R.drawable.f_4,
R.drawable.f_5, R.drawable.f_6, R.drawable.f_7, R.drawable.f_8,
R.drawable.f_9, R.drawable.f_10, R.drawable.f_11, R.drawable.f_12,
R.drawable.f_13, R.drawable.f_14, R.drawable.f_15, R.drawable.f_16,
R.drawable.f_17, R.drawable.f_18, R.drawable.f_19, R.drawable.f_20,
R.drawable.f_21, R.drawable.f_22, R.drawable.f_23, R.drawable.f_24,
R.drawable.f_25, R.drawable.f_26, R.drawable.f_27, R.drawable.f_28,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quote_display);
detector = new GestureDetector(this, this);
imageView();
}//onCreate
//Dynamic ImageView
protected void imageView(){
r1 = (RelativeLayout)findViewById(R.id.r1);
imageView = new ImageView (this);
imageView.setImageResource(imageList[imageArrayCorrection]);
//Setting the parameters of the relative layout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int)LayoutParams.MATCH_PARENT,
(int)LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
r1.addView(imageView);
}//imageView
//registering TouchEvent with GerstureDetector
public boolean onTouchEvent(MotionEvent event){
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override//Swipe gestures for left and right
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float sensitivity = 50;
//swipe left check
if(e1.getX() - e2.getX()>sensitivity){
//increase counter to move to the next image
currentImage++;
currentImage = (currentImage + imageList.length)%imageList.length;
imageView.setImageResource(imageList[currentImage]);
//testing Fling Gesture
// Toast.makeText(getApplicationContext(), "<- Fling Gesture Left", 100).show();
return true;
}
//swipe right check
if(e2.getX() - e1.getX()>sensitivity){
//decrease counter to go back to an image
currentImage--;
currentImage = (currentImage + imageList.length)%imageList.length;
imageView.setImageResource(imageList[currentImage]);
//testing Fling Gesture
// Toast.makeText(getApplicationContext(), "Fling Gesture Right ->", 100).show();
}
return true;
}
答案 0 :(得分:0)
我会为每个月制作12个带有图片路径的int-Arrays。 在MonthSelect中,你把你的意图放在一个项目上。您可以阅读here如何操作。 按下按钮时,您将创建一个新的Intent。根据选择的月份,您将右侧Extra添加到此Intent。 例如1月:
Intent intent = new Intent(MonthSelect.this,MonthDisplay.class);
//...
intent.putExtra("Month", 1); // '1' because January is the first Month
然后在QuoteDisplay的onCreate方法中存储名为&#34;月&#34;的Intent-Item;另一个属性:
int selectedMonth = getIntent().getExtras().getInt("Month");
现在您知道所选的月份,并且可以为ImageView使用正确的图片阵列。