问题描述
我已经实现HorizontalScrollView显示图像。但我不能找到方法来实现对HorizontalScrollView特定图像点击的onclick监听器。
我对的ImageView 1的onclick实施还我的ImageView叫期间SETID但敬酒的消息只显示14。
有人帮我。
更新
山姆他指出错误谢谢;
现在我想显示每一个图片下方。
所以我想实现相对布局及以下code,但它不工作。
下面是我的code
公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
//充气的布局该片段
返回inflater.inflate(R.layout.cfragment,集装箱,FALSE);
}
@覆盖
公共无效调用onStart(){
super.onStart();
/ **设置多选选模式为ListView * /
initfrag();
}
私人无效initfrag(){
RelativeLayout的布局=新的RelativeLayout(getActivity());
TextView的TV1 =新的TextView(getActivity());
tv1.setText(xyzxyzyxyzxyz); tv1.setId(1);
TextView中TV2 =新的TextView(getActivity());
tv2.setText(xyzxyzyxyzxyz); tv2.setId(2);
RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW,tv1.getId());
layout.addView(TV1);
layout.addView(TV2,LP);
最终的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;我++){
IV =新ImageView的(getActivity());
iv.setPadding(20,5,5,5);
iv.setId(ⅰ);
iv.setImageResource(R.drawable.test_play_image);
linearlayout1.addView(ⅳ,200,220);
ImageView的IV2 =新ImageView的(getActivity());
iv2.setPadding(20,5,5,5);
iv2.setImageResource(R.drawable.test_play_image);
linearlayout2.addView(IV2,200,220); ImageView的IV3 =新ImageView的(getActivity());
iv3.setPadding(20,5,5,5);
iv3.setImageResource(R.drawable.test_play_image);
linearlayout3.addView(IV3,200,220);
iv.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
INT S = iv.getId();
吐司T = Toast.makeText(getActivity(),ID为+ S,Toast.LENGTH_SHORT);
t.show();
}
});
您想获得您刚才点击,而不是在循环最后创建的视图的视图的ID。所以,简单地在你的OnClickListener变量从 IV更改
:
int类型= iv.getId();
要 v
,查看传递给的onClick()
:
int类型= v.getId();
I have implemented HorizontalScrollView to show image. but i cant find way to implement onclick listener for particular image click on HorizontalScrollView.
i have implemented onclick on imageview 1 also i setid during imageview called but on toast message it only shows "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. So simply change the variable in your OnClickListener from iv
:
int s =iv.getId();
To v
, the View passed to onClick()
:
int s = v.getId();
这篇关于如何实现在点击侦听horizontalscrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!