Horizo​​ntalListView不滚动,显示第一个项目

时间:2014-06-12 05:58:29

标签: android listview scrollview horizontalscrollview

我在HorizontalListView中有ScrollView(MeetMe库),但ListView不滚动。第一项显示正常,但只是不会滚动。

我认为我的适配器可能有问题吗?

最初我认为这是因为ScrollView中的ListView,但我注释掉了ScrollView代码,但它仍然没有工作。

我还尝试使用此代码截取MotionEvent,但这也无效。

avatarList.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

BaseAdapter

public class AvatarAdapter extends BaseAdapter {

private String[] avatars = { "avatar1", 
                             "avatar2", 
                             "avatar3", 
                             "avatar4", 
                             "avatar5" };

private Context context;

public AvatarAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return avatars.length;
}

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

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

private static class ViewHolder {
    private ImageView avatar;
}

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

    if (convertView == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.fragment_pick_avatar, parent, false);

        holder = new ViewHolder();
        holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);

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

    holder.avatar.setImageDrawable(context.getResources().getDrawable(GlobalMethods.getAvatarDrawableId(context, avatars[position])));

    return convertView;
}

}

1 个答案:

答案 0 :(得分:3)

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

这一行应该是这样的:

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

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

到此:

@Override
public Object getItem(int position) {
return avatars[position];
}