ListView+介面卡

程式碼修行者發表於2015-07-20
public class MainActivity extends ActionBarActivity implements ListView.OnItemClickListener,ListView.OnScrollListener {
    private ListView listView;
    private ArrayAdapter arrayAdapter;
    private SimpleAdapter simpleAdapter;
    private List<Map<String,Object>> data_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView);

        String[] data = {"列表1","列表2","列表3","列表4","列表5"};
        data_list = new ArrayList<Map<String ,Object>>();


        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);

        /**
         * 上下文
         * 資料來源
         * 佈局xml
         * 資料來源的欄位
         * 佈局xml中對應的欄位
         */
        simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});


        //listView.setAdapter(arrayAdapter);
        listView.setAdapter(simpleAdapter);


        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);
    }

    private List<Map<String,Object>> getData(){
        for(int i = 0; i < 40 ; ++i){
            Map<String,Object> map = new HashMap<String ,Object>();
            map.put("pic",R.mipmap.ic_launcher);
            map.put("text", "文字" + i);
            data_list.add(map);
        }
        return data_list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String str = listView.getItemAtPosition(position).toString();
        Toast.makeText(getApplicationContext(),position+"=>"+str,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState){
            case SCROLL_STATE_FLING:
                Log.i("main", "手指快速滑動螢幕並離開,導致檢視一直滑動");
                Map<String,Object> map = new HashMap<String ,Object>();

                //簡單的滑動重新整理
                map.put("pic",R.mipmap.ic_launcher);
                map.put("text", "新的文字"+ System.currentTimeMillis());
                data_list.add(map);
                listView.deferNotifyDataSetChanged(); //通知資料來源
                break;
            case SCROLL_STATE_IDLE:
                Log.i("main","檢視停止滑動");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("main","手指真正螢幕上滑動");
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="15dp"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginTop="15dp"
        />
</LinearLayout>

listview

相關文章