如何在ImageView上实现onClick()方法

时间:2016-08-02 00:49:07

标签: android android-fragments

我已动态填充ImageViews和Textviews。

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

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适配器:

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()并切换到片段和活动?

4 个答案:

答案 0 :(得分:3)

public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
        this.context = context;
        this.wrapperOutletList = wrapperOutletList;
        this.listener = listener;      //---> pass in listener....

    }

    @Override
    public int getCount() {
        return wrapperOutletList.size();
    }

    @Override
    public Object getItem(int position) {
        return wrapperOutletList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
            viewHolder = new ViewHolder(convertView);
            viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (!wrapperOutletList.isEmpty()) {
            WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
            Outlet outlet = wrapperOutlet.getOutlet();
            if (outlet != null) {
                if (TextUtils.isEmpty(outlet.getTel())){
                    viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
                    viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
                       viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
                } else {
                    viewHolder.ivCall.setImageResource(R.drawable.icon_call);
                    viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
                    viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);    
                }


            }
        }

        return convertView;
    }

传入听众。举个例子。下面的代码是您在片段中调用适配器的位置。我的片段实现了View.OnClickListener

lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));

答案 1 :(得分:1)

接口回调:

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

为适配器提供下面此类的引用,以便它可以调用该方法。不建议给适配器本身一个类的参考,因为它不是适配器负责知道谁收到图像,因此违反了OOD的单一责任原则,但为了学习它没关系。

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中

imageViewVar.setOnClickListener(new View.OnClickListener(){

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


});

答案 2 :(得分:0)

grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);

为什么要将网格变量指向两个单独的View对象?

答案 3 :(得分:0)

if (convertView == null) {

        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]);
        imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(choosedPosition){
                case 0:do something;break;
                case 1:do something;break;
                ...
            }
        }
        });
        }

这只是一个例子,无法运行。