2012-08-16 103 views
0

我有一個使用兩個不同佈局(mycontentmtall或mycontenmotall)的列表視圖。我想在點擊時更改這兩個佈局(當我單擊一行中的對象以從我創建的兩個新佈局(mycontentmtallexapand或mycontentmoallexpand)中選擇一個時)。當在列表視圖onClick時更改行內容視圖

我面對的問題是,當我點擊ListView中的一個項目,我沒有看到佈局的變化。

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

       final ViewHolder holder; 
       // int type = getItemViewType(position); 
       final Message message = getItem(position); 

       if (convertView == null) { 

        holder = new ViewHolder(); 
        // if (type == TYPE_MT) { 
        if (!message.getIsMO()) { 
         convertView = mInflater.inflate(R.layout.mycontentmtall, 
           null); 
         holder.body = (TextView) convertView 
           .findViewById(R.id.bodyMT); 
         holder.date = (TextView) convertView 
           .findViewById(R.id.dateMT); 
         holder.from = (TextView) convertView 
           .findViewById(R.id.fromMT); 
         holder.status = (TextView) convertView 
           .findViewById(R.id.statusMT); 
        } else { 
         convertView = mInflater.inflate(R.layout.mycontentmoall, 
           null); 
         holder.body = (TextView) convertView 
           .findViewById(R.id.bodyMO); 
         holder.date = (TextView) convertView 
           .findViewById(R.id.dateMO); 
         holder.from = (TextView) convertView 
           .findViewById(R.id.fromMO); 
         holder.status = (TextView) convertView 
           .findViewById(R.id.statusMO); 
        } 
        convertView.setTag(holder); 

       } 

       else { 

        holder = (ViewHolder) convertView.getTag(); 
       } 
      // Boolean flag = false; 


      // final String body = message.getBody(); 
       convertView.setOnClickListener(new OnClickListener() { 
        private int pos = position; 

        @Override 
        public void onClick(View v) { 

         Toast.makeText(context, "Click-" + pos, Toast.LENGTH_SHORT) 
           .show(); 



         View newConvertView = null; 
         if (!message.getIsMO()){ 
         newConvertView = mInflater.inflate(R.layout.mycontentmtallexapand, 
           null); 
         holder.body = (TextView) newConvertView 
           .findViewById(R.id.bodyMTExpand); 
         holder.date = (TextView) newConvertView 
           .findViewById(R.id.dateMTExpand); 
         holder.from = (TextView) newConvertView 
           .findViewById(R.id.fromMTExpand); 
         holder.status = (TextView) newConvertView 
           .findViewById(R.id.statusMTExpand); 


         } 
         else 
         { 
          newConvertView = mInflater.inflate(R.layout.mycontentmoallexpand, 
            null); 
          holder.body = (TextVi`enter code here`ew) newConvertView 
            .findViewById(R.id.bodymoExpand); 
          holder.date = (TextView) newConvertView 
            .findViewById(R.id.datemoExpand); 
          holder.from = (TextView) newConvertView 
            .findViewById(R.id.frommoExpand); 
          holder.status = (TextView) newConvertView 
            .findViewById(R.id.statusmoExpand); 
         } 

    //     newConvertView 
    //        .setLayoutParams(new LayoutParams(
    //          LinearLayout.LayoutParams.MATCH_PARENT, 
    //          LinearLayout.LayoutParams.WRAP_CONTENT)); 
         holder.body.setText(message.getBody()); 
         holder.date.setText(message.getDate()); 
         holder.from.setText(message.getPhoneNumber()); 
         holder.status.setText(String.valueOf(message.getStatus())); 
    //     flag = true; 

        } 
       }); 
// s=body.substring(0, 20); 
       holder.body.setText(message.getBody()); 
      holder.date.setText(message.getDate()); 
      holder.from.setText(message.getPhoneNumber()); 
      holder.status.setText(String.valueOf(message.getStatus())); 

      return convertView; 

     } 
+0

嘗試在onclick()方法中更改值後使佈局無效()。 – MobileEvangelist 2012-08-16 07:20:47

+0

我該怎麼做? – mkounal 2012-08-16 07:44:16

+0

試着做這個newConvertView .invalidate();在將值設置爲文章視圖 – MobileEvangelist 2012-08-16 09:08:23

回答

1

在的onclick()convertView的嘗試使用convertView而不是創建新視圖(newConvertview)中,首先去除在convertView的元素,然後在其上充氣新的佈局。

希望對你有所幫助..