Android基础(六) ListView - 复杂布局

一、概述

     ListView的Item显示可以使用复杂布局。下面的实例演示了Item中包含两个TextView和一个Button布局的 ListView,并从中学习ListView和Button的事件触发方法。

二、实例

【效果图】
Android基础(六) ListView - 复杂布局_第1张图片 

 

【代码要点】

(1)ListViewHasBtn.java主程序文件。
定义新类ListBtnAdapter扩展了ArrayAdapter类,并重写getView方法,在该方法里面实现自定义布局的显示。

package enleo.ListViewHasBtn; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ListViewHasBtn extends Activity { private ListView listview; private ArrayList<ItemBO> mListItem; private void DisplayToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview = (ListView) findViewById(R.id.list_view); mListItem = ItemBO.getItems(); listview.setAdapter(new ListBtnAdapter(ListViewHasBtn.this, R.layout.list_item, mListItem)); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub DisplayToast("ListView Item position is "+arg2); } }); } private class ListBtnAdapter extends ArrayAdapter<ItemBO>{ private ArrayList<ItemBO> mList; // --CloneChangeRequired private Context mContext; public ListBtnAdapter(Context context, int textViewResourceId, ArrayList<enleo.ListViewHasBtn.ItemBO> objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub mList = objects; mContext = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = convertView; if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = mInflater.inflate(R.layout.list_item, null); } else { view = convertView; } final ItemBO listItem = mList.get(position); if (listItem != null) { ((TextView) view.findViewById(R.id.tv_name)).setText(listItem .getName()); ((Button) view.findViewById(R.id.button1)).setText(listItem .getDescription()); Button btn = ((Button) view.findViewById(R.id.button1)); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(ListViewHasBtn.this, Activity2.class); intent.putExtra("NAME", listItem.getName()); startActivity(intent); finish(); } }); } return view; } } } 

(2)点击按钮后弹出新ActivityActivity2.java
package enleo.ListViewHasBtn; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class Activity2 extends Activity { private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout relativeLayout = new RelativeLayout(this); Button button = new Button(this); button.setText("Go Back"); Bundle bundle = getIntent().getExtras(); if (bundle != null) { button.setText("Item name = " + bundle.getString("NAME") + " --- Go Back "); } else { button.setText("Go Back"); } context = this; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(context, ListViewHasBtn.class)); finish(); } }); relativeLayout.addView(button); LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); setContentView(relativeLayout, params); } }  

(3)在AndroidManifest.xml里面记得要加上新建Activity的声明。
<activity android:name=".Activity2"/>

(4) Copy of list_item.xml:ListView Item的布局文件,包括两个TextView和一个Button
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:padding="6px" android:layout_height="67px" android:id="@+id/rlt_main" android:background="#000000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_name" android:text="Name" android:layout_centerVertical="true"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Description" android:id="@+id/tv_description" android:layout_centerVertical="true" android:layout_toRightOf="@+id/tv_name" android:layout_marginLeft="10px"></TextView> <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:focusable="false" android:layout_alignParentRight="true" android:text="Click me!"> </Button> </RelativeLayout>  

三、总结

【新建Activity】

新建的Activity必须在AndroiManifest.xml里面声明。否则显示找不到该Activity,错误提示如下:

 

02-26 06:50:58.109: ERROR/AndroidRuntime(790): android.content.ActivityNotFoundException: Unable to find explicit activity class {enleo.ListViewHasBtn/enleo.ListViewHasBtn.Activity2}; have you declared this activity in your AndroidManifest.xml?

 

【Android 中的Adapter】

Android基础(六) ListView - 复杂布局_第2张图片

 

【解决ListView中的onItemClick不响应】

   因为当Item布局里面存在Button(或CheckBox之类的选择性View)时,Button事件响应的优先级是高于ListView的,所以当点击Item会出现未见ListView响应。解决方法是在Layout文件里为Button添加属性:

   android:focusable="false"  

 

你可能感兴趣的:(android,ListView,layout,Class,button,Constructor)