Android 为水平滚动视图实现onClickListener
我使用Android 为水平滚动视图实现onClickListener,android,android-imageview,horizontalscrollview,onitemclicklistener,picasso,Android,Android Imageview,Horizontalscrollview,Onitemclicklistener,Picasso,我使用Picasso填充水平滚动视图,问题是我无法正确实现onClickListener,因为列表的项目视图没有任何索引 xml: <HorizontalScrollView android:layout_width="match_parent" android:layout_height="120dp" android:id="@+id/myGallery"> <LinearLayout android:id="@+id/cha
Picasso
填充水平滚动视图,问题是我无法正确实现onClickListener
,因为列表的项目视图没有任何索引
xml:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/myGallery">
<LinearLayout
android:id="@+id/channelsScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
private void populateImages() {
final List<String> urls = new ArrayList<>();
//getting list of urls
for (int i = 0; i < ActivityLoading.dataChannelsArrayList.get(0).getChannelArrayList().size(); i++) {
urls.add(getString(R.string.get_channels_img_host) + ActivityLoading.dataChannelsArrayList.get(0).getChannelArrayList().get(i).getId());
}
//add views to horizontal scrollView by the amount of urls
myGallery = (LinearLayout) findViewById(R.id.channelsScrollView);
for (int i = 0; i < urls.size(); i++) {
myGallery.addView(insertPhoto(urls.get(i), i));
}
}
public View insertPhoto(String path, int position) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setGravity(Gravity.CENTER);
final SquaredImageView squaredImageView = new SquaredImageView(getApplicationContext(), position);
squaredImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(this).load(path).placeholder(R.drawable.loading_small).into(squaredImageView);
//I need to receive somehow the position of view pressed
squaredImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this crashes here
Toast.makeText(getApplicationContext(), squaredImageView.id, Toast.LENGTH_SHORT).show();
}
});
layout.addView(squaredImageView);
return layout;
}
和代码:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/myGallery">
<LinearLayout
android:id="@+id/channelsScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
private void populateImages() {
final List<String> urls = new ArrayList<>();
//getting list of urls
for (int i = 0; i < ActivityLoading.dataChannelsArrayList.get(0).getChannelArrayList().size(); i++) {
urls.add(getString(R.string.get_channels_img_host) + ActivityLoading.dataChannelsArrayList.get(0).getChannelArrayList().get(i).getId());
}
//add views to horizontal scrollView by the amount of urls
myGallery = (LinearLayout) findViewById(R.id.channelsScrollView);
for (int i = 0; i < urls.size(); i++) {
myGallery.addView(insertPhoto(urls.get(i), i));
}
}
public View insertPhoto(String path, int position) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setGravity(Gravity.CENTER);
final SquaredImageView squaredImageView = new SquaredImageView(getApplicationContext(), position);
squaredImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(this).load(path).placeholder(R.drawable.loading_small).into(squaredImageView);
//I need to receive somehow the position of view pressed
squaredImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this crashes here
Toast.makeText(getApplicationContext(), squaredImageView.id, Toast.LENGTH_SHORT).show();
}
});
layout.addView(squaredImageView);
return layout;
}
private void populateImages(){
最终列表URL=新的ArrayList();
//获取URL列表
对于(int i=0;i
如何将url
列表的索引绑定到水平滚动视图中相应的SquaredImageView
项?或者,您可以为选项1提供更好的方法:
将“位置”参数更改为“最终”:
public View insertPhoto(字符串路径,最终整数位置){
然后在onClickListener中:
您可以访问“位置”参数。
Toast.makeText(getApplicationContext(),“POSITION:+POSITION,Toast.LENGTH\u SHORT).show();
备选案文2:
设置squaredImageView的标记:
squaredImageView.setTag(位置);
然后在onClickListener中,通过调用以下命令获取该标记:
int location=(int)v.getTag();
尝试为布局设置onClickListener