小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

不懂記,SimpleAdapter自定義和添加自控件監(jiān)聽——day4

 昵稱15103532 2013-12-20
SimpleAdapter 雖然很簡單,但是總會有忘記的一天。忘了在哪里看過,說持久化到硬盤里,我這希望持久化到網(wǎng)盤,只要有網(wǎng)絡(luò),都可以看到。
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的一項(xiàng)
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,如果指定了就調(diào)用其setViewValue方法, SimpleAdapter.ViewBinder是一個接口,也只有這一個方法,如果ViewBinder返回true表示我們已經(jīng)完成了對這個View的數(shù)據(jù)綁定,就不再調(diào)用系統(tǒng)默認(rèn)的實(shí)現(xiàn),當(dāng)然我們也可以設(shè)置一個ViewBinder添加一些功能后通過返回false再讓系統(tǒng)綁定數(shù)據(jù),比如對按鈕添加響應(yīng)事件,而按鈕上的文字由默認(rèn)實(shí)現(xiàn)綁定.通過看bindView的實(shí)現(xiàn)就可以明白開始時所說的綁定順序了:Checkable,TextView,ImageView.

  我們對ListItem的自定義是通過對SimpleAdapter設(shè)置ViewBinder來實(shí)現(xiàn)的

系統(tǒng)對每一個view調(diào)用binder的setViewValue(此例中是R.id.text和R.id.button,一個TextView與一個Button),我們首先檢測這個view是不是一個Button,如果是的話就關(guān)聯(lián)點(diǎn)擊事件,可能通過getParent()函數(shù)取得parentView以找到這個view的兄弟view,比如這個例子中的實(shí)現(xiàn)就是點(diǎn)擊Button后輸出這個Button所在的ListItem中的TextView上的文字.

  在setViewValue中可以完全自定義我們的實(shí)現(xiàn),比如在Button后加一個TextView,當(dāng)然可以加任何View,但這樣做沒任何意義,當(dāng)你需要這樣做時你不需要用SimpleAdater而應(yīng)該用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);
}
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多