我是靠谱客的博主 大意小蘑菇,最近开发中收集的这篇文章主要介绍Android中listView选中第一行,或任意一行(listView优化,很标准),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

效果图:


代码:

<span style="font-size:18px;">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);
		this.context = MainActivity.this;
		listview = (ListView) findViewById(R.id.list_view_parent);
		adapter = new PersonAdapter(context, getPersons());
		listview.setAdapter(adapter);
		adapter.setDefSelect(0);
		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(context, 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;
		}

	}
}
</span>
javaBean:

<span style="font-size:18px;">public class Person {

	private String name;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + "]";
	}

}</span>

布局:

<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list_view_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_list_item"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent" />

</RelativeLayout></span>

<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/item1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

</LinearLayout></span>

源码下载

http://download.csdn.net/detail/zhaihaohao1/9466861

Android中listView选中第一行,或任意一行,选中后能取消

http://download.csdn.net/download/zhaihaohao1/10255237

最后

以上就是大意小蘑菇为你收集整理的Android中listView选中第一行,或任意一行(listView优化,很标准)的全部内容,希望文章能够帮你解决Android中listView选中第一行,或任意一行(listView优化,很标准)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(30)

评论列表共有 0 条评论

立即
投稿
返回
顶部