getView ()에서 :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_layout, null);
}
TextView tv=(TextView)convertView .findViewById(R.id.itemName);
try {
if(position ==1){
convertView.setBackgroundColor(Color.GREEN);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return convertView;
}
선택한 항목을 유지하려면 :
먼저 : 어댑터에서 다음 사항을 정의하십시오.
private int selectedIndex;
private int selectedColor = Color.parseColor("#1b1b1b");
In your adapter class; add this method:
public void setSelectedIndex(int position)
{
selectedIndex = position;
notifyDataSetChanged();
}
getView ()에서 다음 행을 추가하십시오.
if(selectedIndex!= -1 && position == selectedIndex)
{
txtTitle.setBackgroundColor(Color.BLACK);
}
else
{
txtTitle.setBackgroundColor(selectedColor);
}
지금 작동합니다 .. :)
-------------------재사용 할 뷰가없는 경우 레이아웃을 확장해야 함으로 변경
if (convertView !=null)
합니다if (convertView ==null)
.특정 행 / 목록 항목을 강조 표시하려면 목록보기에 클릭 리스너를 추가해야합니다. 클릭 리스너를 추가하는 방법은 다음과 같습니다. 강조 표시하도록 보기 를 업데이트하십시오 .
lv.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, SendMessage.class); String message = "abc"; intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } });
변수 v에서 팽창 된 뷰를 설정하고 convertView를 반환합니다. convertView에 v를 할당하거나 v를 반환하십시오.
익명 클래스 내에서 listView를 사용할 수 없다고 생각합니다 . AdapterView arg0을 사용해야합니다.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
AnyObject obj=(AnyObject)arg0.getItemAtPosition(position);
........
.........
.........
}
});
출처
https://stackoverflow.com/questions/22050061