1-声明变量

    private ListView listView;    private SimpleAdapter simpleAdapter;    List<Map<String,Object>> valueList = new ArrayList<>();    Map<String, Object> mapRow0 = new HashMap<>();    Map<String, Object> mapRow1 = new HashMap<>();    Map<String, Object> mapRow2 = new HashMap<>();    Map<String, Object> mapRow3 = new HashMap<>();

2-Map添加映射

    mapRow0.put("view0","第1个View");    mapRow0.put("view1","第2个View");    mapRow1.put("view0","第1个View");    mapRow1.put("view1","第2个View");    mapRow2.put("view0","第1个View");    mapRow2.put("view1","第2个View");    mapRow3.put("view0","第1个View");    mapRow3.put("view1","第2个View");

3-将Map添加到List中

    valueList.add(mapRow0);    valueList.add(mapRow1);    valueList.add(mapRow2);    valueList.add(mapRow3);

4-给listView,sampleAdapter赋值,关联listView

        listView = (ListView) findViewById(R.id.listview);        SimpleAdapter = new SimpleAdapter(MainActivity.this,                valueList,                R.layout.test_layout,                new String[]{"view0","view1"},                new int[]{R.id.gg1,R.id.gg2});        listView.setAdapter(SimpleAdapter);

这里的view0与test_layout中的gg1关联,view1与test_layout中的gg2关联;

5-设置Touch事件

        listView.setOnTouchListener(new OnTouchListener(){            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                if(event.getAction() == MotionEvent.ACTION_DOWN)                {                    //listView.setBackgroundColor(Color.BLUE);                    Toast.makeText(MainActivity.this,                            "TOUCH.....",                            Toast.LENGTH_LONG).show();                    //startActivity(new Intent(MainActivity.this,LoginActivity.class));                    mapRow0.put("view0","第1个 Touch-Row0");                    mapRow0.put("view1","第2个 Touch-Row0");                    listView.setAdapter(simpleAdapter);                }                return false;            }        });

修改mapRow0中的值,不用重新设置适配器simpleAdapter,只用让listView重新加载适配器即可。

6-test_layout.xml编写

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/testlayout">    <TextView        android:text="TextView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/gg1" />    <TextView        android:text="TextView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/gg2" />LinearLayout>

更多相关文章

  1. 箭头函数的基础使用
  2. Android(安卓)添加快捷方式
  3. android spinner 选中后显示字体的颜色设置
  4. Android(安卓)P Android.dp添加逻辑判断
  5. android 开机启动服务
  6. android studio无法关联源码
  7. 解决android.os.DeadObjectException问题
  8. Android(安卓)fragment在xml中使用没添加ID
  9. Android——Notifications笔记

随机推荐

  1. qcts--快速使用Android(安卓)CTS进行测试
  2. android项目中在webview中打开pdf
  3. Android(安卓)init源代码分析(1)概要分析
  4. Android我还可以相信你多少系列文章二之
  5. android的binder机制研究(C++部分)
  6. Android系统原理及开发要点详解
  7. 掌握Android中的进程和线程
  8. Android之Service学习篇二:Service启动方
  9. SEAndroid安全机制简要介绍和学习计划
  10. android播放swf文件