色を正常に代用するために管理している行のリストビューセットを作成しました。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で
答えはありますか? – sivaram