我想要獲取圖像視圖在圖像點擊偵聽器上的位置,它是在水平滾動視圖中使用for loop來動態添加的。並且該位置想要在另一個活動中設置View pager current item 。Android獲取視圖在水平滾動視圖中的位置
我想這個方法:
int numpos = imageView.getId();
Log.e("numpos "," = "+ numpos);
每個視圖位置正在逐漸-1。
在先進的感謝
這裏是我的代碼
private void prepareTextViewsForHistory(){
HorizontalScrollView parent=(HorizontalScrollView)findViewById(R.id.horizontal_scroll_view_id_01);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
parent.addView(layout);
for (int i=DEFAULT_HISTORY_MAX - 1; i >= 0; i--)
{
final ImageView imageView=new ImageView (this);
imageView.setTag(i);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.e("imageView ", " Click ");
int numpos = imageView.getId();
Log.e("numpos "," = "+ numpos);
String item_ActivityId = all_Post.getStrActivityId();
Toast.makeText(getBaseContext(), String.valueOf(v.getId()), Toast.LENGTH_SHORT).show();
Log.e(" Image ID = "," = "+String.valueOf(v.getId()));
Intent intent = new Intent(getContext(),SimpleImageActivity.class);
//intent.putExtras(extras);
intent.putExtra("activity_id",item_ActivityId);
startActivity(intent);
}
});
layout.addView(textView);
}
}
使用'v.getTag()'而不是'v.getId()'。 –