ListView

问题一:嵌套问题

  • ScrollView嵌套ListView listview获取焦点把ScrollView顶上去的解决办法

    解决方法一:

      //重写ScrollView  
      @Override  
      protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {  
          return 0;  
      }  

    解决方法二:

        listView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
          @Override
          public void onLayoutChange(View v, int left, int top, int right,
                                     int bottom, int oldLeft, int oldTop, int oldRight,
                                     int oldBottom) {
              scrollView.scrollTo(0,0);
    
          }
      });

    解决方法三:

       让listView控件失去焦点
       listView.setFocusable(false); 不能获得focus  

问题二:状态问题

当我们使用ListView或GridView的时候,当列表为空的时候,我们需要一个特殊的View来提示用户操作,于是就有了setEmptyView().下面看看如何使用:

参考文献:Android ListView setEmptyView

问题三:多布局问题

问题四:适配器问题

SimpleAdapter

     List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();  
    for (int i = 0; i < name.length; i++) {  
        Map<String, Object> listem = new HashMap<String, Object>();  
        listem.put("head", imageids[i]);  
        listem.put("name", name[i]);  
        listem.put("desc", desc[i]);  
        listems.add(listem);  
    }  

    /*SimpleAdapter的参数说明 
     * 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要 
     * 第二个参数表示生成一个Map(String ,Object)列表选项 
     * 第三个参数表示界面布局的id  表示该文件作为列表项的组件 
     * 第四个参数表示该Map对象的哪些key对应value来生成列表项 
     * 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系 
     * 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源 
     * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
     * 这个head的组件会被name资源覆盖 
     * */  
    SimpleAdapter simplead = new SimpleAdapter(this, listems,  
            R.layout.simple_item, new String[] { "name", "head", "desc" },  
            new int[] {R.id.name,R.id.head,R.id.desc});  

问题五:自定义问题

  • 可被ScrollView嵌套的ListView

  • ListView嵌套ListView的效果

  • 上滑下滑定制阻尼系数效果的ListView

  • 带有排序,单选,多选,侧滑效果的ListView

文献

Last updated

Was this helpful?