android– 单击后获取动态创建的子视图的位置

android– 单击后获取动态创建的子视图的位置,第1张

概述我根据列表的大小动态扩充布局.现在每当我点击动态视图时,我必须获得点击子视图的位置.我正在尝试这段代码,但运气不好……LinearLayoutlayoutViewDealsList=(LinearLayout)view.findViewById(R.id.baseLinearLayoutSiteHistory);txtViewNoDeals=(TextView)view.

我根据列表的大小动态扩充布局.现在每当我点击动态视图时,我必须获得点击子视图的位置.我正在尝试这段代码,但运气不好……

    linearLayout layoutVIEwDealsList = (linearLayout) vIEw.findVIEwByID(R.ID.baselinearLayoutSiteHistory);    txtVIEwNoDeals = (TextVIEw) vIEw.findVIEwByID(R.ID.txtVIEwNoDeals);    if(dealsList != null && dealsList.size() > 0) {        for(int index = 0; index < dealsList.size(); index++) {            txtVIEwNoDeals.setVisibility(VIEw.GONE);            LayoutInflater inflater = (LayoutInflater) profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            layoutVIEw = inflater.inflate(R.layout.deal_row, null);            txtVIEwDealsPrice = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealPrice);              txtVIEwDealsTitle = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealTitle);            txtVIEwDealsDescription = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealDescription);            txtVIEwDealsBuyer = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealBuyer);            txtVIEwDealsDate = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealDate);            txtVIEwDealsPrice.setText("Price: $" + dealsList.get(index).getDeal_amount());            txtVIEwDealsTitle.setText(dealsList.get(index).getTitle());            txtVIEwDealsDescription.setText(dealsList.get(index).getDesc());            layoutVIEwDealsList.addVIEw(layoutVIEw);            int position = layoutVIEwDealsList.indexOfChild(layoutVIEw);            layoutVIEwDealsList.setTag(position);            appDelegate.setDealsList(dealsList);        }    }     layoutVIEwDealsList.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            int position  = (Integer) v.getTag();//layoutVIEwDealsList.indexOfChild(layoutVIEw);            Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();        }    });

我无法知道我在哪里做错了.

解决方法:

您需要在layoutVIEw内部循环中应用onClickListener.

linearLayout layoutVIEwDealsList = (linearLayout) vIEw.findVIEwByID(R.ID.baselinearLayoutSiteHistory);txtVIEwNoDeals = (TextVIEw) vIEw.findVIEwByID(R.ID.txtVIEwNoDeals);if(dealsList != null && dealsList.size() > 0) {    for(int index = 0; index < dealsList.size(); index++) {        txtVIEwNoDeals.setVisibility(VIEw.GONE);        LayoutInflater inflater = (LayoutInflater) profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        layoutVIEw = inflater.inflate(R.layout.deal_row, null);    txtVIEwDealsPrice = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealPrice);      txtVIEwDealsTitle = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealTitle);    txtVIEwDealsDescription = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealDescription);    txtVIEwDealsBuyer = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealBuyer);    txtVIEwDealsDate = (TextVIEw) layoutVIEw.findVIEwByID(R.ID.txtVIEwDealDate);    txtVIEwDealsPrice.setText("Price: $" + dealsList.get(index).getDeal_amount());    txtVIEwDealsTitle.setText(dealsList.get(index).getTitle());    txtVIEwDealsDescription.setText(dealsList.get(index).getDesc());    layoutVIEwDealsList.addVIEw(layoutVIEw);    int position = layoutVIEwDealsList.indexOfChild(layoutVIEw);    layoutVIEwDealsList.setTag(position);    appDelegate.setDealsList(dealsList);layoutVIEw.setonClickListener(this); <--------------- setonClickListener}

}

你的onClick()看起来像……

@OverrIDe        public voID onClick(VIEw v) {            int position  = (Integer) v.getTag();            Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();        }
总结

以上是内存溢出为你收集整理的android – 单击获取动态创建的子视图的位置全部内容,希望文章能够帮你解决android – 单击后获取动态创建的子视图的位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115612.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存