2014-09-10 89 views
0

我需要在我的viewHolder對象中設置標籤的幫助。我有一個比屏幕更長的列表(在電話上),我需要爲每個視圖設置不同的標籤。正如你可以在我的預覽應用程序(下面的鏈接)中看到的外部片段中的第一項和最後一項綁定在一起。我不'那樣想,我希望每一個觀點都是獨一無二的。我怎樣才能做到這一點。指定唯一標籤列表中的項目列表

https://drive.google.com/file/d/0B2xlu23GjnfvTEgtbkprOThtMmM/edit?usp=sharing

感謝

我InspectionListAdapter

public class InspectionListAdapter extends ArrayAdapter<String> { 

private final Activity context; 
private final String[] inspectionTitles; 
private final String[] description; 

public InspectionListAdapter(Activity context, String[] inspectionTitles, 
     String[] description) { 
    super(context, R.layout.inspection_item_layout, inspectionTitles); 
    this.context = context; 
    this.inspectionTitles = inspectionTitles; 
    this.description = description; 
} 

public static class ViewHolder { 
    public ImageView imageView, expandButton; 
    public TextView txtTitle; 
    public TextView txtDescription; 
    public RelativeLayout expandLayout; 
    public EditText ownDesc, estCost; 
    public LayerDrawable listItem; 
    public GradientDrawable color; 
    public CheckBox checkRepair, checkDone; 
    public RelativeLayout mainLayout; 

} 

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

    final ViewHolder viewHolder; 

    View rowView = view; 

    if (rowView == null) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.inspection_item_layout, null, 
       true); 

     viewHolder = new ViewHolder(); 



     viewHolder.mainLayout = (RelativeLayout) rowView 
       .findViewById(R.id.item_layout_main); 

     viewHolder.txtTitle = (TextView) rowView 
       .findViewById(R.id.textViewItemTitle); 

     viewHolder.txtDescription = (TextView) rowView 
       .findViewById(R.id.textViewTip); 

     viewHolder.listItem = (LayerDrawable) rowView.getResources() 
       .getDrawable(R.drawable.layer_car_background); 

     viewHolder.color = (GradientDrawable) (viewHolder.listItem 
       .findDrawableByLayerId(R.id.item_list_background)); 

     viewHolder.imageView = (ImageView) rowView 
       .findViewById(R.id.imageViewItem); 

     viewHolder.expandButton = (ImageView) rowView 
       .findViewById(R.id.imageViewTitle); 

     viewHolder.ownDesc = (EditText) rowView 
       .findViewById(R.id.editTextDescription); 

     viewHolder.estCost = (EditText) rowView 
       .findViewById(R.id.editTextEstCost); 

     viewHolder.checkRepair = (CheckBox) rowView 
       .findViewById(R.id.checkBox2); 

     viewHolder.checkDone = (CheckBox) rowView 
       .findViewById(R.id.checkBox1); 

     viewHolder.checkRepair 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton view, 
          boolean arg1) { 
         // TODO Auto-generated method stub 

         if (view.isChecked()) { 
          viewHolder.checkDone.setChecked(false); 
         } 
        } 
       }); 
     viewHolder.checkDone 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton view, 
          boolean arg1) { 
         // TODO Auto-generated method stub 

         if (view.isChecked()) { 
          viewHolder.checkRepair.setChecked(false); 

         } 
        } 
       }); 

     viewHolder.expandLayout = (RelativeLayout) rowView 
       .findViewById(R.id.relativeLayoutExpandable); 

     viewHolder.expandLayout.setVisibility(View.GONE); 

     viewHolder.expandButton 
       .setOnClickListener(new OnClickListener() { 

        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         if (viewHolder.expandLayout.getVisibility() == View.VISIBLE) { 
          viewHolder.expandButton 
            .setImageResource(R.drawable.ic_action_expand); 
          viewHolder.expandLayout 
            .setVisibility(View.GONE); 
          if (viewHolder.checkDone.isChecked()) { 

           viewHolder.mainLayout 
             .setBackgroundResource(R.drawable.layer_car_background_green); 

          } 
          if (viewHolder.checkRepair.isChecked()) { 

           viewHolder.mainLayout 
             .setBackgroundResource(R.drawable.layer_car_background_red); 
          } 

         } else { 
          viewHolder.expandButton 
            .setImageResource(R.drawable.ic_action_collapse); 
          viewHolder.expandLayout 
            .setVisibility(View.VISIBLE); 

          viewHolder.mainLayout 
            .setBackgroundResource(R.drawable.layer_car_background); 

         } 
        } 
       }); 


     rowView.setTag(viewHolder); 

    } else { 

     viewHolder = (ViewHolder) rowView.getTag(); 
    } 


    viewHolder.txtTitle.setText(inspectionTitles[position]); 
    viewHolder.txtDescription.setText("Tip: \n" + description[position]); 
    viewHolder.imageView.setImageResource(R.drawable.ic_launcher); 
    viewHolder.expandButton 
      .setImageResource(R.drawable.ic_action_expand); 

    return rowView; 
} 

}

+0

我建議你學習getView的工作原理。意見將得到回收。而且你不應該使「每個視圖都是唯一的」 – 2014-09-10 16:16:00

+0

我的列表不是一個動態列表,它是由string.xml文件中的字符串數組生成的預設列表。我希望我的所有觀點都保留 其onClickListeners和onCheckChangedListeners – AbsoluteHeero 2014-09-10 16:34:09

+0

請解釋您試圖爲每個視圖設置不同標記的位置。我所看到的是,您將視圖持有者設置爲視圖標記。他們在哪個方面有所不同?什麼'保持點擊聽衆等'這樣做呢?當然還有更多的物品可以放在屏幕上。而您的Google文檔在我的平板電腦上無法讀取,因此我不知道那裏出了什麼問題。 – greenapps 2014-09-10 20:00:37

回答

0

的錯誤是在OnClickListener的onclick的viewHolder.expandbutton。在那裏你可以立即開始'if(viewHolder .....)`。但這是錯誤的viewHolder,因爲在用戶點擊視圖時,許多viewHolders可能已經創建並且死亡。所以你必須先獲得正確的viewHolder。 (未經測試)

   View rowView = (View)v.getParent(); 

       ViewHolder viewHolder =(ViewHolder) rowView.getTag(); 

你把它放在onClick()方法中。 final ViewHolder viewHolder;爲了在onClick()和onCheckedChanged中使用它,你做了最後的決定。刪除最後一個。不幸的是,getParent()在onCheckedChanged中不起作用。所以我們做的不同。

viewHolder.checkDone.setTag(viewHolder.checkRepair); 
    viewHolder.checkDone 
      .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton view, 
         boolean arg1) { 

        CheckBox checkBox = (CheckBox)view.getTag(); 

        if (view.isChecked()) { 
         //viewHolder.checkRepair.setChecked(false); 
         checkBox.setChecked(false); 
        } 
       } 
      }); 

對其他複選框做相同的操作。

現在顏色擴展到另一個視角。原因是您應該在設置新的txtTitle,txtDescription等的同時設置默認顏色。所以在那裏添加viewHolder.mainLayout.setBackgroundResource(R.drawable.layer_car_background);

其餘的代碼未命中保持狀態。如果一個項目被展開並且用戶上下滾動列表(我添加了更多的項目以便能夠滾動更多),則展開狀態消失。此外,複選框不會保持選中狀態並且顏色消失(添加更多項目以查看)。你應該添加一些布爾數組(至少三個)來保持狀態。更新事件中的這些數組。在getView()中讀取數組,並相應地檢查顏色和複選框。添加你將看到的那些陣列並不是很多工作...

+0

幹得好!非常感謝 !現在,我會與國家轟動一時! – AbsoluteHeero 2014-09-11 18:26:48

相關問題