2012-01-27 72 views
0

我有列表項目有兩個textview和一個imageview.I充氣列表ArrayAdapter.Everything工作正常,除了更改列表項目顏色click.I在我的22項列表顯示。主要是在屏幕上顯示10個項目,並獲得滾動其他項目。現在我的問題是,當我點擊0-9之間的單個項目(最初的10個項目)項目單擊時正確更改其顏色,但是當我滾動並單擊對於位置大於9的項目(在最初的10個項目之後)我的活動崩潰了。我指的是http://www.mail-archive.com/[email protected]/msg09740.html鏈接編寫的代碼for loop。幫助我擺脫此問題。任何建議或解決方案將高度讚賞.Thanx提前。獲取問題設置列表視圖項目的顏色

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     // requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard 
      setContentView(R.layout.defect_pic_listview); 

      try{ 

       adapter = new MyArrayAdapter(this,makeList()); 
       setListAdapter(adapter); 
       adapter.notifyDataSetChanged(); 
       getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
       getListView().setOnItemClickListener(new OnItemClickListener() { 

         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
         // Toast.makeText(getApplicationContext(), "Item "+position+" is clicked", 
         //   Toast.LENGTH_SHORT).show(); 

          System.out.println("position"+position); 
           int first = getListView().getFirstVisiblePosition(); 
           System.out.println("first="+first); 
           int last = getListView().getLastVisiblePosition(); 
           System.out.println("last="+last); 
           int total = last - first; 
           System.out.println("total="+total); 
           if(getListView().isItemChecked(position)){ 
            for(int i = 0 ; i <= last ; i++){ 
             System.out.println("i="+i); 
             if(first+i == position){ 
              getListView().getChildAt(i).setBackgroundColor(Color.GREEN); 
              System.out.println("l1="+getListView()); 
            //  l.getItemAtPosition(i); 
            //  System.out.println("l position"+l); 
             } 
             else{ 
              getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 
              System.out.println("l2="+getListView()); 
             } 
            } 
           } 
           else{ 
            getListView().getChildAt(position - first).setBackgroundColor(Color.TRANSPARENT); 
           } 
          } 
        }); 
      } 
      catch(Exception e){ 
       Log.d("error",e.getMessage()); 
      } 
    } 
+0

您可以發佈代碼MyArrayAdater.class? – Hiral 2012-01-27 10:44:04

+0

下次,請編輯您的原始問題。請不要兩次問同一件事。 – Kev 2012-02-16 21:26:18

回答

0

我給的答案我自己的問題。

這裏是完美運行代碼: -

getListView().setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, 
         long id) { 


      // Toast.makeText(getApplicationContext(), "Item "+position+" is clicked", 
       //   Toast.LENGTH_SHORT).show(); 

         System.out.println("position="+position); 
         int first = getListView().getFirstVisiblePosition(); 
         System.out.println("first="+first); 
         int last = getListView().getLastVisiblePosition(); 
         System.out.println("last="+last); 
         int total = last - first; 
         System.out.println("total="+total); 
         if(getListView().isItemChecked(position)){ 
          for(int i = first ; i <= last ; i++){ 
           System.out.println("i="+i); 
           if(i == position){ 
            Log.w("TAG", "I am in If block"); 
            getListView().getChildAt(i-first).setBackgroundColor(Color.GREEN); 
            System.out.println("l1="+getListView()); 
          //  l.getItemAtPosition(i); 
          //  System.out.println("l position"+l); 
           } 
           else{ 

            getListView().getChildAt(i-first).setBackgroundColor(Color.TRANSPARENT); 
            System.out.println("l2="+getListView()); 
           } 
          } 
         } 
         else{ 
          getListView().getChildAt(position).setBackgroundColor(Color.TRANSPARENT); 
         } 
        } 

      }); 
0

將此代碼用於for循環。

if(getListView().isItemChecked(position)) 
{          
    for(int i = 0 ; i < total ; i++) 
    {           
    System.out.println("i="+i); 
    if(first+i == position) 
    { 
     getListView().getChildAt(i).setBackgroundColor(Color.GREEN); 
      System.out.println("l1="+getListView()); 
            //  l.getItemAtPosition(i); 
            //  System.out.println("l position"+l); 
     } 
     else 
     { 
     getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 
     System.out.println("l2="+getListView 
()); 
    } 
} 

我想你打算只更改點擊項目的bacground顏色。爲此,你不需要添加for循環。如果你的意圖和我說的一樣,那麼只能在for循環的地方使用這個代碼。

getListView().getChildAt(position).setBackgroundColor(Color.GREEN); 
+0

我已經檢查了所有變量和變量的組合,但沒有一個爲整個lisview工作。如果我不在這裏使用循環,那麼下一個點擊的項目將被着色,但jvm如何着色前一個項目。 – himanshu 2012-01-27 10:16:11

0

還有其他的方法。

首先你需要添加一個選擇器xml例如listviewitem_bg.xml

<item android:drawable="@drawable/listview_normal" android:state_enabled="true" android:state_pressed="false"/> 
<item android:drawable="@drawable/listview_press" android:state_enabled="true" android:state_pressed="true"/> 

然後將其設置爲你的列表視圖單元格的背景。

+0

你不明白我的問題,我不想要方法突出顯示該項目只在按下狀態。我想改變一個項目的顏色時,點擊它和項目持有的顏色,直到其他項目單擊,現在點擊的項目持有的顏色。 – himanshu 2012-01-27 10:10:02

0

你可以嘗試這樣的:

public class CustomAdapter extends ArrayAdapter<String> { 

    String[] array; 
    Context mContext; 
    LayoutInflater mInflater; 
    int[] itemStates; 

    public CustomAdapter(Context context, int textViewResourceId, 
      String[] objects) 
    { 
     super(context, textViewResourceId, objects); 
     array=objects; 
     mContext=context; 
     mInflater = LayoutInflater.from(context); 
     //save all buttons state as 0(not clicked) initially 
     itemStates=new int[objects.length]; 
     for(int i=0;i<objects.length;i++) 
     { 
      itemStates[i]=0; 
     } 
    } 

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

     if(convertView==null) 
     { 
       convertView = mInflater.inflate(R.layout.custom_listitem, null); 
       holder = new ViewHolder(); 

       holder.text=(TextView)convertView.findViewById(R.id.text); 
       holder.layout=(LinearLayout)convertView.findViewById(R.id.linear_layout); // outer most linear layout iin custom_listitem xml 
       convertView.setTag(holder); 
     } 
     else 
       holder=(ViewHolder)convertView.getTag(); 

     holder.text.setText(array[position]); 

     if(itemStates[position]==0) 
     { 
      holder.layout.setBackgroundResource(R.drawable.red_gradient); // item is not clicked/selected yet 
     } 
     else 
     {    
      holder.button.setBackgroundResource(R.drawable.green_gradient); // item is clicked/selected so change its color   
     }  

     final int pos=position; 
     holder.layout.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       itemStates[pos]=1;    
       holder.button.setBackgroundResource(R.drawable.green_gradient);     
      } 
     }); 

     return convertView; 
    } 

    static class ViewHolder 
    { 
     TextView text; 
     LinearLayout layout; 
    } 
} 

這會給你的行爲,如:

  • 首先,所有項目將是紅色。
  • 如果您點擊第二項,則它會變成綠色(根據我的代碼)。
  • 現在,當您滾動並點擊任何其他項目時,它們將繼續從紅色變爲綠色。
  • 但是,如果你點擊「綠色」彩色物品,它會再次變成紅色,因爲它是未經選擇的!
0

你可以做這樣的事情

@外的上創建

View prevView; 

然後在OnCreate中

lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
        long arg3) { 
       if (prevView != null) { 
        prevView.setBackgroundColor(Color.TRANSPARENT); 
       } 
       view.setBackgroundColor(Color.RED); 
       prevView = view; 
      } 
     }); 
0

一個清潔的(?)解決方案我已經成功地使用了創建LinearLayout的擴展窗口小部件(或者您用於listitem佈局的任何根視圖類型),它實現了Checkable

public class CheckableLinearLayout extends LinearLayout implements Checkable { 

boolean checked = false; 

public CheckableLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CheckableLinearLayout(Context context) { 
    super(context); 
} 

@Override 
public void setChecked(boolean checked) { 

    this.checked = checked; 
    updateView(); 


} 

/* (non-Javadoc) 
* @see android.widget.Checkable#isChecked() 
*/ 
@Override 
public boolean isChecked() { 
    return this.checked; 
} 

/* (non-Javadoc) 
* @see android.widget.Checkable#toggle() 
*/ 
@Override 
public void toggle() { 
    this.checked=!this.checked; 
    updateView(); 

} 

private void updateView() { 
    if (this.checked) { 
          //Change to Whatever your checked color should be, maybe expose this as attribute so i't can be changed from xml attribute 
      setBackgroundResource(R.color.default_background_color); 
    } else { 
     setBackgroundDrawable(null); 
    } 
    invalidate(); 
} 

} 

然後在你的項目佈局的xml:

<my.app.widgets.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

..any views for your listitem 

<my.app.widgets.CheckableLinearLayout/>