Listview

listview相关设置

android:divider="@null" 两个上下相邻item之间的分割置空
android:dividerHeight 这个属性是上下相邻的item间的距离
android:divider="#fffff"  分割线颜色
android:dividerHeight="1dp"  分割线高度

Listview和Scrollview冲突问题
在用到Scrollview中嵌套listview时,listview只能显示一个item的问题
原文网址:
http://blog.csdn.net/wulianghuan/article/details/8627958

public static void setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();
        if(listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for(int i = 0, len = listAdapter.getCount(); i < len; i++) { 
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); 
            totalHeight += listItem.getMeasuredHeight(); 
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

Scrollview显示不在顶端的问题

listview.setFocusable(false);

Scrollview中有其他滑动控件时也一样

通过OnScrollListener实现上拉刷新,下拉加载


package com.example.think.listview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

private List<String> list;
private ListView listView;
private String str = "测试数据";
private int i = 0;
private boolean flage = false;
private Adapter adapter;
private int firstItem;
private int lastItem;
/** * 当前list size **/
private int count;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);

adapter = new Adapter(MainActivity.this);
list = adapter.getList();
for (int a = 0; a <= 4; a++) {
            add();
        }
        add();
listView.setAdapter(adapter);

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
/* * scrollState值: * 当屏幕停止滚动时为SCROLL_STATE_IDLE = 0; * 当屏幕滚动且用户使用的触碰或手指还在屏幕上时为SCROLL_STATE_TOUCH_SCROLL = 1; * 由于用户的操作,屏幕产生惯性滑动时为SCROLL_STATE_FLING = 2 */
                // 当滚动停止且滚动的总数等于数据的总数,上拉加载
if (lastItem == count && scrollState == SCROLL_STATE_IDLE) {
//此处进行加载数据的操作
add();
return;
                }
// 当往下拉操作
if (firstItem == 0 && scrollState == SCROLL_STATE_IDLE) {
                    adda();
                }
            }

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//firstVisibleItem 表示屏幕中第一条数据在adapter中的位置
//visibleItemCount 表示屏幕中最后一条数据在adapter中的位置
//totalItemCount 表示adapter中的总条数
lastItem = firstVisibleItem + visibleItemCount;// 计算出lastitem的值
firstItem = firstVisibleItem;// 同样拿出lastitem的值
}
        });
    }

private void add() {
for (int i = 0; i < 5; i++) {
list.add(str + this.i);
this.i++;
        }
count = list.size();
adapter.notifyDataSetChanged();
    }

private void adda() {
        List<String> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
            list1.add(str + this.i);
this.i++;
        }
for (int a = 0; a < list.size(); a++) {
            list1.add(list.get(a));
        }
list.clear();
for (int a = 0; a < list1.size(); a++) {
list.add(list1.get(a));
        }
count = list.size();
adapter.notifyDataSetChanged();
    }

}


你可能感兴趣的:(android,ListView,scrollview,下拉,上拉加载)