在Android listview的setOnItemClickListener上设置item点击事件

发布于 2024-11-06 21:20:05 字数 1786 浏览 0 评论 0原文

我使用自定义适配器在列表视图的一行中有三个文本视图,单击该行时,我想执行所选文本视图的单击事件。

下面是我的点击事件的示例代码。在第一次单击时,监听器被设置,只有在第二次单击时才会发生实际的单击事件,我想在第一次单击本身上找到它。是否可以?

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,      int pos, long id) {

                    final Order orderBooking = (Order)adapter.getItemAtPosition(pos);

                    // SKU listener
                    final TextView tvSkuId = (TextView) view.findViewById(orderBooking.getSelectedSkuId());
                    tvSkuId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSkuClickListener(view, orderBooking);
                        }
                    });

                    // SO listener
                    final TextView tvSoId = (TextView) view.findViewById(orderBooking.getSelectedSoId());
                    tvSoId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSoClickListener(view, orderBooking);
                        }
                    });

                    // OR listener
                    final TextView txtOrId = (TextView) view.findViewById(orderBooking.getSelectedOrId());
                    //onOrClickListener(view, orderBooking);
                    txtOrId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onOrClickListener(view, orderBooking);
                        }
                    });
            }
        });

I have three textviews in a row of a listview using custom adapter and on click of the row, i want to perform the click event of the selected text view.

Below is my sample code for the click event. Here on the first click, the listeners are set and only on the second click the actual click event happens, I want to find this on the first click itself. Is it possible?

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,      int pos, long id) {

                    final Order orderBooking = (Order)adapter.getItemAtPosition(pos);

                    // SKU listener
                    final TextView tvSkuId = (TextView) view.findViewById(orderBooking.getSelectedSkuId());
                    tvSkuId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSkuClickListener(view, orderBooking);
                        }
                    });

                    // SO listener
                    final TextView tvSoId = (TextView) view.findViewById(orderBooking.getSelectedSoId());
                    tvSoId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSoClickListener(view, orderBooking);
                        }
                    });

                    // OR listener
                    final TextView txtOrId = (TextView) view.findViewById(orderBooking.getSelectedOrId());
                    //onOrClickListener(view, orderBooking);
                    txtOrId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onOrClickListener(view, orderBooking);
                        }
                    });
            }
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

全部不再 2024-11-13 21:20:05

为什么不在初始化时设置这些 clickListener?当您将数据绑定到视图时,您应该在适配器中进行设置。您的代码是错误的,因为每次用户单击列表行时它都会初始化您的视图。

Why not just set those clickListeners in time of initialization? You should make it in your Adapter, when you bind your data to the views. Your code is wrong, cause it will initialize your views every time the user clicks a list's row.

囍孤女 2024-11-13 21:20:05

在适配器的 getview 方法中执行此操作。为每个文本视图设置一个 onclick 监听器。

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    List<SectionObj> article = (List<SectionObj>) children.get(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.listchildlayout_galleryview, null);
    }       
     Gallery gallery = (Gallery) convertView.findViewById(R.id.customviewgallery);
     gallery.setAdapter(new AddImgAdp(context, article, groupPosition ));
     gallery.setOnItemClickListener(mOnGalleryClick);

    return convertView;
}

private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


    }       
};

以上是画廊视图。做同样的事情,除了对文本视图做同样的事情

Do it in your adapater, in the getview method. Set an onclick listner for every text view.

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    List<SectionObj> article = (List<SectionObj>) children.get(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.listchildlayout_galleryview, null);
    }       
     Gallery gallery = (Gallery) convertView.findViewById(R.id.customviewgallery);
     gallery.setAdapter(new AddImgAdp(context, article, groupPosition ));
     gallery.setOnItemClickListener(mOnGalleryClick);

    return convertView;
}

private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


    }       
};

The above is for a gallery view. Do the same thing except do it for a textview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文