Android - Listview仅在滚动后才有效

时间:2014-11-24 18:00:35

标签: android android-listview onitemclicklistener

我将onItemClickListener添加到自定义列表视图中。但是这个onItemClickListener只有在我滚动listview之后才能工作。

    PostListAdapter adapter = new PostListAdapter(getActivity(), bps);
    listView = (ListView) v.findViewById(R.id.board_listview);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             Toast.makeText(getActivity(),"List Item Clicked:" + position, Toast.LENGTH_LONG).show();
        }
    });

自定义行的xml仅包含视图,textView和imageView。 我做错了什么?

编辑: PostListAdapter:

Context context;
BoardPostStruct bps[];

public PostListAdapter(Context context, BoardPostStruct[] bps){
    this.context=context;
    this.bps=bps;
}
    @Override
public int getCount() {
    // TODO Auto-generated method stub
    return bps.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return bps[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    PostHolder holder = null;


    if(convertView==null){
        LayoutInflater inflater = ((FragmentActivity)context).getLayoutInflater();
        row = inflater.inflate(R.layout.club_board_post, parent, false);


        holder = new PostHolder();
        holder.userImage = (ImageView)row.findViewById(R.id.club_board_post_user_image);
        holder.userName = (TextView)row.findViewById(R.id.club_board_post_userName);
        holder.postTime= (TextView)row.findViewById(R.id.club_board_post_postTime);
        holder.postContent= (TextView)row.findViewById(R.id.club_board_post_textpost);
        holder.commentCount=(TextView)row.findViewById(R.id.club_board_post_comment_count);
        row.setTag(holder);
    }else{
        holder = (PostHolder)row.getTag();
    }

    BoardPostStruct data = bps[position];
    holder.userImage = data.userImage;
    holder.userName.setText(data.userName);

    holder.postContent.setText(data.postContent);
    holder.postTime.setText(data.postTime);
    if(!data.commentCount.isEmpty()){
        holder.commentCount.setText(data.commentCount);
        holder.commentCount.setVisibility(View.VISIBLE);
    }else{
        holder.commentCount.setVisibility(View.GONE);
    }


    return row;
}

static class PostHolder{
    ImageView userImage;
    //TextView userName;
    TextView postTime;
    TextView postContent;
    TextView commentCount;
    TextView userName;
}

1 个答案:

答案 0 :(得分:0)

我看到了与getLayoutInflater相关的一些问题...所以在 getView 方法中,我建议您更改LayoutInflater构造:

//(...)
if(convertView==null){
        LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(R.layout.club_board_post, parent, false);
//(...)
希望能提供帮助。