2016-11-10 9 views
-1

ちょっと、私はリストビュー内の特定の位置の色を変更しようとしています。私がしたいポジションの色を変えることができますが、ハイライトしたいアイテムのリストから10のポジションが強調表示されているパターンがあるように見えます。特定のリストビューの色を変更する問題

私のカスタムadapter.javaが色の変更コードでどのように設定されているかを確認します。

package com.example.zach.listview; 

import android.content.Context; 
import android.graphics.Color; 
import android.support.annotation.NonNull; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


class CustomAdapter extends ArrayAdapter<String>{ 
public CustomAdapter(Context context, String[] routes) { 
    super(context, R.layout.custom_row ,routes); 
} 
@NonNull 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater routeInflater = LayoutInflater.from(getContext()); 
    View customView = convertView; 
    if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);} 

    String singleRoute = getItem(position); 
    TextView routeText = (TextView) customView.findViewById(R.id.routeText); 

    routeText.setText(singleRoute); 

    //if (getItem(position).equals("Information")){ 
    // routeText.setBackgroundColor(Color.GRAY); 
    //} 

    if(position == 0) 
    { 
     routeText.setBackgroundColor(Color.GRAY); 
    } 
    return customView; 
} 
} 
+0

[Androidの代替行の色をリストビューで表示](http://stackoverflow.com/questions/13109840/android-alternate-row-colors-in-listview) –

+0

代わりにrecyclerviewを試してください –

答えて

0

これはリサイクルのためです。スクロールすると同じビューが再利用されます。ビューの値を設定した場合は、elseステートメントで値をデフォルトにリセットする必要があります。リサイクル時には設定されたままになります。

リストビューの基本ルール - 設定したことがある場合は、常に設定してください。

0

この方法を試してください。

if(position == 0) { 
    routeText.setBackgroundColor(Color.GRAY); 
} else { 
    routeText.setBackgroundColor(null); // or any other you want. 
} 

希望すると、これが役立ちます。

ハッピーコーディング。

関連する問題