Я заполняю Horizontal ScrollView, используя Picasso
, и проблема в том, что я не могу правильно реализовать onClickListener
, потому что эти представления элементов списка не имеют никакого индекса.реализовать onClickListener для горизонтального ScrollView
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;
}
как связать индекс urls
списка для соответствующего SquaredImageView
элемента в горизонтальной ScrollView? Или, может быть, вы можете предложить лучший подход для onClick
Попробуйте установить onClickListener для макета вместо – scubasteve623