快速滚动ListView时,项目消失

时间:2014-08-23 09:39:46

标签: android listview android-custom-view baseadapter

我创建了一个扩展BaseAdapter的适配器。 以下是课程:

class LocalAdapter extends BaseAdapter{

    private Context context;
    public LocalAdapter(Context ctx){
        context=ctx;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view==null){
            LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = li.inflate(R.layout.chat_list_item,viewGroup,false);
        }


        TextView rightView = (TextView)view.findViewById(R.id.rightAligned);
        TextView leftView = (TextView)view.findViewById(R.id.leftAligned);



        if(messages.get(i).getFrom().equals(UserInfo.getMobileNumber())){
            rightView.setText(messages.get(i).getMessage());
            leftView.setVisibility(View.GONE);
        }
        else{
            leftView.setText(messages.get(i).getMessage());
            rightView.setVisibility(View.GONE);
        }

        return view;
    }
}

适配器可以从主类访问ArrayList。它有大约30项。现在,当我加载List时,所有项目都被加载,但是当滚动列表时,一些项目会在滚动时消失。

请提供解决方案。

2 个答案:

答案 0 :(得分:3)

您可能没有调用setVisiblity(View.VISIBLE)只隐藏视图,以这种方式修复:

if (messages.get(i).getFrom().equals(UserInfo.getMobileNumber())){
    rightView.setText(messages.get(i).getMessage());
    leftView.setVisibility(View.GONE);
    rightView.setVisibility(View.VISIBLE);
} else {
    leftView.setText(messages.get(i).getMessage());
    rightView.setVisibility(View.GONE);
    leftView.setVisibility(View.VISIBLE);
}

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

class LocalAdapter extends BaseAdapter {

    private Context context;
    public LocalAdapter(Context ctx){
        context=ctx;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int i) {
        return messages.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.chat_list_item, viewGroup, false);
            viewHolder.rightView = (TextView)view.findViewById(R.id.rightAligned);
            viewHolder.leftView = (TextView)view.findViewById(R.id.leftAligned);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }


        if(messages.get(i).getFrom().equals(UserInfo.getMobileNumber())){
            viewHolder.rightView.setText(messages.get(i).getMessage());
            viewHolder.leftView.setVisibility(View.GONE);
            viewHolder.rightView.setVisibility(View.VISIBLE);
        }else{
            viewHolder.leftView.setText(messages.get(i).getMessage());
            viewHolder.rightView.setVisibility(View.GONE);
            viewHolder.leftView.setVisibility(View.VISIBLE);
        }

        return view;
    }

    class ViewHolder{
        TextView rightView;
        TextView leftView;
    }
}