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

Avi*_*kaj 4 android

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

    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();


        }
    });
Run Code Online (Sandbox Code Playgroud)

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

Har*_*shi 7

您需要在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
}
Run Code Online (Sandbox Code Playgroud)

}

而你的onClick()样子......

@Override
        public void onClick(View v) {

            int position  = (Integer) v.getTag();
            Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();


        }
Run Code Online (Sandbox Code Playgroud)