如何知道点击阵列的哪个位置?

时间:2016-11-11 10:42:29

标签: android

我正在开发一个应用程序,其中我有一个包含一些图像URL的String数组,我在指定的时间间隔内加载并在图像视图中设置.problem是我想索引(对于ex.0,1,2, 3,4,5)该元素被用户点击。你能建议我这样做吗?

代码: -

    String[] dealText = {"Install Medlife", "Install Voonik", "Install IndigoRummy", "Install Chai point"};
    String[] arr = new String[]{"http://media.vcommission.com/brand/files/vcm/3012/Medlife_CPS_MLVC2_336X280.jpg",
        "http://media.vcommission.com/brand/files/vcm/3156/Voonik_CPS_Half_Price_Fashion_Sale_728x90.jpg",
        "http://media.vcommission.com/brand/files/vcm/3144/IndigoRummy_CPA_starter_banus_100_250x250.gif",
        "http://media.vcommission.com/brand/files/vcm/3010/ChaiPoint_CPS_Get_20_Off_300X250.jpg"};

    m_ToolTipLayout = (LinearLayout) findViewById(R.id.toolTip_Layout);
    m_ToolTipText = (TextView) findViewById(R.id.tooltip_text);

    //  Initally TooltipLayout will remain Invisible
    m_ToolTipLayout.setVisibility(View.INVISIBLE);
    m_ToolTipText.setVisibility(View.INVISIBLE);

    //Ticker image
    m_TickerDeals = (ImageView) findViewById(R.id.tickerDeals);
    m_oHandler = new Handler();
    Runnable oRunnable = new Runnable() {
        int i = 0;

        @Override
        public void run() {
            Picasso.with(getApplicationContext()).load(arr[i]).into(m_TickerDeals);


            m_ToolTipLayout.setVisibility(View.VISIBLE);
            m_ToolTipText.setVisibility(View.VISIBLE);
            m_ToolTipText.setText(dealText[i]);
            i++;
            if (i > arr.length - 1) {
                i = 0;
            }
            m_TickerDeals.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  Didn't know where to go
                }
            });
            m_oHandler.postDelayed(this, 6000);

        }
    };
    m_oHandler.postDelayed(oRunnable, 6000);

1 个答案:

答案 0 :(得分:0)

m_TickerDeals.setTag(new Integer(i));
m_TickerDeals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index = (Integer)v.getTag();
// Now use your index
}
});

你的setTag用于放置索引并在clicklistener下获取并使用它。