我可以在片段中使com.daimajia.slider.library.SliderLayout可单击

时间:2016-04-20 07:54:11

标签: android onclicklistener

我想让daimajia slidetlayout可点击。我在SliderLayout上设置了OnClickListener,但它不起作用。我还在xml中设置了clickable true,但仍然不起作用。

这里是我的代码: -

<RelativeLayout
    android:id="@+id/relative_slider"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true"
    android:clickable="true"
    android:duplicateParentState="true"
    android:focusable="true">

    <com.daimajia.slider.library.SliderLayout
         android:id="@+id/slider"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         custom:indicator_visibility="visible" />

</RelativeLayout>

我的片段: -

mDemoSlider = (SliderLayout)rootView.findViewById(R.id.slider);
mDemoSlider.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getContext(),"click",Toast.LENGTH_LONG).show();
        int pos = mDemoSlider.getCurrentPosition();
        int id = image_link.get(pos);
        if(id!=0){
            Intent intent = new Intent(getContext(),ProductsCategory.class);
            intent.putExtra("category",id+"");
            startActivity(intent);
        }
    }
});

2 个答案:

答案 0 :(得分:3)

com.daimajia.slider.library已经有内置事件。 这仅在添加滑块名称时可用。您应该为每个滑块添加名称。

HashMap<String,String> url_maps = new HashMap<String, String>();
        url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
        url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
        url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

        for (String name : url_maps.keySet()) {
            TextSliderView textSliderView = new TextSliderView(getContext());
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(url_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);

            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra", name);

            mDemoSlider.addSlider(textSliderView);
        }

在文本滑块视图

textSliderView.setOnSliderClickListener(this);

并实现BaseSliderView.OnSliderClickListener。然后我们可以覆盖下面的代码

 @Override
    public void onSliderClick(BaseSliderView slider) {
//do something
    }

答案 1 :(得分:1)

只是为了补充答案。值得注意的是,在方法上:

@Override
    public void onSliderClick(BaseSliderView slider) {
       //do something
 }

此方法调用接收的BaseSliderView可能不是您刚刚单击的当前滑块。您必须显式调用getCurrentSlider()方法来获取先前附加的包。例如:

@Override
    public void onSliderClick(BaseSliderView slider) {
         BaseSliderView clickedSlide  = sliderLayout.getCurrentSlider();
         //get bundles attached  and or do something else
 }