本文介绍了如何在ImageView上实现onClick()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态填充了ImageViews和Textviews.

I have dynamically populated ImageViews and Textviews.

现在我在ImageView上实现onClick方法时遇到了小问题.在某些ImageView上单击,它应切换为片段,然后单击一些活动.这是我的代码-我的主要片段:

Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.here is my code - my main fragment:

public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
            "string1",
            "string2",
            "string3",
            "string4",
            "string5",
            "string6"
    };
    public static int[] gridViewImages = {
            R.drawable.delivery,
            R.drawable.shipping_logs,
            R.drawable.meassurement,
            R.drawable.takeovers,
            R.drawable.settings,
            R.drawable.download_data
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, parent, false);
 gridView = (GridView) view.findViewById(R.id.grid);
        gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));

        imageView = (ImageView) view.findViewById(R.id.gridview_image);
}

我的自定义gridview适配器:

my custom gridview adapter:

public class CustomAndroidGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private final String[] string;
    private final int[] Imageid;

    public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.string = string;
    }

    @Override
    public int getCount() {
        return string.length;
    }

    @Override
    public Object getItem(int p) {
        return null;
    }

    @Override
    public long getItemId(int p) {
        return 0;
    }

    @Override
    public View getView(final int p, final View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid, null);
            TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
            textView.setText(string[p]);
            imageView.setImageResource(Imageid[p]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

问题:如何在ImageView上实现onClick()并切换到片段和活动?

推荐答案

接口回调:

public interface LaunchFragmentWithImage{
 void launchFragmentWithImage(int imageID);
}

在下面为适配器提供此类的引用,以便它可以调用该方法.不建议给适配器本身提供对类的引用,因为适配器不负责知道谁接收到图像,从而违反了OOD的单一责任"原则,但为了学习就可以了.

Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.

public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{

  @Override //Method from interface we've defined.
  public void launchFragmentWithImage(int imageID){
     FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
     Bundle imageData = new Bundle();
     imageData.putInt(imageID, "key");
     fragmentToLaunch.setArguments(imageData);

   getSupportFragmentManager().beginTransaction() //Call replace not add
                              .replace(containerID, fragmentToLaunch)
                              .commit();

  }

}

在AdapterClass中

In AdapterClass

imageViewVar.setOnClickListener(new View.OnClickListener(){

 @Override
 public void onClick(View view){
 fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
 }


});

这篇关于如何在ImageView上实现onClick()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:12