在scrollView中确定单击的对象?

时间:2013-11-23 05:49:22

标签: android scrollview

我填充了一个垂直滚动视图,其中包含一个可能很长的垂直线性布局,其中包含设备上的“配置文件”,如下所示:

for (Profile profile : Profiles) {
    LayoutInflater inflater = getLayoutInflater();
    View profileCard = inflater.inflate(R.layout.selector, null);

    ...

    profileList.addView(profileCard);
}

selector布局中,我提供了一个onClick方法:因此,如果单击其中一个配置文件,则会运行onClick方法。但是,我想特别选择那个特定的配置文件:如何返回在scrollView中单击了哪个元素的值?

1 个答案:

答案 0 :(得分:1)

这样做

for (final Profile profile : Profiles) {
            LayoutInflater inflater = getLayoutInflater();
            View profileCard = inflater.inflate(R.layout.selector, null);

            /*
             *your code  
             */


            profileCard.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Profile clickdProfile = profile;

                }
            });

            profileList.addView(profileCard);
        }

注意:不要在布局文件中提供onclick。