android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)
1、因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为 2,为了使LISTVIEW的ITEM在点击时索引不变,实现以下方法
2、实现接口android.widget.AdapterView.OnItemClickListener
3、重写setOnItemClickListener onItemClick方法
private OnItemClickListener mItemClickListener;
// 重写item点击方法
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener listener) {
mItemClickListener = listener;
super.setOnItemClickListener(this);// 将点击事件设置给当前的RefreshListView
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(parent, view, position
- getHeaderViewsCount(), id);
}
}
import java.text.SimpleDateFormat; import java.util.Date; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import com.itheima.zhsh66.R; /** * 下拉刷新的listview * */ public class RefreshListView extends ListView implements OnScrollListener, android.widget.AdapterView.OnItemClickListener { private static final int STATE_PULL_TO_REFRESH = 1;// 下拉刷新 private static final int STATE_RELEASE_TO_REFRESH = 2;// 松开刷新 private static final int STATE_REFRESHING = 3;// 正在刷新 // 下拉刷新头布局 private View mHeaderView; // 头布局高度 private int mHeaderViewHeight; // 脚布局 private View mFooterView; private int mFooterViewHeight; private int startY = -1; // 当前下拉刷新的状态 private int mCurrentState = STATE_PULL_TO_REFRESH;// 默认是下拉刷新 private TextView tvTitle; private ImageView ivArrow; private ProgressBar pbLoading; private TextView tvTime; private RotateAnimation animUp;// 箭头向上动画 private RotateAnimation animDown;// 箭头向下动画 private boolean isLoadingMore;// 标记是否正在加载更多 public RefreshListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initHeaderView(); initFooterView(); } public RefreshListView(Context context, AttributeSet attrs) { super(context, attrs); initHeaderView(); initFooterView(); } public RefreshListView(Context context) { super(context); initHeaderView(); initFooterView(); } /** * 初始化头布局 */ private void initHeaderView() { mHeaderView = View.inflate(getContext(), R.layout.list_refresh_header, null); this.addHeaderView(mHeaderView);// 添加头布局 // 隐藏头布局(1, 获取头布局高度, 2.设置负paddingTop,布局就会往上走) // int height = mHeaderView.getHeight();//此处无法获取高度,因为布局还没有绘制完成 // 绘制之前就要获取布局高度 mHeaderView.measure(0, 0);// 手动测量布局 mHeaderViewHeight = mHeaderView.getMeasuredHeight();// 测量之后的高度 // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); tvTitle = (TextView) mHeaderView.findViewById(R.id.tv_title); ivArrow = (ImageView) mHeaderView.findViewById(R.id.iv_arrow); pbLoading = (ProgressBar) mHeaderView.findViewById(R.id.pb_loading); tvTime = (TextView) mHeaderView.findViewById(R.id.tv_time); initAnim(); setCurrentTime();// 设置初始时间 } /** * 初始化脚布局 */ private void initFooterView() { mFooterView = View.inflate(getContext(), R.layout.list_refresh_footer, null); this.addFooterView(mFooterView); mFooterView.measure(0, 0); mFooterViewHeight = mFooterView.getMeasuredHeight(); // 隐藏脚布局 mFooterView.setPadding(0, -mFooterViewHeight, 0, 0); // 设置滑动监听 this.setOnScrollListener(this); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: startY = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE: if (startY == -1) {// 如果用户按住头条新闻向下滑动, 会导致listview无法拿到ACTION_DOWN, // 此时要重新获取startY startY = (int) ev.getY(); } // 如果当前正在刷新, 什么都不做了 if (mCurrentState == STATE_REFRESHING) { break; } int endY = (int) ev.getY(); int dy = endY - startY; if (dy > 0 && getFirstVisiblePosition() == 0) {// 向下滑动&当前显示的是第一个item,才允许下拉刷新 int paddingTop = dy - mHeaderViewHeight;// 计算当前的paddingtop值 // 根据padding切换状态 if (paddingTop >= 0 && mCurrentState != STATE_RELEASE_TO_REFRESH) { // 切换到松开刷新 mCurrentState = STATE_RELEASE_TO_REFRESH; refreshState(); } else if (paddingTop < 0 && mCurrentState != STATE_PULL_TO_REFRESH) { // 切换到下拉刷新 mCurrentState = STATE_PULL_TO_REFRESH; refreshState(); } mHeaderView.setPadding(0, paddingTop, 0, 0);// 重新设置头布局padding return true; } break; case MotionEvent.ACTION_UP: startY = -1;// 起始坐标归零 if (mCurrentState == STATE_RELEASE_TO_REFRESH) { // 如果当前是松开刷新, 就要切换为正在刷新 mCurrentState = STATE_REFRESHING; // 显示头布局 mHeaderView.setPadding(0, 0, 0, 0); refreshState(); // 下拉刷新回调 if (mListener != null) { mListener.onRefresh(); } } else if (mCurrentState == STATE_PULL_TO_REFRESH) { // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); } break; default: break; } return super.onTouchEvent(ev); } /** * 初始化箭头动画 */ private void initAnim() { animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animUp.setDuration(500); animUp.setFillAfter(true);// 保持状态 animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animDown.setDuration(500); animDown.setFillAfter(true);// 保持状态 } /** * 根据当前状态刷新界面 */ private void refreshState() { switch (mCurrentState) { case STATE_PULL_TO_REFRESH: tvTitle.setText("下拉刷新"); // 箭头向下移动 ivArrow.startAnimation(animDown); // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); break; case STATE_RELEASE_TO_REFRESH: tvTitle.setText("松开刷新"); // 箭头向上移动 ivArrow.startAnimation(animUp); // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); break; case STATE_REFRESHING: tvTitle.setText("正在刷新..."); pbLoading.setVisibility(View.VISIBLE); ivArrow.clearAnimation();// 必须清除动画,才能隐藏控件 ivArrow.setVisibility(View.INVISIBLE); break; default: break; } } private OnRefreshListener mListener; public void setOnRefreshListener(OnRefreshListener listener) { mListener = listener; } /** * 设置上次刷新时间 */ private void setCurrentTime() { // 08:10 8:10 1 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH表示24小时制 String time = format.format(new Date()); tvTime.setText(time); } // 刷新完成 public void onRefreshComplete(boolean success) { if (!isLoadingMore) { // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); mCurrentState = STATE_PULL_TO_REFRESH; // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); tvTitle.setText("下拉刷新"); // 刷新失败,不需要更新时间 if (success) { setCurrentTime(); } } else { // 隐藏脚布局 mFooterView.setPadding(0, -mFooterViewHeight, 0, 0); isLoadingMore = false; } } public interface OnRefreshListener { // 下拉刷新的回调 public void onRefresh(); // 加载更多的回调 public void loadMore(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { int lastVisiblePosition = getLastVisiblePosition();// 当前界面显示的最后一个item的位置 if (lastVisiblePosition >= getCount() - 1 && !isLoadingMore) { isLoadingMore = true; // System.out.println("到底了"); // 加载更多了....(到底了) // 显示脚布局 mFooterView.setPadding(0, 0, 0, 0); // listview设置当前要展示的item的位置 setSelection(getCount() - 1);// 跳到加载更多item的位置去展示 if (mListener != null) { mListener.loadMore(); } } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } private OnItemClickListener mItemClickListener; // 重写item点击方法 @Override public void setOnItemClickListener( android.widget.AdapterView.OnItemClickListener listener) { mItemClickListener = listener; super.setOnItemClickListener(this);// 将点击事件设置给当前的RefreshListView } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mItemClickListener != null) { mItemClickListener.onItemClick(parent, view, position - getHeaderViewsCount(), id); } } }
import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.itheima.zhsh66.NewsDetailActivity; import com.itheima.zhsh66.R; import com.itheima.zhsh66.base.BaseMenuDetailPager; import com.itheima.zhsh66.domain.NewsData; import com.itheima.zhsh66.domain.NewsData.News; import com.itheima.zhsh66.domain.NewsData.TopNews; import com.itheima.zhsh66.domain.NewsMenuData.NewsTabData; import com.itheima.zhsh66.global.Constants; import com.itheima.zhsh66.utils.CacheUtils; import com.itheima.zhsh66.utils.PrefUtils; import com.itheima.zhsh66.view.HorizontalScrollViewPager; import com.itheima.zhsh66.view.RefreshListView; import com.itheima.zhsh66.view.RefreshListView.OnRefreshListener; import com.lidroid.xutils.BitmapUtils; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; import com.lidroid.xutils.view.annotation.ViewInject; import com.viewpagerindicator.CirclePageIndicator; /** * 12个页签的页面对象 * */ public class TabDetailPager extends BaseMenuDetailPager { // 页签分类的网络信息 private NewsTabData mTabData; // 网络返回的新闻列表数据 private NewsData mNewsTabData; // 加载新闻列表的url private String mUrl; // 头条新闻的网络数据 private ArrayList<TopNews> mTopNewsList; // 头条新闻的数据适配器 private TopNewsAdapter mTopNewsAdapter; // 新闻列表的集合 private ArrayList<News> mNewsList; private NewsAdapter mNewsAdapter; @ViewInject(R.id.vp_tab_detail) private HorizontalScrollViewPager mViewPager; @ViewInject(R.id.lv_tab_detail) private RefreshListView lvList; @ViewInject(R.id.indicator) private CirclePageIndicator mIndicator; @ViewInject(R.id.tv_title) private TextView tvTopNewsTitle; private String mMoreUrl;// 下一页的链接 private Handler mHandler = null; public TabDetailPager(Activity activity, NewsTabData tabData) { super(activity); mTabData = tabData; mUrl = Constants.SERVER_URL + mTabData.url; } @Override public View initView() { View view = View.inflate(mActivity, R.layout.pager_tab_detail, null); ViewUtils.inject(this, view); View header = View.inflate(mActivity, R.layout.list_header_topnews, null); ViewUtils.inject(this, header);// 必须也将头布局注入到ViewUtils // 给listview添加头布局 lvList.addHeaderView(header); // 设置下拉刷新监听 lvList.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // 从网络加载数据 getDataFromServer(); } @Override public void loadMore() { // 加载更多数据 if (mMoreUrl != null) { System.out.println("加载下一页数据..."); getMoreDataFromServer(); } else { lvList.onRefreshComplete(true);// 收起加载更多布局 Toast.makeText(mActivity, "没有更多数据了", Toast.LENGTH_SHORT) .show(); } } }); lvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { System.out.println("pos:" + position); News news = mNewsList.get(position); // 当前点击的item的标题颜色置灰 TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); tvTitle.setTextColor(Color.GRAY); // 将已读状态持久化到本地 // key: read_ids; value: 1324,1325,1326 String readIds = PrefUtils.getString("read_ids", "", mActivity); if (!readIds.contains(news.id)) {// 以前没有添加过,才添加进来 readIds = readIds + news.id + ",";// 1324,1325, PrefUtils.putString("read_ids", readIds, mActivity); } // 跳到详情页 Intent intent = new Intent(mActivity, NewsDetailActivity.class); intent.putExtra("url", news.url); mActivity.startActivity(intent); } }); return view; } @Override public void initData() { String cache = CacheUtils.getCache(mUrl, mActivity); if (!TextUtils.isEmpty(cache)) { processResult(cache, false); } getDataFromServer(); } private void getDataFromServer() { HttpUtils utils = new HttpUtils(); utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { String result = responseInfo.result; processResult(result, false); System.out.println("访问网络成功!!!"); CacheUtils.setCache(mUrl, result, mActivity); // 收起下拉刷新控件 lvList.onRefreshComplete(true); } @Override public void onFailure(HttpException error, String msg) { // 收起下拉刷新控件 lvList.onRefreshComplete(false); error.printStackTrace(); Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show(); } }); } /** * 加载更多数据 */ protected void getMoreDataFromServer() { HttpUtils utils = new HttpUtils(); utils.send(HttpMethod.GET, mMoreUrl, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { String result = responseInfo.result; processResult(result, true); // 收起加载更多布局 lvList.onRefreshComplete(true); } @Override public void onFailure(HttpException error, String msg) { error.printStackTrace(); Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show(); // 收起加载更多布局 lvList.onRefreshComplete(false); } }); } protected void processResult(String result, boolean isMore) { Gson gson = new Gson(); mNewsTabData = gson.fromJson(result, NewsData.class); if (!TextUtils.isEmpty(mNewsTabData.data.more)) { // 初始化下一页链接地址 mMoreUrl = Constants.SERVER_URL + mNewsTabData.data.more; } else { // 没有下一页了 mMoreUrl = null; } if (!isMore) { // 初始化头条新闻 mTopNewsList = mNewsTabData.data.topnews; if (mTopNewsList != null) { mTopNewsAdapter = new TopNewsAdapter(); mViewPager.setAdapter(mTopNewsAdapter); mIndicator.setViewPager(mViewPager);// 将指示器和viewpager绑定 mIndicator.setSnap(true);// 快照模式 mIndicator.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { //System.out.println("position:" + position); TopNews topNews = mTopNewsList.get(position); tvTopNewsTitle.setText(topNews.title); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); mIndicator.onPageSelected(0);// 将小圆点位置归零, 解决它会在页面销毁时仍记录上次位置的bug tvTopNewsTitle.setText(mTopNewsList.get(0).title);// 初始化第一页标题 } // 初始化新闻列表 mNewsList = mNewsTabData.data.news; if (mNewsList != null) { mNewsAdapter = new NewsAdapter(); lvList.setAdapter(mNewsAdapter); } if (mHandler == null) { mHandler = new Handler() { public void handleMessage(android.os.Message msg) { int currentItem = mViewPager.getCurrentItem(); if (currentItem < mTopNewsList.size() - 1) { currentItem++; } else { currentItem = 0; } mViewPager.setCurrentItem(currentItem); mHandler.sendEmptyMessageDelayed(0, 2000); }; }; // 延时2秒切换广告条 mHandler.sendEmptyMessageDelayed(0, 2000); mViewPager.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("ACTION_DOWN"); // 删除所有消息 mHandler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_CANCEL:// 事件取消(当按下后,然后移动下拉刷新,导致抬起后无法响应ACTION_UP, // 但此时会响应ACTION_CANCEL,也需要继续播放轮播条) case MotionEvent.ACTION_UP: // 延时2秒切换广告条 mHandler.sendEmptyMessageDelayed(0, 2000); break; default: break; } return false; } }); } } else { // 加载更多 ArrayList<News> moreData = mNewsTabData.data.news; mNewsList.addAll(moreData);// 追加数据 mNewsAdapter.notifyDataSetChanged();// 刷新listview } } class TopNewsAdapter extends PagerAdapter { BitmapUtils mBitmapUtils; public TopNewsAdapter() { // 初始化xutils中的加载图片的工具 mBitmapUtils = new BitmapUtils(mActivity); // 设置默认加载图片 mBitmapUtils .configDefaultLoadingImage(R.drawable.topnews_item_default); } @Override public int getCount() { return mTopNewsList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView view = new ImageView(mActivity); view.setScaleType(ScaleType.FIT_XY);// 设置图片填充效果, 表示填充父窗体 // 获取图片链接, 使用链接下载图片, 将图片设置给ImageView, 考虑内存溢出问题, 图片本地缓存 mBitmapUtils.display(view, mTopNewsList.get(position).topimage); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } class NewsAdapter extends BaseAdapter { public BitmapUtils mBitmapUtils; public NewsAdapter() { mBitmapUtils = new BitmapUtils(mActivity); mBitmapUtils .configDefaultLoadingImage(R.drawable.pic_item_list_default); } @Override public int getCount() { return mNewsList.size(); } @Override public News getItem(int position) { return mNewsList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(mActivity, R.layout.list_item_news, null); holder = new ViewHolder(); holder.tvTitle = (TextView) convertView .findViewById(R.id.tv_title); holder.tvDate = (TextView) convertView .findViewById(R.id.tv_date); holder.ivIcon = (ImageView) convertView .findViewById(R.id.iv_icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } News news = getItem(position); holder.tvTitle.setText(news.title); holder.tvDate.setText(news.pubdate); mBitmapUtils.display(holder.ivIcon, news.listimage); // 标记已读和未读 String readIds = PrefUtils.getString("read_ids", "", mActivity); if (readIds.contains(news.id)) { // 已读 holder.tvTitle.setTextColor(Color.GRAY); } else { // 未读 holder.tvTitle.setTextColor(Color.BLACK); } return convertView; } } static class ViewHolder { public TextView tvTitle; public TextView tvDate; public ImageView ivIcon; } }
public View initView() {
View view = View.inflate(mActivity, R.layout.pager_tab_detail, null);
ViewUtils.inject(this, view);
View header = View.inflate(mActivity, R.layout.list_header_topnews,
null);
ViewUtils.inject(this, header);// 必须也将头布局注入到ViewUtils
// 给listview添加头布局
lvList.addHeaderView(header);
// 设置下拉刷新监听
lvList.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// 从网络加载数据
getDataFromServer();
}
@Override
public void loadMore() {
// 加载更多数据
if (mMoreUrl != null) {
System.out.println("加载下一页数据...");
getMoreDataFromServer();
} else {
lvList.onRefreshComplete(true);// 收起加载更多布局
Toast.makeText(mActivity, "没有更多数据了", Toast.LENGTH_SHORT)
.show();
}
}
});
lvList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("pos:" + position);
News news = mNewsList.get(position);
// 当前点击的item的标题颜色置灰
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setTextColor(Color.GRAY);
// 将已读状态持久化到本地
// key: read_ids; value: 1324,1325,1326
String readIds = PrefUtils.getString("read_ids", "", mActivity);
if (!readIds.contains(news.id)) {// 以前没有添加过,才添加进来
readIds = readIds + news.id + ",";// 1324,1325,
PrefUtils.putString("read_ids", readIds, mActivity);
}
// 跳到详情页
Intent intent = new Intent(mActivity, NewsDetailActivity.class);
intent.putExtra("url", news.url);
mActivity.startActivity(intent);
}
});
return view;
}
4、标记未读、已读,持续化到本地(TabDetailPager.java)
// 将已读状态持久化到本地
// key: read_ids; value: 1324,1325,1326
String readIds = PrefUtils.getString("read_ids", "", mActivity);
if (!readIds.contains(news.id)) {// 以前没有添加过,才添加进来
readIds = readIds + news.id + ",";// 1324,1325,
PrefUtils.putString("read_ids", readIds, mActivity);
}
在class NewsAdapter extends BaseAdapter中对已读灰色化
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mActivity, R.layout.list_item_news,
null);
holder = new ViewHolder();
holder.tvTitle = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tvDate = (TextView) convertView
.findViewById(R.id.tv_date);
holder.ivIcon = (ImageView) convertView
.findViewById(R.id.iv_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
News news = getItem(position);
holder.tvTitle.setText(news.title);
holder.tvDate.setText(news.pubdate);
mBitmapUtils.display(holder.ivIcon, news.listimage);
// 标记已读和未读
String readIds = PrefUtils.getString("read_ids", "", mActivity);
if (readIds.contains(news.id)) {
// 已读
holder.tvTitle.setTextColor(Color.GRAY);
} else {
// 未读
holder.tvTitle.setTextColor(Color.BLACK);
}
return convertView;
}