onListItemClick で、クリックしたアイテムの背景を変更したいのですが、これは私のコードです:
public void onListItemClick(ListView parent, View v, int position, long id){
if (position != previous_position){
v.setBackgroundResource(R.drawable.category_clicked_item_bg);
if (previous_category != null)
previous_category.setBackgroundResource(R.drawable.category_item_bg);
previous_category = v;
previous_position = position;
}
}
previous_category と previous_position は保護された変数です。
問題は、項目をクリックすると、リスト内の複数の項目が強調表示されることです (具体的には 1 つまたは 2 つ)。リストには20行あります。中央の1つをクリックすると、1つだけが強調表示され、最初または最後の行をクリックすると、反対側の他の行が間隔13行で強調表示されます(おそらく中央の行を押す理由ですドン2 番目にハイライトしないでください)。
クリックした行を 1 行だけ強調表示するリストが必要ですが、何が問題ですか?
追加コード
それが役立つ場合は、ListView アダプター クラスも示します。
package domehotel.guestbook.page.category;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import domehotel.guestbook.R;
public class CategoryAdapter extends ArrayAdapter<CategoryItem>{
Context context;
int layoutResourceId;
CategoryItem data[] = null;
public CategoryAdapter(Context context, int layoutResourceId, CategoryItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CategoryHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CategoryHolder();
holder.category_name = (TextView)row.findViewById(R.id.category_name);
row.setTag(holder);
}
else
{
holder = (CategoryHolder)row.getTag();
}
CategoryItem category = data[position];
holder.category_name.setText(category.category_name);
return row;
}
static class CategoryHolder
{
TextView category_name;
}
}