我很绝望。我试图onClickListener
在 Fragment 类中添加一个。这个想法是我有一个ViewPager
图像。我想要的只是单击图像本身并让它显示吐司消息。我知道您不能将 an 注册OnClickListener
到ViewPager
自身,因此我尝试将其添加Fragment
到处理ImageView
. 现在发生的情况是,该onClick
方法有效,如果我单击其中一个图像,它会显示一条消息,但它会继续显示我使用的 DEFAULT 开关盒。我这辈子都无法理解为什么......!:( 这是代码,也许您可以看到解决方案?
谢谢。
主要活动:
package com.example.viewpagerexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity{
private static final int NUM_PAGES = 5;
private MyAdapter mAdapter;
private ViewPager mPager;
private Button leftBTN;
private Button rightBTN;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
leftBTN = (Button)findViewById(R.id.leftBTN);
leftBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(mPager.getCurrentItem()-1, true);
}
});
rightBTN = (Button)findViewById(R.id.rightBTN);
rightBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(mPager.getCurrentItem()+1, true);
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Fragment getItem(int position) {
ImageFragment myFragment = new ImageFragment();
Bundle data = new Bundle();
data.putInt("current_page", position);
myFragment.setArguments(data);
return myFragment;
}
}
}
这是带有 OnClickListener 实现的 Fragment 类:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageFragment extends Fragment implements OnClickListener{
private int position;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
position = data.getInt("current_page", 0);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_details, container, false);
imageView = (ImageView) view.findViewById(R.id.imageView1);
imageView.setOnClickListener(this);
switch (position) {
case 0:
imageView.setImageResource(R.drawable.splash);
return view;
case 1:
imageView.setImageResource(R.drawable.girrafe);
return view;
case 2:
imageView.setImageResource(R.drawable.lion);
return view;
case 3:
imageView.setImageResource(R.drawable.monkey);
return view;
case 4:
imageView.setImageResource(R.drawable.chicken);
return view;
default:
return null;
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.drawable.splash:
Log.d("DEBUG", "image 0 pressed");
Toast.makeText(getActivity(), "Pic 1", Toast.LENGTH_SHORT).show();
return;
case R.drawable.girrafe:
Log.d("DEBUG", "image 1 pressed");
Toast.makeText(getActivity(), "Pic 2", Toast.LENGTH_SHORT).show();
return;
case R.drawable.lion:
Log.d("DEBUG", "image 2 pressed");
Toast.makeText(getActivity(), "Pic 3", Toast.LENGTH_SHORT).show();
return;
case R.drawable.monkey:
Log.d("DEBUG", "image 3 pressed");
Toast.makeText(getActivity(), "Pic 4", Toast.LENGTH_SHORT).show();
return;
case R.drawable.chicken:
Log.d("DEBUG", "image 4 pressed");
Toast.makeText(getActivity(), "Pic 5", Toast.LENGTH_SHORT).show();
return;
default:
Log.d("DEBUG", "v.getId(): " + v.getId());
Log.d("DEBUG", "default pressed");
Toast.makeText(getActivity(), "DEFAULT pressed!", Toast.LENGTH_SHORT).show();
return;
}
}
}