设置ListView默认选中第一项

在adapter里添加一个方法:

public void setDefSelect(int position) {
            this.defItem = position;
            notifyDataSetChanged();
        }
adapter.setDefSelect(0);//设置默认选中第一项
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 getPersons() {
        List persons = new ArrayList();
        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 persons;
        private ViewHolder holder;
        private int defItem;//声明默认选中的项

        public PersonAdapter(Context context, List 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;
        }

    }
}

你可能感兴趣的:(设置ListView默认选中第一项)