1. 程式人生 > >設定ListView預設選中第一項

設定ListView預設選中第一項

專案中遇到這樣的一個需求,開啟一個poupuWindow,裡面是一個ListView列表,需求預設選中第一項,查了網上的很多方法都是listView.setItemChecked(0,true);然並卵,發現沒什麼效果。最終發現,正確的姿勢是這樣的!在adapter裡新增一個方法:

public void setDefSelect(int position) {
            this.defItem = position;
            notifyDataSetChanged();
        }

然後程式碼中呼叫:

adapter.setDefSelect(0
);//設定預設選中第一項

完整Demo程式碼如下:

package com.example.listviewdefitem;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import
android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ListView listview; private
PersonAdapter adapter; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.list_view_parent); adapter = new PersonAdapter(this, getPersons()); listview.setAdapter(adapter); adapter.setDefSelect(0);//設定預設選中第一項 //ListView項的點選事件 listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { adapter.setDefSelect(position); Person p = (Person) listview.getItemAtPosition(position); Toast.makeText(MainActivity.this, p.toString(), Toast.LENGTH_SHORT).show(); } }); } public List<Person> getPersons() { List<Person> persons = new ArrayList<Person>(); Person p1 = new Person(); p1.setName("張三"); p1.setSex("20"); Person p2 = new Person(); p2.setName("李四"); p2.setSex("21"); Person p3 = new Person(); p3.setName("王五"); p3.setSex("22"); Person p4 = new Person(); p4.setName("招六"); p4.setSex("23"); Person p5 = new Person(); p5.setName("李四"); p5.setSex("24"); persons.add(p1); persons.add(p2); persons.add(p3); persons.add(p4); persons.add(p5); return persons; } class PersonAdapter extends BaseAdapter { private Context context; private List<Person> persons; private ViewHolder holder; private int defItem;//宣告預設選中的項 public PersonAdapter(Context context, List<Person> persons) { super(); this.context = context; this.persons = persons; } @Override public int getCount() { return persons.size(); } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } /** 介面卡中新增這個方法 */ public void setDefSelect(int position) { this.defItem = position; notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_parent_item, null); holder = new ViewHolder(); holder.item1 = (TextView) convertView.findViewById(R.id.item1); holder.item2 = (TextView) convertView.findViewById(R.id.item2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (defItem == position) { convertView.setBackgroundResource(R.drawable.selector_package_list_n); } else { convertView.setBackgroundResource(android.R.color.transparent); } //繫結資料 Person person = persons.get(position); holder.item1.setText(person.getName()); holder.item2.setText(person.getSex()); return convertView; } class ViewHolder { TextView item1, item2; } } }