SimpleAdapter 虽然很简单,但是总会有忘记的一天。忘了在哪里看过,说持久化到硬盘里,我这希望持久化到网盘,只要有网络,都可以看到。 activity_list_view_demo.xml <RelativeLayout xmlns:android="http://schemas./apk/res/android" xmlns:tools="http://schemas./tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ListViewDemoActivity" > <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 这是组成listView的一项 listitem.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" xmlns:tools="http://schemas./tools" android:id="@+id/listitem" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解压文件" /> </LinearLayout> ListViewDemoActivity .java import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class ListViewDemoActivity extends Activity { private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_demo); mListView = (ListView) findViewById(R.id.list); String[] from = { "Text", "Button" }; int[] to = { R.id.text, R.id.btn }; ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < 5; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("Text", i + "1"); map.put("Button", i + "2"); data.add(map); } 其流程大致是,首先检查SimpleAdapter有没有指定SimpleAdapter.ViewBinder,如果指定了就调用其setViewValue方法, SimpleAdapter.ViewBinder是一个接口,也只有这一个方法,如果ViewBinder返回true表示我们已经完成了对这个View的数据绑定,就不再调用系统默认的实现,当然我们也可以设置一个ViewBinder添加一些功能后通过返回false再让系统绑定数据,比如对按钮添加响应事件,而按钮上的文字由默认实现绑定.通过看bindView的实现就可以明白开始时所说的绑定顺序了:Checkable,TextView,ImageView. 我们对ListItem的自定义是通过对SimpleAdapter设置ViewBinder来实现的 系统对每一个view调用binder的setViewValue(此例中是R.id.text和R.id.button,一个TextView与一个Button),我们首先检测这个view是不是一个Button,如果是的话就关联点击事件,可能通过getParent()函数取得parentView以找到这个view的兄弟view,比如这个例子中的实现就是点击Button后输出这个Button所在的ListItem中的TextView上的文字. 在setViewValue中可以完全自定义我们的实现,比如在Button后加一个TextView,当然可以加任何View,但这样做没任何意义,当你需要这样做时你不需要用SimpleAdater而应该用BaseAdapter: SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listitem, from, to); SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { if (view instanceof Button) { final View button = view; // button.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher)); view.setOnClickListener(new OnClickListener() { LinearLayout listItem = (LinearLayout) button.getParent(); TextView tv = (TextView) listItem.findViewById(R.id.text); @Override public void onClick(View v) { Toast.makeText(ListViewDemoActivity.this, tv.getText(), Toast.LENGTH_SHORT).show(); } }); return false; } return false; } }; adapter.setViewBinder(binder); mListView.setAdapter(adapter); } } |
|
来自: 昵称15103532 > 《待分类1》