2017-02-27 11 views
0

色を正常に代用するために管理している行のリストビューセットを作成しました。AndroidのListViewで選択した行を強調表示します。

ただし、ユーザーが選択した行の色を変更することができます。次のように

私はXMLを設定している:

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/listView" 
    android:listSelector="@android:color/holo_red_light"/> 

私の完全なJavaコードは以下の通りです:

public class Task2Activity extends ListActivity { 

ListView list; 
String [] dogTitles; 
String [] dogDescriptions; 
String [] dogToastDescriptions; 
int[] images = {R.drawable.husky, R.drawable.pug, R.drawable.yorkshire_terrier, R.drawable.french_bulldog, 
R.drawable.border_collie, R.drawable.great_dane, R.drawable.staffordshire_bull_terrier, R.drawable.golden_retriever, 
R.drawable.german_shepherd, R.drawable.doberman}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_task2); 

    Resources res = getResources(); 
    dogTitles = res.getStringArray(R.array.titles); 
    dogDescriptions = res.getStringArray(R.array.descriptions); 
    dogToastDescriptions = res.getStringArray(R.array.toastdescriptions); 



    //list = (ListView) findViewById(R.id.listView); 
    dogAdapter <String> adapter = new dogAdapter<String>(this, dogTitles, images, dogDescriptions) { 
     //list. 

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

      View view = super.getView(position, convertView, parent); 
      if (position %2 == 1) 
      { 
       view.setBackgroundColor(Color.parseColor("#e6e9ef")); 
      } 
      else 
      { 
       view.setBackgroundColor(Color.parseColor("#c9d0db")); 
      } 
      return view; 

     } 
    }; 

    setListAdapter(adapter); 

    registerForContextMenu(getListView()); 

     } 

    @Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.my_context_menu, menu); 

} 

protected void onListItemClick(ListView l, View v, int position, long id){ 

    String item = dogToastDescriptions[position]; 


     Toast.makeText(this, item, Toast.LENGTH_LONG).show(); 
    getListView().setSelected(true); 


     } 

class dogAdapter<S> extends ArrayAdapter<String> { 
Context context; 
int[] images; 
String[] titleArray; 
String[] descriptionArray; 

dogAdapter(Context c, String[] titles, int imgs[], String[] desc) { 
    super(c, R.layout.single_row, R.id.largeText, titles); 
    this.context = c; 
    this.images = imgs; 
    this.titleArray = titles; 
    this.descriptionArray = desc; 
} 

class MyViewHolder { 
    ImageView myImage; 
    TextView myTitle; 
    TextView myDescription; 

    MyViewHolder(View v) { 
     myImage = (ImageView) v.findViewById(R.id.imageView); 
     myTitle = (TextView) v.findViewById(R.id.largeText); 
     myDescription = (TextView) v.findViewById(R.id.smallText); 
    } 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    MyViewHolder holder = null; 
    if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.single_row, parent, false); 
     holder = new MyViewHolder(row); 
     row.setTag(holder); 
     Log.d("dogs", "Creating a new row"); 
    } else { 
     holder = (MyViewHolder) row.getTag(); 
     Log.d("dogs", "Recycling stuff"); 
    } 
    //ImageView myImage = (ImageView) row.findViewById(R.id.imageView); 
    //TextView myTitle = (TextView) row.findViewById(R.id.largeText); 
    //TextView myDescription = (TextView) row.findViewById(R.id.smallText); 

    holder.myImage.setImageResource(images[position]); 
    holder.myTitle.setText(titleArray[position]); 
    holder.myDescription.setText(descriptionArray[position]); 

    return row; 
} 

} 
} 

すべてのヘルプは大歓迎します。多くのおかげでonListItemClickで

+0

答えはありますか? – sivaram

答えて

2

()選択した項目の色を設定することができます。

<ListView android:id="@+id/my_listView" 
    android:choiceMode="singleChoice" 
    android:listSelector="@android:color/darker_gray" <!--your color here --> 
    /> 

アンドロイド:

protected void onListItemClick(ListView l, View v, int position, long id){ 

    String item = dogToastDescriptions[position]; 

    v.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_green_light));//your color here 

    Toast.makeText(this, item, Toast.LENGTH_LONG).show(); 
    getListView().setSelected(true); 

} 

リストビューでのオプションがハイライト値を選択していることもあります: choiceMode = "singleChoice"はあなたが探しているものです

+0

ありがとうございます。クリックすると強調表示されますが、別の行をクリックすると強調表示されたままになります。言い換えれば、私は行をクリックし続けて、それを強調表示し続けます。 1つを強調表示してから別のものをクリックすると別のものを強調表示できるようにしたいと思います。ありがとう –

+0

@ PhilAdams、強調表示された最後にクリックされたフラグメントの位置を保存し、元の色に戻してから、新しく選択したアイテムを強調表示された色に変更する必要があります。 –

+0

@BradleyWilson提案していただきありがとうございます。私はそれをどうすればよいか分かりませんが、私はそれを見て、 –

関連する問題