消息应用程序的ListView在滚动后显示错误的listItem布局

sta*_*cks 5 java android listview android-listview

我知道在stackoverflow上发布了很多类似的问题,所以请不要认为我没有搜索到高低.我认为我的问题只是从现在完全理解listViews和列表项的生命周期.我有一个列表视图,可以包含两种类型的消息,出站或入站.最初,我的listView将使用不同的背景颜色,具体取决于消息的类型(出站与入站),并且它可以完美地工作.现在我的应用程序不需要列表项的不同背景,但它实际上需要不同的列表项的不同布局.

这是我的适配器的剪辑.

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

        View v = convertView;

        SoapBoxMessage thisMessage = messages.get(position);

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (thisMessage.isOutbound()) {
                v = inflater.inflate(R.layout.outbound_row, null);

            } else {
                v = inflater.inflate(R.layout.inbound_row, null);

            }
        }
Run Code Online (Sandbox Code Playgroud)

tyc*_*czj 2

问题是 listview 正在回收视图,因此当您检查视图是否为 null 时,它不会通过,因为视图在回收时不为 null

每次调用 getView 时都需要膨胀视图,基本上删除if(v == null)