我将视图放大,设置ID,设置一些标记,在其上设置onClickListener并将其添加到父视图。view.getId在onClick监听器中为-1
LinearLayout llCategoryListIncome = (LinearLayout)calculatorFlipContainerBack.findViewById(R.id.ll_category_list_income);
// inflate category item
LinearLayout categoryItem = (LinearLayout)inflater.inflate(
R.layout.fragment_category_item,
container,
false);
Integer id = 2;
Integer position = 3;
categoryItem.setId(position);
categoryItem.setTag(R.string.tag_category_position, position);
categoryItem.setTag(R.string.tag_category_id, id);
categoryItem.setOnClickListener(this);
llCategoryListIncome.addView(categoryItem);
在onClick监听器中我想检查这个ID。但ID始终是-1。与标签相同的问题。标签始终为空。
public void onClick(View v) {
Integer position = (Integer)view.getTag(R.string.tag_category_position);
Integer id = (Integer)view.getTag(R.string.tag_category_id);
Log.d(TAG, "view get id " + view.getId()); // is null
Log.d(TAG, "position is " + position); // is null
Log.d(TAG, "id is " + id); // is -1
任何想法什么可能是错的?
您确定您点击的视图实际上是您的线性布局,而不是其中的其他小部件吗? –
是的,我确定。我删除了setOnClickListener调用,并检查onClick是否被调用 - 并且它不被调用。 –
这是一个错字吗? 'view'从哪里来? – codeMagic