0

我很绝望。我试图onClickListener在 Fragment 类中添加一个。这个想法是我有一个ViewPager图像。我想要的只是单击图像本身并让它显示吐司消息。我知道您不能将 an 注册OnClickListenerViewPager自身,因此我尝试将其添加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;
    }
}

}

4

1 回答 1

0

我认为在您的 onclick 方法中,v.getId()(单击视图的 id)总是返回 R.id.imageView1,这就是为什么您总是在 onClick 中获得默认开关盒的原因。

您将 onClickListener 设置为您的 imageView,但是您忘记更改 imageview 的 id,因此 id 仍然是 R.id.imageView1 并且您的侦听器无法正确处理它。

尝试像这样编辑 onCreateView 方法的 switch 语句:

switch (position) {
case 0:
    imageView.setImageResource(R.drawable.splash);
    imageView.setId(R.drawable.splash);
    return view;
case 1:
    imageView.setImageResource(R.drawable.girrafe);
    imageView.setId(R.drawable.girrafe);
    return view;
case 2:
    imageView.setImageResource(R.drawable.lion);
    imageView.setId(R.drawable.lion);
    return view;
case 3:
    imageView.setImageResource(R.drawable.monkey);
    imageView.setId(R.drawable.monkey);
    return view;
case 4:
    imageView.setImageResource(R.drawable.chicken);
    imageView.setId(R.drawable.chicken);
    return view;
default:
    return null;
}
于 2013-03-27T15:13:17.930 回答