下面的代码正在从左到右滑动,现在我想the
从右到左设置相同的东西。滑动正在工作,但是发言正在与 position.in pageAdapter 向前滑动工作良好。向后滑动也在工作很好,但 Textto 语音无法正常工作
下面是代码:
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
在这里它不起作用。谁能给我建议并让我摆脱这个问题
public class Learn extends Activity implements TextToSpeech.OnInitListener{
private TextToSpeech tts;
private ViewPager pic;
private Button next,back;
private TextView text;
private Context context;
private int i=0;
public Integer[] fruits={R.drawable.apple1,R.drawable.banana1,R.drawable.grapes1,R.drawable.cherry1,R.drawable.lemon1,
R.drawable.orange1,R.drawable.strwberry1,R.drawable.water1};
public String [] names={"Apple","Banana","Grapes","Cherry","Lemon","Orange","Strawberry","Water melon"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn);
tts = new TextToSpeech(this, this);
pic=(ViewPager)findViewById(R.id.view_pager);
next=(Button)findViewById(R.id.button1);
back=(Button)findViewById(R.id.button2);
text=(TextView)findViewById(R.id.text);
ImagePageAdapter adapterpic=new ImagePageAdapter(this);
pic.setAdapter(adapterpic);
}
private class ImagePageAdapter extends PagerAdapter {
public ImagePageAdapter(Context learn) {
// TODO Auto-generated constructor stub
context=learn;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView image=new ImageView(context);
image.setImageResource(fruits[position]);
((ViewPager) container).addView(image);
if(position>=1){
image.setImageResource(fruits[position]);
speakOut(names[position-1]);
}
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
private void speakOut(String text) {
// TODO Auto-generated method stub
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
pic.setEnabled(true);
//speakOut("apple");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
}
Here is stack trace
02-27 16:39:00.994: I/TTS received:(2369): Apple
02-27 16:39:05.144: I/TTS received:(2369): Banana
02-27 16:39:06.534: I/TTS received:(2369): Grapes
02-27 16:39:24.273: I/TTS received:(2369): Banana
02-27 16:39:25.355: I/TTS received:(2369): Grapes
02-27 16:39:26.593: I/TTS received:(2369): Cherry
02-27 16:39:28.624: I/TTS received:(2369): Apple
请给我建议以摆脱这个问题