• Android-ListView


    1. ListView

    1.1 常用属性

    属性 描述
    fastScrollEnabled 是否显示滚动条
    divider 设置每个Item之间的颜色分割条,也可以是drawable
    dividerHeight 设置分割条高度
    stackFromBottom 列表从底部开始显示

    1.2常用方法:

    添加、移除 HeaderView 和 FooterView

    • addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象
    • addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象
      注意:addHeaderViewView方法必须放在listview.setAdapter()前面,否则会报错

    代码:

    final Button btnTest1=new Button(this);
    btnTest1.setText("Button1");
    
    Button btnTest2=new Button(this);
    btnTest2.setText("Button2");
    
    listView.addHeaderView(btnTest1); //添加
    listView.addFooterView(btnTest2);
    
    btnTest1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listView.removeHeaderView(btnTest1); //移除
        }
    });
    

    1.3.基本使用:ListView不显示点击效果及分割线

    android:listSelector="@android:color/transparent"

    android:divider="@null"

    2. 数据源绑定

    2.1 ArrayAdapter 简单使用

        // ListView ArrayAdapter 简单样式数据源绑定
    	// 1.声明数据源
        final String[] datas={"Item1","Item2","Item3","Item4","Item5",
                "Item6","Item7","Item8","Item9","Item10","Item11","Item12"};
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked,datas);
        /* 系统体统样式:
                 文本样式: simple_list_item_1
           带对号的选中项: simple_list_item_checked
          RadioButton样式: simple_list_item_single_choice
            CheckBox 样式:simple_list_item_multiple_choice
      选中后背景色修改的样式:simple_list_item_activated_1
         */
        // 2.设置数据源
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); //设置选中模式,单选或多选
    
       // 2.获取选中项文本
       long[] selectedItems= listView.getCheckItemIds();
       for (int i = 0; i < selectedItems.length; i++) {
           Toast.makeText(ListViewActivity.this,
                   datas[(int) selectedItems[i]], Toast.LENGTH_SHORT).show();
       }
    

    2.2 SimpleAdapter

        // SimpleAdapter
         List<Map<String,String>> listData=new ArrayList<Map<String,String>>();
         Map<String,String> map1=new HashMap<String,String>();
         map1.put("Id","1");
         map1.put("Name","Mi");
    
         Map<String,String> map2=new HashMap<String,String>();
         map2.put("Id","2");
         map2.put("Name","HuaWei");
    
         listData.add(map1);
         listData.add(map2);
    
         SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), listData,R.layout.item_layout,
                 new String[]{"Id","Name"},new int[]{tv_title, tv_content});
    
         lv.setAdapter(adapter);
    

    2.3 ListVIew优化 通过ViewHolder

    android:layout_height="match_parent"

    两种方式: 复用 ConvertView ,ViewHolder 重用组件

    public class MyArrayAdapter extends ArrayAdapter<News> {
    
        public MyArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<News> objects) {
            super(context, resource, objects);
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_layout, null);
    
                viewHolder = new ViewHolder();
                viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
                viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
                viewHolder.tv_title = (TextView) convertView.findViewById(tv_title);
    
                convertView.setTag(viewHolder);
    
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
    
            News news = getItem(position);
            //赋值
            viewHolder.iv.setImageResource(news.getImageId());
            viewHolder.tv_title.setText(news.getTitle());
            viewHolder.tv_content.setText(news.getContent());
    
            viewHolder.iv.setTag(position);
            //设置图片的点击事件
            viewHolder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Image:" + v.getTag().toString(), Toast.LENGTH_SHORT).show();
                }
            });
    
            //设置 整个 Item的点击事件
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Item", Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }
    
        /**
         * 内部类 ViewHolde 用于对控件的实例进行缓存。
         * 这样的话就没必要每次都通过 findViewById 方法来获取控件实例
         */
        class ViewHolder {
            ImageView iv;
            TextView tv_title;
            TextView tv_content;
        }
    }
    

    3.简单实现上拉记载更多

      listView.setOnScrollListener(new AbsListView.OnScrollListener() {
          int currentCount = 0;
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
              if (currentCount != 0 && currentCount == listView.getCount()) {
                  Toast.makeText(ListViewActivity.this, "Loading......", Toast.LENGTH_SHORT).show();
              }
          }
          @Override
          public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
              currentCount = firstVisibleItem + visibleItemCount;
          }
      });
    
  • 相关阅读:
    (Good Bye 2019) Codeforces 1270B Interesting Subarray
    (Good Bye 2019) Codeforces 1270A Card Game
    Codeforces 1283D Christmas Trees(BFS)
    Codeforces 1283C Friends and Gifts
    Codeforces 1283B Candies Division
    1095 Cars on Campus (30)
    1080 Graduate Admission (30)
    1099 Build A Binary Search Tree (30)
    1018 Public Bike Management (30)
    1087 All Roads Lead to Rome (30)
  • 原文地址:https://www.cnblogs.com/-Tiger/p/8336367.html
Copyright © 2020-2023  润新知