简体   繁体   English

如何实现用于水平滚动视图的单击侦听器

[英]How to implement on click listener for horizontalscrollview

I have implemented HorizontalScrollView to show image. 我已经实现了Horizo​​ntalScrollView以显示图像。 but i cant find way to implement onclick listener for particular image click on HorizontalScrollView. 但我找不到为特定图像实现onclick侦听器的方法,请单击Horizo​​ntalScrollView。

i have implemented onclick on imageview 1 also i setid during imageview called but on toast message it only shows "14".. 我在imageview 1上实现了onclick,在imageview期间我也调用了setid,但是在吐司消息上它仅显示“ 14”。

anybody please help me 有人请帮助我

Updated 更新

Thanks to sam he point out error; 多亏了山姆,他指出了错误。 Now i want to display text below every image. 现在我想在每个图像下方显示文本。 so i am trying to implement relative layout and following code but it does not working. 所以我试图实现相对布局和下面的代码,但它不起作用。

Here is my code 这是我的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.cfragment, container, false);
    }
@Override
public void onStart() {
    super.onStart();
    /** Setting the multiselect choice mode for the listview */
    initfrag();
}
private void initfrag() {
    RelativeLayout layout = new RelativeLayout(getActivity());
     TextView tv1 = new TextView(getActivity());
     tv1.setText("xyzxyzyxyzxyz"); tv1.setId(1);
     TextView tv2 = new TextView(getActivity());
     tv2.setText("xyzxyzyxyzxyz");tv2.setId(2);
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
             RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
     lp.addRule(RelativeLayout.BELOW, tv1.getId());
     layout.addView(tv1);        
     layout.addView(tv2, lp);
    final LinearLayout linearlayout1 = (LinearLayout)getView().findViewById(R.id.container);
    LinearLayout linearlayout2 = (LinearLayout)getView().findViewById(R.id.container2);
    LinearLayout linearlayout3 = (LinearLayout)getView().findViewById(R.id.container3);
    for (int i = 0; i < 15; i++) { 
        iv = new ImageView(getActivity());
        iv.setPadding(20, 5, 5, 5);
        iv.setId(i);
         iv.setImageResource(R.drawable.test_play_image);
         linearlayout1.addView(iv, 200, 220);
         ImageView iv2 = new ImageView(getActivity());
         iv2.setPadding(20, 5, 5, 5);
         iv2.setImageResource(R.drawable.test_play_image);
         linearlayout2.addView(iv2, 200, 220);

         ImageView iv3 = new ImageView(getActivity());
            iv3.setPadding(20, 5, 5, 5);
             iv3.setImageResource(R.drawable.test_play_image);
             linearlayout3.addView(iv3, 200, 220);
             iv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int s =iv.getId();
                    Toast t = Toast.makeText(getActivity(), "id is "+s, Toast.LENGTH_SHORT);
                    t.show();
                }
            });

You want to get the id of the View that you just clicked, not the last View created in your loop. 您想获取刚刚单击的视图的ID,而不是循环中创建的最后一个视图的ID。 So simply change the variable in your OnClickListener from iv : 因此,只需从iv更改OnClickListener中的变量:

int s =iv.getId();

To v , the View passed to onClick() : 对于v ,将视图传递给onClick()

int s = v.getId();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM