Ejemplo n.º 1
0
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.d(TAG, "Item点击:" + position);
    // 获取头布局数量
    int headerViewsCount = lvList.getHeaderViewsCount();
    // 需要减去头布局的占位
    position = position - headerViewsCount;
    System.out.println("第" + position + "个被点击了");
    if (mNewsList != null) {
      News news = mNewsList.get(position);
      Log.d(TAG, "新闻id=" + news.id + "; 新闻标题=" + news.title);
      // 35314,35315,35316
      String ids = SharePreferenceUtils.getString(mActivity, PREF_NEWS_READ, "");
      // 只有在不包含该id时才添加
      if (!ids.contains(news.id)) {
        ids = ids + news.id + ",";
        // 更新已读id列表
        SharePreferenceUtils.putString(mActivity, PREF_NEWS_READ, ids);
      }
      // 刷新ListView
      // mNewsAdapter.notifyDataSetChanged();
      // 局部刷新ListView
      mNewsAdapter.changeTextColor(view);

      Intent intent = new Intent(mActivity, NewsDetailActivity.class);
      intent.putExtra("news_url", news.url);
      mActivity.startActivity(intent);
    }
  }
Ejemplo n.º 2
0
  /** 加载更多数据 */
  private void getMoreDataFromNet() {
    if (mMoreUrl != null) {
      HttpUtils utils = new HttpUtils();
      utils.send(
          HttpMethod.GET,
          mMoreUrl,
          new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
              processData(responseInfo.result, true);
              // 隐藏下拉刷新控件
              lvList.onRefreshComplete(false);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
              Log.e(TAG, "请求失败:" + msg);
              // 隐藏下拉刷新控件
              lvList.onRefreshComplete(false);
              Toast.makeText(mActivity, "加载更多失败", Toast.LENGTH_SHORT).show();
            }
          });
    } else {
      lvList.onRefreshComplete(false);
      Toast.makeText(mActivity, "没有更多数据了", Toast.LENGTH_SHORT).show();
    }
  }
Ejemplo n.º 3
0
  @Override
  public View initView() {
    //		//要给帧布局填充布局对象
    //		view = new TextView(mActivity);
    //		//view.setText(mTabData.title);//此处空指针
    //		view.setTextColor(Color.RED);
    //		view.setTextSize(22);
    //		view.setGravity(Gravity.CENTER);

    View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
    ViewUtils.inject(this, view);

    // 给listview添加头布局
    View mHeaderView = View.inflate(mActivity, R.layout.list_item_header, null);
    // 此处必须将头布局也注入
    ViewUtils.inject(this, mHeaderView);
    lvList.setOnRefreshListener(
        new OnRefreshListener() {

          @Override
          public void onRefresh() {
            Log.d(TAG, "onRefresh...");
            getDataFromServer();
          }

          @Override
          public void onLoadMore() {
            Log.d(TAG, "onLoadMore...");
            getMoreDataFromNet();
          }
        });
    lvList.addHeaderView(mHeaderView);
    /** 设置Item点击监听 */
    lvList.setOnItemClickListener(this);
    return view;
  }
Ejemplo n.º 4
0
  protected void processData(String result, boolean more) {
    Gson gson = new Gson();
    TabDetailBean newsTabBean = gson.fromJson(result, TabDetailBean.class);
    // 获取加载更多的url
    if (!TextUtils.isEmpty(newsTabBean.data.more)) {
      mMoreUrl = GlobalContants.SERVER_URL + newsTabBean.data.more;
    } else {
      mMoreUrl = null;
    }

    if (!more) {
      // 头条新闻填充数据
      mTopNews = newsTabBean.data.topnews;
      Log.e("test", "数量:" + mTopNews.size());
      if (mTopNews != null) {
        mViewPager.setAdapter(new TopNewsAdapter());
        //				mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
        //
        //					@Override
        //					public void onPageSelected(int position) {
        //						//更新头条新闻标题
        //						mTextView.setText(mTopNews.get(position).title);
        //					}
        //
        //					@Override
        //					public void onPageScrolled(int arg0, float arg1, int arg2) {
        //
        //					}
        //
        //					@Override
        //					public void onPageScrollStateChanged(int arg0) {
        //
        //					}
        //				});
        mIndicator.setViewPager(mViewPager);
        // 快照方式展示
        mIndicator.setSnap(true);

        // 事件要设置给Indicator
        mIndicator.setOnPageChangeListener(
            new OnPageChangeListener() {

              @Override
              public void onPageSelected(int position) {
                // 更新头条新闻标题
                TopNews topNews = mTopNews.get(position);
                mTextView.setText(topNews.title);
              }

              @Override
              public void onPageScrolled(
                  int position, float positionOffset, int positionOffsetPixels) {}

              @Override
              public void onPageScrollStateChanged(int state) {}
            });

        // 更新第一个头条新闻标题
        mTextView.setText(mTopNews.get(0).title);
        // 默认让第一个选中(解决页面销毁后重新初始化时,Indicator仍然保留上次圆点位置的bug)
        mIndicator.onPageSelected(0);
      }
      mTextView.setText(mTopNews.get(0).title);

      // 列表新闻
      mNewsList = newsTabBean.data.news;
      if (mNewsList != null) {
        mNewsAdapter = new NewsAdapter();
        lvList.setAdapter(mNewsAdapter);
      }
    } else {
      // 加载更多数据
      ArrayList<News> news = newsTabBean.data.news;
      if (mNewsList != null) {
        mNewsList.addAll(news);
        mNewsAdapter.notifyDataSetChanged();
      }
    }

    if (mHandler == null) {
      // 创建轮播条的Handler
      mHandler =
          new Handler() {
            public void handleMessage(android.os.Message msg) {
              // 判断是否到达最后一个
              int item = mViewPager.getCurrentItem();
              if (item < mTopNews.size() - 1) {
                item++;
              } else {
                item = 0;
              }
              // Log.d(TAG, "轮播条:" + item);
              mViewPager.setCurrentItem(item);
              mHandler.sendMessageDelayed(Message.obtain(), TOP_NEWS_CHANGE_TIME);
            };
          };
      // 延时4s发送消息
      mHandler.sendMessageDelayed(Message.obtain(), TOP_NEWS_CHANGE_TIME);
      mViewPager.setOnTouchListener(
          new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                  System.out.println("ACTION_DOWN");
                  // 停止广告自动轮播
                  // 删除handler的所有消息
                  mHandler.removeCallbacksAndMessages(null);
                  // mHandler.post(new Runnable() {
                  //
                  // @Override
                  // public void run() {
                  // //在主线程运行
                  // }
                  // });
                  break;
                case MotionEvent.ACTION_CANCEL: // 取消事件,
                  // 当按下viewpager后,直接滑动listview,导致抬起事件无法响应,但会走此事件
                  System.out.println("ACTION_CANCEL");
                  // 启动广告
                  mHandler.sendEmptyMessageDelayed(0, TOP_NEWS_CHANGE_TIME);
                  break;
                case MotionEvent.ACTION_UP:
                  System.out.println("ACTION_UP");
                  // 启动广告
                  mHandler.sendEmptyMessageDelayed(0, TOP_NEWS_CHANGE_TIME);
                  break;

                default:
                  break;
              }
              return false;
            }
          });
    }
  }