gpt4 book ai didi

android - 如何从android中的horizo​​ntalscrollview将图像设置到viewpager中

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:26 24 4
gpt4 key购买 nike

我正在尝试将图像从 horizo​​ntalscrollview 设置到 viewpager。

我无法获取要在 setcurrentItem 方法中使用的 horizo​​ntalscrollview 的 ID。

我试过很多东西,但我做不到。

这是 MainActivity 类

public class MainActivity extends AppCompatActivity {

int toplamSayfa = 5;
private View btnNext, btnPrev;
HorizontalScrollView hs;
ImageView imageView = null;

private Integer images[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};

ViewPager viewPager;

LinearLayout myGallery;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

CustomPagerAdapter adapter = new CustomPagerAdapter(this, toplamSayfa);
myGallery = (LinearLayout) findViewById(R.id.container);
hs = (HorizontalScrollView) findViewById(R.id.scrollview);
viewPager = (ViewPager) findViewById(R.id.view_pager);
btnNext = findViewById(R.id.next);
btnPrev = findViewById(R.id.prev);

viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);

btnPrev.setOnClickListener(onClickListener(0));
btnNext.setOnClickListener(onClickListener(1));

addImagesToThegallery();
}

private void addImagesToThegallery() {

final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);

for ( Integer image : images) {

imageGallery.addView(getImageView(image));


}

}

private View getImageView(final Integer image) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//the method where i am stuck

viewPager.setCurrentItem();

}
});



imageView.setImageResource(image);

return imageView;
}


private View.OnClickListener onClickListener(final int i) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i > 0) {

if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
} else {

if (viewPager.getCurrentItem() > 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
}
};
}
}

最佳答案

更新 addImagesToThegallery 方法以在将图像从 images 数组添加到图库时保留图像的索引:

private void addImagesToThegallery() {
final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);

int i = 0;
for ( Integer image : images) {
imageGallery.addView(getImageView(image,i));
i += 1;
}
}

并在此处使用索引:

private View getImageView(final Integer image, int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//use the index here

viewPager.setCurrentItem(index);

}
});



imageView.setImageResource(image);

return imageView;
}

关于android - 如何从android中的horizo​​ntalscrollview将图像设置到viewpager中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com