在我的项目中,daimajia AndroidImageSlider 完成了一个图像滑块。当我点击图像时,我想打开另一个活动/页面。我试过并在sliderlayout中插入了一个linearlayout。但是,在此linearlayout中单击侦听器会使sliderlayout卡住。
<com.daimajia.slider.library.SliderLayout
android:id="@+id/advertiseSlider"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.daimajia.slider.library.SliderLayout>
答案 0 :(得分:4)
没有正确测试此代码,但它是这样的:
SliderLayout mySliderLayout = (SliderLayout) findViewById(R.id.slider);
for (int i = 0; i < item.getImages().size(); i++) {
BaseSliderView baseSliderView = new BaseSliderView(context) {
@Override public View getView() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.image_slider, null);
ImageView target = (ImageView) v.findViewById(R.id.daimajia_slider_image);
bindEventAndShow(v, target);
return v;
}
};
baseSliderView.image(item.getImages().get(i));
baseSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override public void onSliderClick(BaseSliderView slider) {
Log.d("MyActivity", "index selected:" + mySliderLayout.getCurrentPosition());
}
});
mySliderLayout.addSlider(baseSliderView);
}
答案 1 :(得分:1)
我知道这是一个非常古老的问题,但我想我会发表评论。
@Override
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.activity_layout);
SliderLayout mySliderLayout = (SliderLayout) findViewById(R.id.slider);
HashMap<String, Integer> image_maps = new HashMap<String, Integer>();
image_maps.put("Title1", R.drawable.slide1);
image_maps.put("Title2", R.drawable.slide2);
image_maps.put("Title3", R.drawable.slide3);
for (String name : image_maps.keySet())
{
TextSliderView textSliderView = new TextSliderView(this);
textSliderView
.description(name)
.image(image_maps.get(name))
.setOnSliderClickListener(this);
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra", name);
sliderShow.addSlider(textSliderView);
}
sliderShow.setPresetTransformer(SliderLayout.Transformer.Accordion);
sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
sliderShow.setCustomAnimation(new DescriptionAnimation());
sliderShow.setDuration(10000);
}
public void onSliderClick(BaseSliderView sliderview)
{
Intent myNewIntent = new Intent(getApplicationContext(), MyNewActivity.class);
startActivity(myNewIntent);
}
我希望它对任何人都有用: - )
答案 2 :(得分:1)
SliderLayout sliderLayout = (SliderLayout) view.findViewById(R.id.advertiseSlider);
final List<String> listUrls = allImageUrlList();
for(int index = 0; index <listUrls.size(); index++){
DefaultSliderView defaultSliderView = new DefaultSliderView(mContext);
final int finalI = index;
defaultSliderView.image(urls.get(index))
.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override
public void onSliderClick(BaseSliderView slider) {
Intent intent = new Intent(this,Image.class);
intent.putExtra("imageurls", urls.get(finalI));
startActivity(intent);
}
});
sliderLayout.addSlider(defaultSliderView);
答案 3 :(得分:0)
这是解决方案。首先,您需要在您的活动中实施BaseSliderView.OnSliderClickListener
。
它将提供以下覆盖方法,
@Override
public void onSliderClick(BaseSliderView slider) {
Toast.makeText(getContext(),slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
}
确保已将onClickListener添加到滑块,如下所示
DefaultSliderView textSliderView = new DefaultSliderView(getContext());
textSliderView
.image("")
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);