Android-ListView

1. ListView

1.1 經常使用屬性

屬性 描述
fastScrollEnabled 是否顯示滾動條
divider 設置每一個Item之間的顏色分割條,也能夠是drawable
dividerHeight 設置分割條高度
stackFromBottom 列表從底部開始顯示

1.2經常使用方法:

添加、移除 HeaderView 和 FooterViewjava

  • addHeaderView(View v):添加headView(表頭),括號中的參數是一個View對象
  • addFooterView(View v):添加footerView(表尾),括號中的參數是一個View對象
    注意:addHeaderViewView方法必須放在listview.setAdapter()前面,不然會報錯

代碼:android

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"ide

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 重用組件this

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;
      }
  });
相關文章
相關標籤/搜索