Listview不同项问题

这里主要介绍下一个Listview怎么显示不同类型的条目的问题。

方法一:

最简单就是在getView里面判断position再分别inflate出不同的条目。

例子:创建45个不同的条目,在第一个和第20个是button,其他是Textview。

见代码:

</pre><pre name="code" class="html"><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"
    tools:context="com.example.listviewtest.MainActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="按钮" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="文本" />

</LinearLayout>

package com.example.listviewtest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
	private ListView listView;
	private final int MAX_SIZE = 45;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) this.findViewById(R.id.listview);
		listView.setAdapter(new MyAdapter());
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return MAX_SIZE;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (position == 0 || position == 20) {
				convertView = getLayoutInflater().inflate(R.layout.button_item,
						null);
			} else {
				convertView = getLayoutInflater().inflate(
						R.layout.textview_item, null);
			}
			return convertView;
		}

	}

}



这里没有做缓存优化。当然上面的方法是可以行通的。


方法二:

运用getViewTypeCount和getItemViewType方法,第一个方法返回有多少种类型,第二个方法为不同位置的Item设置type。

下面看修改后的代码(缓存已经写好了)

package com.example.listviewtest;

import android.support.v7.app.ActionBarActivity;
import android.R.integer;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	private ListView listView;
	private final int MAX_SIZE = 45;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) this.findViewById(R.id.listview);
		listView.setAdapter(new MyAdapter());
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return MAX_SIZE;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			int type = getItemViewType(position);
			if (convertView == null) {
				if (type == 0) {
					// 按钮
					BtViewHolder btViewHolder = new BtViewHolder();
					convertView = getLayoutInflater().inflate(
							R.layout.button_item, null);
					btViewHolder.bt = (Button) convertView
							.findViewById(R.id.bt);
					convertView.setTag(btViewHolder);
				} else {
					TvViewHolder tvViewHolder = new TvViewHolder();
					convertView = getLayoutInflater().inflate(
							R.layout.textview_item, null);
					tvViewHolder.tv = (TextView) convertView
							.findViewById(R.id.tv);
					convertView.setTag(tvViewHolder);
				}
			}
			if (type == 0) {
				BtViewHolder btViewHolder = (BtViewHolder) convertView.getTag();
				if (btViewHolder != null) {
					btViewHolder.bt.setText(position + "");
				}

			} else {
				TvViewHolder tvViewHolder = (TvViewHolder) convertView.getTag();
				if (tvViewHolder != null) {
					tvViewHolder.tv.setText(position + "");
				}

			}
			return convertView;
		}

		@Override
		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return 2;
		}

		@Override
		public int getItemViewType(int position) {
			if (position == 0 || position == 20)
				return 0;
			return 1;
		}

	}

	private class BtViewHolder {
		public Button bt;
	}

	private class TvViewHolder {
		public TextView tv;
	}

}




你可能感兴趣的:(ListView)