ViewFlipper中具有不同点击侦听器的相同视图

时间:2011-06-05 20:48:43

标签: android

我有一个ViewFlipper,它被多个视图填充,实际上是一遍又一遍的相同视图。一切正常,但将onClickListener设置为按钮的工作方式与预期不符:

flipStack = (ViewFlipper) findViewById(R.id.clubViewFlipper);

for(int i=0; i<= clubDataSet.size()-1; i++) {
clubData = clubDataSet.get(i);

    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = vi.inflate(R.layout.detail_overlay, (ViewGroup)findViewById(R.id.clubDetailScrollView), false);

    Button websiteButton = (Button) view.findViewById(R.id.clubDetailWebsiteButton);
    websiteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(webIntent);
        }
    });

    flipStack.addView(view);
}

ViewFlipper视图的每个网站按钮现在都设置为相同的URL。有没有办法改变这个问题,或者我的方法是否与ViewFlipper错误?

谢谢!

brejoc

2 个答案:

答案 0 :(得分:0)

看起来没问题,除非您永远不会更改网址。如果您想为每个视图设置不同的URL,则需要在循环内的某处更改它。您可以在与您的观看次数相对应的String[]中进行设置,在这种情况下只需使用urls[i]即可。无论如何,那是一种方式。

答案 1 :(得分:0)

您可以使用标记:

flipStack = (ViewFlipper) findViewById(R.id.clubViewFlipper);

for(int i=0; i<= clubDataSet.size()-1; i++) {
clubData = clubDataSet.get(i);

    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = vi.inflate(R.layout.detail_overlay, (ViewGroup)findViewById(R.id.clubDetailScrollView), false);

    Button websiteButton = (Button) view.findViewById(R.id.clubDetailWebsiteButton);

    // set the button's tag to be the url of the club
    websiteButton.setTag(clubData.getUrl());
    websiteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // fetch the URL from the tag.
            Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(v.getTag().toString()));
            startActivity(webIntent);
        }
    });

    flipStack.addView(view);
}