基本上我有一个阵列有2个图像,好像现在但最终会有更多。 每隔几秒就从阵列切换器发送图像。我坚持使用onClick方法。当用户点击该图像时,最终目标就是打开新的Activity类。每个图像都有自己的Activity类,因此当用户点击它时,它应该定向到该特定的类。
我昨天得到了一些帮助
public void onClick(View v) {
Intent n = null;
int id = v.getId();
switch (id){
case R.id.someId:
n = new Intent(getActivity(), FragMent1.class);
break;
case R.id.someOtherId:
n = new Intent(getActivity(), FragMent2.class);
break;
}
我可以将第一个案例陈述设置为" R.id.textSwitcher"但第二是问题。因为我希望图像动画在一个地方,新的R.id ...意味着新的位置也是新的" mSwitcher1.setOnClickListener(新的View.OnClickListener()"
public class Animation extends Fragment implements AnimationListener {
public HomeAnimation(){}
private static boolean flag = false;
Animation anim;
private Handler handler;
public ImageSwitcher mSwitcher,mSwitcher1;
int textToShow[] = {R.drawable.dabangg, R.drawable.car };
int messageCount = textToShow.length;
int currentIndex = -1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.imgswitch, container, false);
mSwitcher = (ImageSwitcher) v.findViewById(R.id.imageSwitcher1);
mSwitcher.setFactory(mFactory);
anim = AnimationUtils.loadAnimation(getActivity(),R.anim.fade_in);
mSwitcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent n = null;
int id = v.getId();
// Fragment fragment = null;
//Bundle args = new Bundle();
switch (v.getId()){
case 0:
n = new Intent(getActivity(), Fragment1.class);
getActivity().startActivity(n);
break;
case 1:
n = new Intent(getActivity(), Fragment2.class);
getActivity().startActivity(n);
break;
}
}
});
// set animation listener
anim.setAnimationListener(this);
return v;
}
public void updateTextView() {
int maxIndex = textToShow.length;
Random random = new Random();
int generatedIndex = random.nextInt(maxIndex);
mSwitcher.setImageResource(textToShow[generatedIndex]);
}
以下是我更新的内容,如果有人需要它
public void updateTextView() {
int maxIndex = textToShow.length;
Random random = new Random();
final int generatedIndex = random.nextInt(maxIndex);
mSwitcher.setImageResource(textToShow[generatedIndex]);
mSwitcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent n = null;
switch(generatedIndex){
case 0:
n = new Intent(getActivity(), Fragment1.class);
getActivity().startActivity(n);
break;
case 1:
n = new Intent(getActivity(), Fragment2.class);
getActivity().startActivity(n);
break;
case 2:
n = new Intent(getActivity(), Fragment3.class);
getActivity().startActivity(n);
break;
}
}
});
答案 0 :(得分:1)
我现在没有时间对此进行测试,而且我不完全确定我明白你的问题在哪里,但试试这个,看看它是不是你想要的。我会在代码中添加注释
mSwitcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent n = null;
int id = v.getId();
// Fragment fragment = null;
//Bundle args = new Bundle();
switch (v.getId()){
case 0:
/*
below should give you the index for the displayed image
you can compare this to the ids in your array using the index
*/
if (v.getDisplayedChild() == 0) { // if 1st image in array do this
n = new Intent(getActivity(), Fragment1.class);
getActivity().startActivity(n);
}
if (v.getDisplayedChild() == 1) { // if 2nd image in array do this
n = new Intent(getActivity(), Fragment2.class);
getActivity().startActivity(n);
}
break;