Android ListView常用小技巧汇总

ListView在我们Android项目中的地位是有目共睹的,相信几乎每一个App中都有它的身影。
ListView主要是用列表形式来加载数据,在特定情况下需要实现一些特殊功能:如刷新数据,加载数据,实现动画效果等。
作为我们常用的控件,有哪些需要注意的呢?
**为ListView的每一Item设置分隔线

第一种方法:也是最简单地方法,在布局文件中设置ListView的
divider属性
如:android:divider=”@color/black”
第二种方法:设置android:divider=”@null”表示不要分隔线,然后,自己在Item布局中添加分隔线。

 
    android:id="@+id/test_lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#d1d1d1"
    android:dividerHeight="1px"
    >
 

** 默认情况下,ListView的item有点击效果,怎么样改变这种效果呢?
设置ListViewr的android:listSelector属性,比如可以设置透明取消这种效果android:listSelector=”@android:color/transparent”
也可以设置自己想要的效果,添加对应的color资源或drawble资源 就可以了。
** 当数据比较多的时候,ListView在滚动时候会显示一个默认的滚动条,要取消这个滚动条的话,可以设置android:scrollbars属性
如:android:scrollbars=”none”
** ListView性能优化,必用ViewHolder来充分发挥ListView的recycle机制
** ListView中数据动态变化,添加或删除数据操作,效果显而易见。

//实现ListView数据添加删除
public class MainActivity extends Activity implements View.OnClickListener {
  private ListView test_lv;
  private List dataList;
  private TestAdapter adapter;
  private Button add_btn, del_btn;
  private ImageView emptyIv;
  private LinearLayout operator_ll;

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

  }

  private void initDatas() {
    dataList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      TestBean bean = new TestBean();
      bean.setTitle("标题_" + i);
      bean.setContent("这是内容_" + i);
      bean.setType(i % 2 == 0 ? 1 : 2);
      dataList.add(bean);
    }
  }

  private void initViews() {
    this.test_lv = (ListView) findViewById(R.id.test_lv);
    this.add_btn = (Button) findViewById(R.id.add_btn);
    this.del_btn = (Button) findViewById(R.id.del_btn);
    emptyIv = (ImageView) findViewById(R.id.empty_iv);
    operator_ll=(LinearLayout)findViewById(R.id.operator_ll);
    this.add_btn.setOnClickListener(this);
    this.del_btn.setOnClickListener(this);
    if (dataList.size() == 0) {
      emptyIv.setVisibility(View.VISIBLE);
      operator_ll.setVisibility(View.GONE);
      test_lv.setEmptyView(emptyIv);
    } else {
      emptyIv.setVisibility(View.GONE);
      operator_ll.setVisibility(View.VISIBLE);
      adapter = new TestAdapter(this, dataList);
      test_lv.setAdapter(adapter);
    }
    test_lv.setSelection(15);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.add_btn:
        //默认增加在第一个位置
        TestBean bean = new TestBean();
        bean.setTitle("增加项");
        bean.setContent("这是增加内容");
        bean.setType(1);
        dataList.add(0, bean);
        break;
      case R.id.del_btn:
        //默认删除第一条数据
        dataList.remove(0);
        break;
    }
    adapter.notifyDataSetChanged();//刷新ListView数据
  }
}
//ListView对应Adapter
public class TestAdapter extends BaseAdapter {
  private Context mContext;
  private List listDatas;

  public TestAdapter(Context mContext, List listDatas) {
    this.mContext = mContext;
    this.listDatas = listDatas;
  }

  @Override
  public int getCount() {
    return listDatas.size();
  }

  @Override
  public Object getItem(int position) {
    return listDatas.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null);
      holder.title = (TextView) convertView.findViewById(R.id.item_title);
      holder.content = (TextView) convertView.findViewById(R.id.item_content);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.content.setText(listDatas.get(position).getContent());
    holder.title.setText(listDatas.get(position).getTitle());
    return convertView;
  }

  final class ViewHolder {
    TextView title;
    TextView content;
  }
}

** 类似聊天界面,ListView中有多种item样式效果

//实现ListView中多种item样式
public class TypeActivity extends Activity {
  private ListView type_lv;
  private List dataList;
  private TypeAdapter adapter;

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

  }

  private void initDatas() {
    dataList = new ArrayList<>();
    for (int i = 0; i < 9; i++) {
      TestBean bean = new TestBean();
      bean.setTitle("标题_" + i);
      bean.setContent("这是内容_" + i);
      bean.setType(i % 2 == 0 ? 1 : 2);
      dataList.add(bean);
    }
  }

  private void initViews() {
    this.type_lv = (ListView) findViewById(R.id.type_lv);
    adapter = new TypeAdapter(this, dataList);
    type_lv.setAdapter(adapter);

  }
}
//对应Adapter
public class TypeAdapter extends BaseAdapter {
  private Context mContext;
  private List listDatas;

  public TypeAdapter(Context mContext, List listDatas) {
    this.mContext = mContext;
    this.listDatas = listDatas;
  }

  @Override
  public int getCount() {
    return listDatas.size();
  }

  @Override
  public Object getItem(int position) {
    return listDatas.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    //根据样式设置不同的布局及数据展示
    int type = getItemViewType(position);
    if (convertView == null) {
      holder = new ViewHolder();
      if (type == 1) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null);
        holder.title = (TextView) convertView.findViewById(R.id.item_title);
        holder.content = (TextView) convertView.findViewById(R.id.item_content);
      } else {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.type_item, null);
        holder.title = (TextView) convertView.findViewById(R.id.type_title);
        holder.content = (TextView) convertView.findViewById(R.id.type_title);
      }
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.content.setText(listDatas.get(position).getContent());
    holder.title.setText(listDatas.get(position).getTitle());
    return convertView;
  }
//关键方法之getViewTypeCount:获取有多少种样式
  @Override
  public int getViewTypeCount() {
    return 2;
  }
//关键方法之getItemViewType:获取item类型
  @Override
  public int getItemViewType(int position) {
    return listDatas.get(position).getType();
  }

  final class ViewHolder {
    TextView title;
    TextView content;
  }
}

相关布局

activity_main.xml




  

    

activity_type.xml





  




test_item.xml




 

 

以上就是搜集的Android ListView常用小技巧全部内容,希望对大家的学习有所帮助。

你可能感兴趣的:(Android ListView常用小技巧汇总)