2016-12-27 113 views
0

我有一個自定義適配器列表視圖,當我在第一個項目點擊有按鈕(「下一個)下面的列表視圖可見,第一行項目的文字顏色的變化。 上的點擊按鈕下面我想改變下一行項文本的顏色。請幫忙,謝謝。列表視圖上的按鈕進行更改項目文本顏色單擊

mainListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

    itemPosition =i; 
      String text = textView.getText().toString(); 
      Toast.makeText(getContext(), "Selected item: " + text + " - " + i, Toast.LENGTH_SHORT).show(); 

      song_namef.setText(text); 

      playSong(i); 
      hello.setVisibility(View.VISIBLE); 

      setItemNormal(); 
      View rowView = view; 
      setItemSelected(rowView); 

     } 
    }); 
public void setItemSelected(View view){ 
    View rowView = view; 
    TextView tv = (TextView)rowView.findViewById(R.id.textView); 
    tv.setTextColor(Color.WHITE); 
} 

public void setItemNormal() 
{ 
    for (int i=0; i< mainListViews.getChildCount(); i++) 
    { 
     View v = mainListViews.getChildAt(i); 
     TextView txtview = ((TextView)v.findViewById(R.id.textView)); 
     txtview.setTextColor(getResources().getColor(R.color.tabsScrollColor)); 
    }} 
hello.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
    Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show(); 

       itemPosition=itemPosition+1; 
here i want to color next row of item text 
}}); 

getView

public View getView(final int position, View convertView, ViewGroup viewGroup) { 

     val = position; 
     viewHolder = null; 

     if (convertView == null) { 
      viewHolder = new ViewHolder(); 
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      convertView = layoutInflater.inflate(R.layout.list_items, viewGroup, false); 

      // Find the child views. 
      viewHolder.country = (TextView) convertView.findViewById(R.id.textView); 
      viewHolder.cancel = (ImageView)convertView.findViewById(R.id.button); 



      convertView.setTag(viewHolder); 
      // convertView.setTag(vh); 
     } 
     else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
      //vh = (ViewHolder)vi.getTag(); 
     } 
     String hello = countryList.get(position); 
     Log.d("Hello",hello); 


     viewHolder.country.setText(hello.substring(18)); 

回答

1

首先創建一個selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true" android:color="@color/black" /> 
<item android:state_focused="true" android:color="@color/black" /> 
<item android:state_pressed="true" android:color="@color/black" /> 
<item android:color="@color/white" /> 
</selector> 

後,在列表項的佈局定義textColor屬性爲:

android:textColor="@color/list_item_text" 
+0

它的列表項目視圖。我怎麼能做到按鈕按鈕點擊,因爲按鈕單擊不能調用列表視圖行! –

+0

@SumitKumar每次點擊按鈕,都會增加列表的行索引,從而實現顏色代碼。 –

+0

你能解釋一些代碼!請 !! –

1

試試這個:

hello.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show(); 
     itemPosition = itemPosition + 1; 

     setItemNormal(); 
     // check if the next view is visible on screen, then change color 
     if(itemPosition <= mainListViews.getLastVisiblePosition()){ 
      View rowView = mainListViews.getChildAt(itemPosition - mainListViews.getFirstVisiblePosition()); 
      setItemSelected(rowView); 
     } 
}}); 

。希望幫助!

順便說一句,直接修改視圖是壞的,因爲視圖將滾動列表滾動時重繪。如果您的列表很長,請查看您的代碼。還給出了變量合適的名稱,例如「hello」 - >「btnNext」將幫助其他人理解您的代碼並提出解決方案。

相關問題