天妒小时光 2015-01-26 09:06 采纳率: 11.1%
浏览 2884

项目能够运行,但一直就在那加载却加载不出,求教

package caifu.life.ZHSQ.main;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import caifu.life.ZHSQ.R;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView.OnLoadListener;
import caifu.life.ZHSQ.sqShouYeListView.AutoListView.OnRefreshListener;

public class ShouYeActivity extends Activity implements OnRefreshListener,
OnLoadListener {
// 获取listview
private AutoListView syListView;
private listViewAdapter adapter;
private ArrayList> item;

// 传递消息处理刷新数据
private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        @SuppressWarnings("unchecked")
        ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) msg.obj;
        switch (msg.what) {
        case AutoListView.REFRESH:// 刷新操作
            syListView.onRefreshComplete();
            item.clear();
            item.addAll(result);
            break;
        case AutoListView.LOAD:// 加载
            syListView.onLoadComplete();
            item.addAll(result);
            break;
        }
        syListView.setResultSize(result.size());
        adapter.notifyDataSetChanged();
    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sq_message);
    // 获取listview
    syListView = (AutoListView) findViewById(R.id.shouyeListView);
    // 配置适配器
    adapter = new listViewAdapter(this, item);
    // 设置适配
    syListView.setAdapter(adapter);
    // 追加刷新与加载监听
    syListView.setOnRefreshListener(this);
    syListView.setOnLoadListener(this);
    initData();
    // 一个动态数组
    getDate();
}

// 定义一个得到数据的方法
private static ArrayList<HashMap<String, Object>> getDate() {
    ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();
    for (int i = 1; i < 4; i++) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("touXiang", R.drawable.sylvtouxiang);
        map.put("yongHuMing", "第" + i + "用户");
        map.put("neiRong", "第" + i + "内容");
        map.put("sylv_image1", R.drawable.sylvimage1);
        map.put("sylv_image2", R.drawable.sylvimage2);
        map.put("sylv_image3", R.drawable.sylvimage3);
        item.add(map);
    }
    return item;
}

private void initData() {
    loadData(AutoListView.REFRESH);
}

private void loadData(final int what) {
    // 起线程获取数据
    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Message msg = handler.obtainMessage();
                msg.what = what;
                msg.obj = getDate();
                handler.sendMessage(msg);
            }
        }
    }).start();
}

public void onLoad() {
    loadData(AutoListView.LOAD);
}

public void onRefresh() {
    loadData(AutoListView.REFRESH);
}

// 适配器
private class listViewAdapter extends BaseAdapter {
    private ViewHolder holder;
    private Context context;
    private ArrayList<HashMap<String, Object>> item;

    public listViewAdapter(Context context,
            ArrayList<HashMap<String, Object>> item) {
        this.item = item;
        this.context = context;
    }

    public int getCount() {
        return getDate().size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.activity_sq_message_item, null);
            // 得到各控件对象
            holder.touXiang = (ImageView) convertView
                    .findViewById(R.id.touXiang);
            holder.yongHuMing = (TextView) convertView
                    .findViewById(R.id.yongHuMing);
            holder.neiRong = (TextView) convertView
                    .findViewById(R.id.neiRong);
            holder.sylv_image1 = (ImageView) convertView
                    .findViewById(R.id.sylv_image1);
            holder.sylv_image2 = (ImageView) convertView
                    .findViewById(R.id.sylv_image2);
            holder.sylv_image3 = (ImageView) convertView
                    .findViewById(R.id.sylv_image3);
            // 绑定viewHolder对象
            convertView.setTag(holder);
        } else {
            // 取出ViewHolder对象
            holder = (ViewHolder) convertView.getTag();
        }
        holder.yongHuMing.setText(getDate().get(position).get("yongHuMing")
                .toString());
        holder.neiRong.setText(getDate().get(position).get("neiRong")
                .toString());
        return convertView;
    }

    public final class ViewHolder {
        public ImageView touXiang;
        public TextView yongHuMing;
        public TextView neiRong;
        public ImageView sylv_image1;
        public ImageView sylv_image2;
        public ImageView sylv_image3;
    }

}

}


  • 写回答

1条回答 默认 最新

  • 天妒小时光 2015-01-26 09:06
    关注
     package caifu.life.ZHSQ.sqShouYeListView;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.animation.LinearInterpolator;
    import android.view.animation.RotateAnimation;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import caifu.life.ZHSQ.R;
    
    
    public class AutoListView extends ListView implements OnScrollListener {
    
        // 区分当前操作是刷新还是加载
        public static final int REFRESH = 0;
        public static final int LOAD = 1;
    
        // 区分PULL和RELEASE的距离的大小
        private static final int SPACE = 20;
    
        // 定义header的四种状态和当前状态
        private static final int NONE = 0;
        private static final int PULL = 1;
        private static final int RELEASE = 2;
        private static final int REFRESHING = 3;
        private int state;
    
        private LayoutInflater inflater;
        private View header;
        private View footer;
        private TextView tip;
        private TextView lastUpdate;
        private ImageView arrow;       //下拉箭头
        private ProgressBar refreshing;//头部进度条
    
        private TextView noData;   //无数据
        private TextView loadFull; //加载完毕
        private TextView more;     //还可以继续加载
        private ProgressBar loading;//底部进度条
    
        private RotateAnimation animation;
        private RotateAnimation reverseAnimation;
    
        private int startY;
    
        private int firstVisibleItem;
        private int scrollState;
        private int headerContentInitialHeight;
        private int headerContentHeight;
    
        // 只有在listview第一个item显示的时候(listview滑到了顶部)才进行下拉刷新,
        //否则此时的下拉只是滑动listview
        private boolean isRecorded;
        private boolean isLoading;// 判断是否正在加载
        private boolean loadEnable = true;// 开启或者关闭加载更多功能
        private boolean isLoadFull;
        private int pageSize = 10;
    
        private OnRefreshListener onRefreshListener;
        private OnLoadListener onLoadListener;
    
        public AutoListView(Context context) {
            super(context);
            initView(context);
        }
    
        public AutoListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context);
        }
    
        public AutoListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initView(context);
        }
    
        // 下拉刷新监听
        public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
            this.onRefreshListener = onRefreshListener;
        }
    
        // 加载更多监听
        public void setOnLoadListener(OnLoadListener onLoadListener) {
            this.loadEnable = true;
            this.onLoadListener = onLoadListener;
        }
    
        public boolean isLoadEnable() {
            return loadEnable;
        }
    
        // 这里的开启或者关闭加载更多,并不支持动态调整
        public void setLoadEnable(boolean loadEnable) {
            this.loadEnable = loadEnable;
            this.removeFooterView(footer);
        }
    
        public int getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
    
        // 初始化组件
        private void initView(Context context) {
    
            // 设置箭头特效
            animation = new RotateAnimation(0, -180,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            animation.setInterpolator(new LinearInterpolator());
            animation.setDuration(100);
            animation.setFillAfter(true);
    
            reverseAnimation = new RotateAnimation(-180, 0,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            reverseAnimation.setInterpolator(new LinearInterpolator());
            reverseAnimation.setDuration(100);
            reverseAnimation.setFillAfter(true);
    
            inflater = LayoutInflater.from(context);
            //头部,加载更多的操作
            footer = inflater.inflate(R.layout.activity_sq_message_footer, null);
            loadFull = (TextView) footer.findViewById(R.id.loadFull);
            noData = (TextView) footer.findViewById(R.id.noData);
            more = (TextView) footer.findViewById(R.id.more);
            loading = (ProgressBar) footer.findViewById(R.id.loading);
            //尾部,可进行下拉刷新
            header = inflater.inflate(R.layout.activity_sq_message_header, null);
            arrow = (ImageView) header.findViewById(R.id.arrow);//箭头图片
            tip = (TextView) header.findViewById(R.id.tip);//松开或下拉
            lastUpdate = (TextView) header.findViewById(R.id.lastUpdate);//时间
            refreshing = (ProgressBar) header.findViewById(R.id.refreshing);//进度条
    
            // 为listview添加头部和尾部,并进行初始化
            headerContentInitialHeight = header.getPaddingTop();
            measureView(header);
            headerContentHeight = header.getMeasuredHeight();
            topPadding(-headerContentHeight);
            this.addHeaderView(header);
            this.addFooterView(footer);
            this.setOnScrollListener(this);
        }
    
        public void onRefresh() {
            if (onRefreshListener != null) {
                onRefreshListener.onRefresh();
            }
        }
    
        public void onLoad() {
            if (onLoadListener != null) {
                onLoadListener.onLoad();
            }
        }
    
        public void onRefreshComplete(String updateTime) {
            lastUpdate.setText(this.getContext().getString(R.string.lastUpdateTime,
                    xiaLaShuaXinUtilTime.getCurrentTime()));
            state = NONE;
            refreshHeaderViewByState();
        }
    
        // 用于下拉刷新结束后的回调
        public void onRefreshComplete() {
            String currentTime = xiaLaShuaXinUtilTime.getCurrentTime();
            onRefreshComplete(currentTime);
        }
    
        // 用于加载更多结束后的回调
        public void onLoadComplete() {
            isLoading = false;
        }
    
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            this.firstVisibleItem = firstVisibleItem;
        }
    
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            this.scrollState = scrollState;
            ifNeedLoad(view, scrollState);
        }
    
        // 根据listview滑动的状态判断是否需要加载更多
        private void ifNeedLoad(AbsListView view, int scrollState) {
            if (!loadEnable) {
                return;
            }
            try {
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                        && !isLoading
                        && view.getLastVisiblePosition() == view
                        .getPositionForView(footer) && !isLoadFull) {
                    onLoad();
                    isLoading = true;
                }
            } catch (Exception e) {
            }
        }
    
        /**
         * 监听触摸事件,解读手势
         */
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (firstVisibleItem == 0) {
                    isRecorded = true;
                    startY = (int) ev.getY();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (state == PULL) {
                    state = NONE;
                    refreshHeaderViewByState();
                } else if (state == RELEASE) {
                    state = REFRESHING;
                    refreshHeaderViewByState();
                    onRefresh();
                }
                isRecorded = false;
                break;
            case MotionEvent.ACTION_MOVE:
                whenMove(ev);
                break;
            }
            return super.onTouchEvent(ev);
        }
    
        // 解读手势,刷新header状态
        private void whenMove(MotionEvent ev) {
            if (!isRecorded) {
                return;
            }
            int tmpY = (int) ev.getY();
            int space = tmpY - startY;
            int topPadding = space - headerContentHeight;
            switch (state) {
            case NONE:
                if (space > 0) {
                    state = PULL;
                    refreshHeaderViewByState();
                }
                break;
            case PULL:
                topPadding(topPadding);
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL
                        && space > headerContentHeight + SPACE) {
                    state = RELEASE;
                    refreshHeaderViewByState();
                }
                break;
            case RELEASE:
                topPadding(topPadding);
                if (space > 0 && space < headerContentHeight + SPACE) {
                    state = PULL;
                    refreshHeaderViewByState();
                } else if (space <= 0) {
                    state = NONE;
                    refreshHeaderViewByState();
                }
                break;
            }
    
        }
    
        // 调整header的大小。其实调整的只是距离顶部的高度。
        private void topPadding(int topPadding) {
            header.setPadding(header.getPaddingLeft(), topPadding,header.getPaddingRight(), header.getPaddingBottom());
            header.invalidate();
        }
        /**
         * 这个方法是根据结果的大小来决定footer显示的。
         * 这里假定每次请求的条数为10。如果请求到了10条。则认为还有数据。如过结果不足10条,则认为数据已经全部加载,这时footer显示已经全部加载
         */
        public void setResultSize(int resultSize) {
            if (resultSize == 0) {
                isLoadFull = true;
                loadFull.setVisibility(View.GONE);
                loading.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                noData.setVisibility(View.VISIBLE);
            } else if (resultSize > 0 && resultSize < pageSize) {
                isLoadFull = true;
                loadFull.setVisibility(View.VISIBLE);
                loading.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                noData.setVisibility(View.GONE);
            } else if (resultSize == pageSize) {
                isLoadFull = false;
                loadFull.setVisibility(View.GONE);
                loading.setVisibility(View.VISIBLE);
                more.setVisibility(View.VISIBLE);
                noData.setVisibility(View.GONE);
            }
    
        }
    
        // 根据当前状态,调整header
        private void refreshHeaderViewByState() {
            switch (state) {
            case NONE:
                topPadding(-headerContentHeight);
                tip.setText(R.string.pull_to_refresh);
                refreshing.setVisibility(View.GONE);
                arrow.clearAnimation();
                arrow.setImageResource(R.drawable.pull_to_refresh_arrow);
                break;
            case PULL://下拉
                arrow.setVisibility(View.VISIBLE);
                tip.setVisibility(View.VISIBLE);
                lastUpdate.setVisibility(View.VISIBLE);
                refreshing.setVisibility(View.GONE);
                tip.setText(R.string.pull_to_refresh);
                arrow.clearAnimation();
                arrow.setAnimation(reverseAnimation);
                break;
            case RELEASE://释放,松开
                arrow.setVisibility(View.VISIBLE);
                tip.setVisibility(View.VISIBLE);
                lastUpdate.setVisibility(View.VISIBLE);
                refreshing.setVisibility(View.GONE);
                tip.setText(R.string.release_to_refresh);
                arrow.clearAnimation();
                arrow.setAnimation(animation);
                break;
            case REFRESHING://刷新
                topPadding(headerContentInitialHeight);
                refreshing.setVisibility(View.VISIBLE);
                arrow.clearAnimation();
                arrow.setVisibility(View.GONE);
                //tip.setVisibility(View.GONE);
                tip.setText(R.string.release_to_refresh);
                lastUpdate.setVisibility(View.VISIBLE);
                //lastUpdate.setVisibility(View.GONE);
                break;
            }
        }
    
        // 用来计算header大小的。比较隐晦。因为header的初始高度就是0,貌似可以不用。
        private void measureView(View child) {
            ViewGroup.LayoutParams p = child.getLayoutParams();
            if (p == null) {
                p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
            int lpHeight = p.height;
            int childHeightSpec;
            if (lpHeight > 0) {
                childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
                        MeasureSpec.EXACTLY);
            } else {
                childHeightSpec = MeasureSpec.makeMeasureSpec(0,
                        MeasureSpec.UNSPECIFIED);
            }
            child.measure(childWidthSpec, childHeightSpec);
        }
    
        /*
         * 定义下拉刷新接口
         */
        public interface OnRefreshListener {
            public void onRefresh();
        }
    
        /*
         * 定义加载更多接口
         */
        public interface OnLoadListener {
            public void onLoad();
        }
    
    }
    这是listview版块
    
    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件