我的每个列表视图项都有自己的linearlayout组件。列表视图位于onItemClick自身的活动中。
在我的自定义适配器文件中,我在此linearlayout上进行了onclick,
private ArrayList<Book> bookArray; // this is the data source
.........
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // fire user Like
//*** how can I get which item is selected here?
但是如何获得点击了linearlayout的项目索引?
此致 锤
答案 0 :(得分:1)
使用setTag()和getTag()记住位置,并在点击LinearLayout时使用它。
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout .setTag(position);
并进入onLlick on LinearLayout将其视为
imgLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer)v.getTag();
//....
}
}
答案 1 :(得分:0)
您可以为线性布局设置标记,并在onClick内部获取标记,该标记将返回clickEvent获取触发的位置。
见下面的代码:
private ArrayList<Book> bookArray; // this is the data source
.........
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setTag(position);
imgLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // fire user Like
//*** how can I get which item is selected here?
Log.d(TAG,"Clicked Pos of Row"+v.getTag().toString();