2014-03-03 140 views
4

我知道有很多類似的問題,但我找不到我犯的錯誤,如果有人可以幫助。 我試圖想聊天格式,但數組列表項的數組列表中顯示的值上滾動查看持有人和自定義列表查看項目滾動時滾動

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

      final ViewHolder holder; 
      if (convertView == null) { 

       convertView = LayoutInflater.from(getActivity()).inflate(
         R.layout.chat_row, parent, false); 
       holder = new ViewHolder(); 
    holder.messageLeft = (TextView) convertView 
        .findViewById(R.id.leftBox); 
      holder.messageRight = (TextView) convertView 
        .findViewById(R.id.rightBox); 
     convertView.setTag(holder); 

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

      try { if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) { 
       holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
       holder.messageLeft.setVisibility(View.VISIBLE); 
       holder.messageRight.setVisibility(View.GONE); 
      } else { 
       holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
       holder.messageRight.setVisibility(View.VISIBLE); 
       holder.messageLeft.setVisibility(View.GONE); 
      } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return convertView; 

     } 

這裏越來越錯位的是視圖holder類

private static class ViewHolder { 
     public TextView messageLeft; 
     public TextView messageRight; 
    } 
+0

高效和正確的方法,我甲肝編輯現在工作的菲英島問題....感謝所有烏拉圭回合支持 –

回答

4

只需在您的if-else聲明holder.messageRight.setVisibility(View.GONE);holder.messageLeft.setVisibility(View.GONE);

try { 
     if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) { 
      holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
      holder.messageLeft.setVisibility(View.VISIBLE); 
      holder.messageRight.setVisibility(View.GONE); 
     } else { 
      holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
      holder.messageRight.setVisibility(View.VISIBLE); 
      holder.messageLeft.setVisibility(View.GONE); 
     } 

讓我知道它是否適用於您。

+0

謝謝你現在完美...... –

2

您pefroming findViewById每次.. ViewHolder的目的不是像that..Change你if-else條件是這樣,並嘗試..

if (convertView == null) { 

     convertView = LayoutInflater.from(getActivity()).inflate(
       R.layout.chat_row, parent, false); 
     holder = new ViewHolder(); 
     holder.messageLeft = (TextView) convertView 
      .findViewById(R.id.leftBox); 
     holder.messageRight = (TextView) convertView 
      .findViewById(R.id.rightBox); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

和刪除

convertView.setTag(holder); 

底部

+0

它沒有幫助有同樣的效果 –

+0

發生了什麼的當滾動 –

+0

雅我做了更正你告訴,並執行什麼user370305告訴,現在它的作品fyn –

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

     final ViewHolder holder; 
     if (convertView == null) { 

      convertView = LayoutInflater.from(getActivity()).inflate(
        R.layout.chat_row, parent, false); 
      holder = new ViewHolder(); 
holder.messageLeft = (TextView) convertView 
       .findViewById(R.id.leftBox); 
     holder.messageRight = (TextView) convertView 
       .findViewById(R.id.rightBox); 
    convertView.setTag(holder); 

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

     try { 
      if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) { 
       holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
       holder.messageLeft.setVisibility(View.VISIBLE); 
      } else { 
       holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position)); 
       holder.messageRight.setVisibility(View.VISIBLE); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return convertView; 

    } 

這是使用viewholder