我根据列表的大小动态扩充布局。现在每当我点击动态视图时,我必须获得点击子视图的位置。我正在尝试这段代码,但运气不好......
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();
}
});
我无法知道我在哪里做错了。
答案 0 :(得分: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
}
}
你的onClick()
看起来像..
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:2)
您需要为每个膨胀的视图设置“OnClickListener”。
OnClickListener onClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();//layoutViewDealsList.indexOfChild(layoutView);
Toast.makeText(profileActivity, ""+position, Toast.LENGTH_SHORT).show();
}
};
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);
appDelegate.setDealsList(dealsList);
layoutView.setTag(position);
layoutView.setOnClickListener(onClickListener);
}
}
另请注意,位置设置为每个布局视图而不是父级。
答案 2 :(得分:1)
在设置标记后,只需将点击侦听器放入视图中 你完成的所有事情@Avinash kumar Pankaj是对的。
答案 3 :(得分:0)
如果有更好的解决方案:
首先设置标记 -
int position = layout.indexOfChild(view);
layout.setTag(位置);
点击时间:
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
fileDataList.remove(position);
((LinearLayout) addView.getParent()).removeView(addView);
}
});
答案 4 :(得分:-1)
CharSequence数据=((TextView)v).getText();
字符串结果= data.toString()