24

我发现了一个可能是错误的神秘问题!我的片段中有一个列表。每行都有一个按钮。列表不应响应点击,但按钮是可点击的。

为了获得点击了哪个按钮,我创建了一个监听器并在我的片段中实现它。这是我的适配器的代码。

public class AddFriendsAdapter extends BaseAdapter {

    public interface OnAddFriendsListener {
        public void OnAddUserClicked(MutualFriends user);
    }

    private final String TAG = "*** AddFriendsAdapter ***";

    private Context context;
    private OnAddFriendsListener listener;
    private LayoutInflater myInflater;
    private ImageDownloader imageDownloader;
    private List<MutualFriends> userList;

    public AddFriendsAdapter(Context context) {
        this.context = context;
        myInflater = LayoutInflater.from(context);

        imageDownloader = ImageDownloader.getInstance(context);
    }

    public void setData(List<MutualFriends> userList) {
        this.userList = userList;

        Log.i(TAG, "List passed to the adapter.");
    }

    @Override
    public int getCount() {
        try {
            return userList.size();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
            holder = new ViewHolder();

            Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
            holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
            holder.tvUserName.setTypeface(font);
            holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
            holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
            holder.btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG, "Item: " + position);
                    listener.OnAddUserClicked(userList.get(position));
                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvUserName.setText(userList.get(position).getName());
        imageDownloader.displayImage(holder.ivPicture, userList.get(position).getPhotoUrl());

        return convertView;
    }

    public void setOnAddClickedListener(OnAddFriendsListener listener) {
        this.listener = listener;
    }

    static class ViewHolder {
        TextView tvUserName;
        ImageView ivPicture;
        Button btnAdd;
    }
}

当我运行应用程序时,我可以看到我的行但是因为我的列表很长并且有超过 200 个项目,当我转到列表中间并单击一个项目然后返回的位置是错误的(它类似于 7,有时是 4 等等)。

现在有什么奥秘?如果我从我的片段中激活列表的项目侦听器并单击行,那么如果我单击按钮,则会在该行上显示正确的行位置,然后将显示错误的位置。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG, "item " + position + " clicked.");
            }
        });

结果在logcat中:

05-09 10:22:25.228: E/AddFriendsFragment(20296): item 109 clicked.
05-09 10:22:34.453: E/*** AddFriendsAdapter ***(20296): Item: 0

任何建议将不胜感激。谢谢

4

3 回答 3

50

因为convertViewand 持有人将被回收使用,所以将你setOnClickListener的 if else 语句移出:

    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
        holder = new ViewHolder();

        Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
        holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
        holder.tvUserName.setTypeface(font);
        holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
        holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.btnAdd.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) 
                Log.e(TAG, "Item: " + position);
                listener.OnAddUserClicked(userList.get(position));
            }
        });

这不是最好的解决方案,因为会有一些性能问题。我建议您为您的视图创建一个地图并为您的项目创建一个新视图,然后为每个视图使用相对视图。

我认为这将是具有最佳性能的更好解决方案:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
        holder = new ViewHolder();

        Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
        holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
        holder.tvUserName.setTypeface(font);
        holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
        holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer pos = (Integer)v.getTag();
                Log.e(TAG, "Item: " + pos);
                listener.OnAddUserClicked(userList.get(pos));
            }
        });

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvUserName.setText(userList.get(position).getName());
    imageDownloader.displayImage(holder.ivPicture, userList.get(position).getPhotoUrl());
    holder.btnAdd.setTag(position);
    return convertView;
}

您也可以自己管理视图。为您的项目创建每个独特的视图,不要回收视图。

//member various
private Map<Integer, View> myViews = new HashMap<Integer, View>(); 

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view = myViews.get(position);
    if (view == null) {
        view = myInflater.inflate(R.layout.list_add_friends_row, null);
        //don't need use the holder anymore.

        Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/ITCAvantGardeStd-Demi.ttf");
        holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
        holder.tvUserName.setTypeface(font);
        holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
        holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer pos = (Integer)v.getTag();
                Log.e(TAG, "Item: " + pos);
                listener.OnAddUserClicked(userList.get(pos));
            }
        });

       holder.tvUserName.setText(userList.get(position).getName());
       imageDownloader.displayImage(holder.ivPicture,  
                userList.get(position).getPhotoUrl());
       myViews.put(position, view);

    }
    return view;
}
于 2013-05-09T02:37:01.240 回答
2

您是否尝试过这样做:

holder.btnAdd.setTag(Integer.valueOf(position));

然后在按钮的回调中单击检索到哪一行,如下所示:

public void btnAddClickListener(View view)
    {
        position = (Integer)view.getTag();
        Foo foo = (Foo)foos_adapter.getItem(position);  //get data of row(position)
        //do some
    }
于 2013-05-09T02:46:43.357 回答
2

我发现另一种有用的方法(当然,如果您使用 ViewHolder 模式)是在调用 getView() 时在单独的属性上设置索引,然后在 onClickListener 中,您只需引用持有人的 position 属性,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if(convertView == null){

        convertView = View.inflate(mContext, R.layout.contact_picker_row,null);

        holder = new ViewHolder();

        holder.body = (RelativeLayout)convertView.findViewById(R.id.numberBody);

        convertView.setTag(holder);

    }else{

        holder = (ViewHolder)convertView.getTag();

    }

    holder.position = position;

    holder.body.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(mContext,"Clicked on: "+holder.position,Toast.LENGTH_LONG).show();

        }
    });

    return convertView;
}

private class ViewHolder{

    RelativeLayout body;
    int position;

}
于 2015-08-24T09:38:47.707 回答