2 wangmingjun2012 wangmingjun2012 于 2014.09.02 09:25 提问

新手请教ListView 刷新问题

这是显示ListView的界面
package com.security.phonepolice.view;

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

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.security.phonepolice.ContantValues;
import com.security.phonepolice.R;
import com.security.phonepolice.entity.Msgbean;
import com.security.phonepolice.http.MyHttpclient;
import com.security.phonepolice.util.SharedPreferenceUtil;
import com.security.phonepolice.view.MessageManager.NotificationAdapter;
import com.security.phonepolice.view.OAFileFragment.FileViewHolder;
import com.security.phonepolice.view.widget.MyListView;
import com.security.phonepolice.view.widget.MyListView.RefreshListener;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OANotifyFragment extends Fragment implements OnClickListener, OnItemClickListener{

private View view;
private ImageView mPreIv;
private MyListView mNotifyLv;
private LayoutInflater mInflater;
private NotifyAdapter adapter = null;
private TextView notifyUpload;
private List<Msgbean> mList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.oa_notify_ui, container, false);
    mInflater = inflater;
    init();
    initEvent();
    return view;
}

private void init() {

    mNotifyLv = (MyListView) view.findViewById(R.id.oa_notify_lv);
}

private void initEvent() {

    mList = new ArrayList<Msgbean>();
    adapter = new NotifyAdapter(mList);
    mNotifyLv.setOnItemClickListener(this);
    mNotifyLv.setAdapter(adapter);
    mNotifyLv.setOnRefreshListener(new RefreshListener() {

        @Override
        public Object refreshing() {
            RequestParams params = new RequestParams();
            params.put("action", "Message");
            String serverAdd = SharedPreferenceUtil.getServerStringSP(getActivity(), "config", "serverAdd");
            MyHttpclient.post(serverAdd+ContantValues.IDCARD_URL, params, new AsyncHttpResponseHandler(){
                @Override
                public void onSuccess(
                        int statusCode,
                        Header[] headers,
                        String content) {
                    super.onSuccess(statusCode, headers, content);
                    if (!content.equals("failed")) {

                        Msgbean msgBean = new Msgbean();
                        try {
                            JSONObject jsonObject=new JSONObject(content);
                            msgBean.setTitle(jsonObject.getString("TITLE"));
                            msgBean.setContent(jsonObject.getString("CONTENT"));
                            Log.i("-----------", jsonObject.getString("TITLE"));
                            List<Msgbean> list = new ArrayList<Msgbean>();
                            list.add(msgBean);
                            mList = list;
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }else {
                        Toast.makeText(getActivity(), "刷新失败", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(
                        Throwable error,
                        String content) {
                    Toast.makeText(getActivity(), "连接失败", Toast.LENGTH_LONG).show();
                    super.onFailure(error, content);
                }
            } );
            return null;
        }

        @Override
        public void refreshed(Object obj) {
            Toast.makeText(getActivity(), "刷新完毕", Toast.LENGTH_LONG).show();
        }

        @Override
        public void more() {
            Toast.makeText(getActivity(), "正在加载", Toast.LENGTH_LONG).show();
        }
    });
}

class NotifyAdapter extends BaseAdapter{

    private List<Msgbean> list;

    public NotifyAdapter(List<Msgbean> list) {
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NotifyViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.oa_item, null);
            viewHolder = new NotifyViewHolder();
            viewHolder.itemIv = (ImageView) convertView.findViewById(R.id.oa_item_iv);
            viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.oa_item_title);
            viewHolder.itemContent = (TextView) convertView.findViewById(R.id.oa_item_content);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (NotifyViewHolder) convertView.getTag();
        }
        viewHolder.itemIv.setImageResource(R.drawable.notification_small);
        viewHolder.itemTitle.setText(list.get(position).getTitle());
        viewHolder.itemContent.setText(list.get(position).getContent());

        return convertView;
    }

}

public static class NotifyViewHolder{
    public ImageView itemIv;
    public TextView itemTitle;
    public TextView itemContent;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = null;
    if (position > 0) {
        //去服务器拉取警情的详细信息
        intent = new Intent();
        intent.putExtra("InfoTag", "OANotify");
        intent.setClass(getActivity(), MessageDetail.class);
        startActivity(intent);
    }

}

}

这是在网上找到的自定义的ListView
package com.security.phonepolice.view.widget;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.security.phonepolice.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**

  • 下拉刷新,底部更多
    *
    */
    public class MyListView extends ListView implements OnScrollListener{

    private float mDownY;
    private float mMoveY;

    private int mHeaderHeight;

    private int mCurrentScrollState;

    private final static int NONE_PULL_REFRESH = 0; //正常状态
    private final static int ENTER_PULL_REFRESH = 1; //进入下拉刷新状态
    private final static int OVER_PULL_REFRESH = 2; //进入松手刷新状态
    private final static int EXIT_PULL_REFRESH = 3; //松手后反弹和加载状态
    private int mPullRefreshState = 0; //记录刷新状态

    private final static int REFRESH_BACKING = 0; //反弹中
    private final static int REFRESH_BACED = 1; //达到刷新界限,反弹结束后
    private final static int REFRESH_RETURN = 2; //没有达到刷新界限,返回
    private final static int REFRESH_DONE = 3; //加载数据结束

    private LinearLayout mHeaderLinearLayout = null;
    private LinearLayout mFooterLinearLayout = null;
    private TextView mHeaderTextView = null;
    private TextView mHeaderUpdateText = null;
    private ImageView mHeaderPullDownImageView = null;
    private ImageView mHeaderReleaseDownImageView = null;
    private ProgressBar mHeaderProgressBar = null;
    private TextView mFooterTextView = null;
    private ProgressBar mFooterProgressBar = null;

    private SimpleDateFormat mSimpleDateFormat;

    private Object mRefreshObject = null;
    private RefreshListener mRefreshListener = null;
    public void setOnRefreshListener(RefreshListener refreshListener) {
    this.mRefreshListener = refreshListener;
    }

    public MyListView(Context context) {
    this(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
    }

    void init(final Context context) {
    mHeaderLinearLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.refresh_list_header, null);
    addHeaderView(mHeaderLinearLayout);
    mHeaderTextView = (TextView) findViewById(R.id.refresh_list_header_text);
    mHeaderUpdateText = (TextView) findViewById(R.id.refresh_list_header_last_update);
    mHeaderPullDownImageView = (ImageView) findViewById(R.id.refresh_list_header_pull_down);
    mHeaderReleaseDownImageView = (ImageView) findViewById(R.id.refresh_list_header_release_up);
    mHeaderProgressBar = (ProgressBar) findViewById(R.id.refresh_list_header_progressbar);

    mFooterLinearLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.refresh_list_footer, null);
    addFooterView(mFooterLinearLayout);
    mFooterProgressBar = (ProgressBar) findViewById(R.id.refresh_list_footer_progressbar);
    mFooterTextView = (TextView) mFooterLinearLayout.findViewById(R.id.refresh_list_footer_text);
    mFooterLinearLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if ("加载更多".equals(mFooterTextView.getText())) {
                mFooterTextView.setText("正在加载");
                mFooterProgressBar.setVisibility(View.VISIBLE);
                if (mRefreshListener != null) {
                    mRefreshListener.more();
                }
            }
        }
    });
    
    setSelection(1);
    setOnScrollListener(this);
    measureView(mHeaderLinearLayout);
    mHeaderHeight = mHeaderLinearLayout.getMeasuredHeight();
    
    mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    mHeaderUpdateText.setText("下拉刷新"+ mSimpleDateFormat.format(new Date()));
    

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
    mDownY = ev.getY();
    break;
    case MotionEvent.ACTION_MOVE:
    mMoveY = ev.getY();
    if (mPullRefreshState == OVER_PULL_REFRESH) {
    mHeaderLinearLayout.setPadding(mHeaderLinearLayout.getPaddingLeft(),
    (int)((mMoveY - mDownY)/3),
    mHeaderLinearLayout.getPaddingRight(),
    mHeaderLinearLayout.getPaddingBottom());
    }
    break;
    case MotionEvent.ACTION_UP:
    //when you action up, it will do these:
    //1. roll back util header topPadding is 0
    //2. hide the header by setSelection(1)
    if (mPullRefreshState == OVER_PULL_REFRESH || mPullRefreshState == ENTER_PULL_REFRESH) {
    new Thread() {
    public void run() {
    Message msg;
    while(mHeaderLinearLayout.getPaddingTop() > 1) {
    msg = mHandler.obtainMessage();
    msg.what = REFRESH_BACKING;
    mHandler.sendMessage(msg);
    try {
    sleep(5);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    msg = mHandler.obtainMessage();
    if (mPullRefreshState == OVER_PULL_REFRESH) {
    msg.what = REFRESH_BACED;
    } else {
    msg.what = REFRESH_RETURN;
    }
    mHandler.sendMessage(msg);
    };
    }.start();
    }
    break;
    }
    return super.onTouchEvent(ev);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
    && firstVisibleItem == 0
    && (mHeaderLinearLayout.getBottom() >= 0 && mHeaderLinearLayout.getBottom() < mHeaderHeight)) {
    //进入且仅进入下拉刷新状态
    if (mPullRefreshState == NONE_PULL_REFRESH) {
    mPullRefreshState = ENTER_PULL_REFRESH;
    }
    } else if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
    && firstVisibleItem == 0
    && (mHeaderLinearLayout.getBottom() >= mHeaderHeight)) {
    //下拉达到界限,进入松手刷新状态
    if (mPullRefreshState == ENTER_PULL_REFRESH || mPullRefreshState == NONE_PULL_REFRESH) {
    mPullRefreshState = OVER_PULL_REFRESH;
    mDownY = mMoveY; //为下拉1/3折扣效果记录开始位置
    mHeaderTextView.setText("松手刷新");//显示松手刷新
    mHeaderPullDownImageView.setVisibility(View.GONE);//隐藏"下拉刷新"
    mHeaderReleaseDownImageView.setVisibility(View.VISIBLE);//显示向上的箭头
    }
    } else if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL && firstVisibleItem != 0) {
    //不刷新了
    if (mPullRefreshState == ENTER_PULL_REFRESH) {
    mPullRefreshState = NONE_PULL_REFRESH;
    }
    } else if (mCurrentScrollState == SCROLL_STATE_FLING && firstVisibleItem == 0) {
    //飞滑状态,不能显示出header,也不能影响正常的飞滑
    //只在正常情况下才纠正位置
    if (mPullRefreshState == NONE_PULL_REFRESH) {
    setSelection(1);
    }
    }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    mCurrentScrollState = scrollState;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
    super.setAdapter(adapter);
    setSelection(1);
    }

    private void measureView(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
    p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_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);
    

    }

    private Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case REFRESH_BACKING:
    mHeaderLinearLayout.setPadding(mHeaderLinearLayout.getPaddingLeft(),
    (int) (mHeaderLinearLayout.getPaddingTop()*0.75f),
    mHeaderLinearLayout.getPaddingRight(),
    mHeaderLinearLayout.getPaddingBottom());
    break;
    case REFRESH_BACED:
    mHeaderTextView.setText("正在加载...");
    mHeaderProgressBar.setVisibility(View.VISIBLE);
    mHeaderPullDownImageView.setVisibility(View.GONE);
    mHeaderReleaseDownImageView.setVisibility(View.GONE);
    mPullRefreshState = EXIT_PULL_REFRESH;
    new Thread() {
    public void run() {
    if (mRefreshListener != null) {
    mRefreshObject = mRefreshListener.refreshing();
    }
    Message msg = mHandler.obtainMessage();
    msg.what = REFRESH_DONE;
    mHandler.sendMessage(msg);
    };
    }.start();
    break;
    case REFRESH_RETURN:
    mHeaderTextView.setText("下拉刷新");
    mHeaderProgressBar.setVisibility(View.INVISIBLE);
    mHeaderPullDownImageView.setVisibility(View.VISIBLE);
    mHeaderReleaseDownImageView.setVisibility(View.GONE);
    mHeaderLinearLayout.setPadding(mHeaderLinearLayout.getPaddingLeft(),
    0,
    mHeaderLinearLayout.getPaddingRight(),
    mHeaderLinearLayout.getPaddingBottom());
    mPullRefreshState = NONE_PULL_REFRESH;
    setSelection(1);
    break;
    case REFRESH_DONE:
    mHeaderTextView.setText("下拉刷新");
    mHeaderProgressBar.setVisibility(View.INVISIBLE);
    mHeaderPullDownImageView.setVisibility(View.VISIBLE);
    mHeaderReleaseDownImageView.setVisibility(View.GONE);
    mHeaderUpdateText.setText("最新更新"+
    mSimpleDateFormat.format(new Date()));
    mHeaderLinearLayout.setPadding(mHeaderLinearLayout.getPaddingLeft(),
    0,
    mHeaderLinearLayout.getPaddingRight(),
    mHeaderLinearLayout.getPaddingBottom());
    mPullRefreshState = NONE_PULL_REFRESH;
    setSelection(1);
    if (mRefreshListener != null) {
    mRefreshListener.refreshed(mRefreshObject);
    }
    break;
    default:
    break;
    }
    }
    };
    public interface RefreshListener {
    Object refreshing();
    void refreshed(Object obj);
    void more();
    }

    public void finishFootView() {
    mFooterProgressBar.setVisibility(View.GONE);
    mFooterTextView.setText("更多");
    }

    public void addFootView() {
    if (getFooterViewsCount() == 0) {
    addFooterView(mFooterLinearLayout);
    }
    }

    public void removeFootView() {
    removeFooterView(mFooterLinearLayout);
    }
    }
    这是实体类:
    package com.security.phonepolice.entity;

public class Msgbean {

private int id;
private String title;
private String content;
private String relName;
private String relTime;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
public String getRelName() {
    return relName;
}
public void setRelName(String relName) {
    this.relName = relName;
}
public String getRelTime() {
    return relTime;
}
public void setRelTime(String relTime) {
    this.relTime = relTime;
}

}
走到这一步法mNotifyLv.setOnRefreshListener(new RefreshListener() 就停了
为什么刷新的时候就是不走接口中的refreshing()方法。求大神指教啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!