/* * 初始化图片图片控件 */ private void InitLayout() { mColumnHeight = new int[mColumnCount]; mIViews = new HashMap<Integer, FlowView>(); mPins = new HashMap<Integer, String>(); mPinMark = new HashMap[mColumnCount]; this.mLineIndex = new int[mColumnCount]; this.mBottomIndex = new int[mColumnCount]; this.mTopIndex = new int[mColumnCount]; for (int i = 0; i < mColumnCount; i++) { mLineIndex[i] = -1; mBottomIndex[i] = -1; mPinMark[i] = new HashMap<Integer, Integer>(); } if (mLayout != null) { mLayout.removeAllViews(); } mScrollView.getView(); mScrollView.setOnScrollListener( new LazyScrollView.OnScrollListener() { @Override public void onTop() { // 滚动到最顶端 Log.d("LazyScroll", "Scroll to top"); } @Override public void onScroll() { Log.d("LazyScroll", "onScroll"); } @Override public void onBottom() { Log.d("LazyScroll", "onBottom"); // 滚动到最低端 if (mWeibo != null && mWeibo.page.currentPage != mWeibo.page.totalPage) { isLoadMore = true; mCategoryHandler.sendEmptyMessage(GlobalParam.SHOW_LOADINGMORE_INDECATOR); getPublishWeiboData(GlobalParam.LIST_LOAD_MORE); } } // 滑动式释放图片 @Override public void onAutoScroll(int l, int t, int oldl, int oldt) { mScrollHeight = mScrollView.getMeasuredHeight(); Log.d("MainActivity", "mScrollHeight:" + mScrollHeight); if (t > oldt) { // 向下滚动 if (t > 2 * mScrollHeight) { // 超过两屏幕后 for (int k = 0; k < mColumnCount; k++) { if (mWaterFallItems != null && mWaterFallItems.size() > k && mWaterFallItems.get(k) != null) { LinearLayout localLinearLayout = mWaterFallItems.get(k); if (mPinMark[k] != null && mPinMark[k].get(Math.min(mBottomIndex[k] + 1, mLineIndex[k])) != null && mPinMark[k].get(Math.min(mBottomIndex[k] + 1, mLineIndex[k])) <= t + 3 * mScrollHeight) { // 最底部的图片位置小于当前t+3*屏幕高度 RelativeLayout singleLayout = (RelativeLayout) localLinearLayout.getChildAt( Math.min(1 + mBottomIndex[k], mLineIndex[k])); if (singleLayout != null && singleLayout.getChildAt(0) != null) { ((FlowView) singleLayout.getChildAt(0)).Reload(); } mBottomIndex[k] = Math.min(1 + mBottomIndex[k], mLineIndex[k]); } Log.d( "MainActivity", "headIndex:" + mTopIndex[k] + " footIndex:" + mBottomIndex[k] + " headHeight:" + mPinMark[k].get(mTopIndex[k])); if (mPinMark[k] != null && mPinMark[k].get(mTopIndex[k]) != null && mPinMark[k].get(mTopIndex[k]) < t - 2 * mScrollHeight) { // 未回收图片的最高位置<t-两倍屏幕高度 int i1 = mTopIndex[k]; mTopIndex[k]++; RelativeLayout singleLayout = (RelativeLayout) localLinearLayout.getChildAt(i1); if (singleLayout != null && singleLayout.getChildAt(0) != null) { ((FlowView) singleLayout.getChildAt(0)).recycle(); } /*((FlowView) localLinearLayout.getChildAt(i1)) .recycle();*/ Log.d("MainActivity", "recycle,k:" + k + " headindex:" + mTopIndex[k]); } } } } } else { // 向上滚动 for (int k = 0; k < mColumnCount; k++) { if (mWaterFallItems != null && mWaterFallItems.size() > k && mWaterFallItems.get(k) != null) { LinearLayout localLinearLayout = mWaterFallItems.get(k); if (mPinMark[k] != null && mPinMark[k].get(mBottomIndex[k]) != null && mPinMark[k].get(mBottomIndex[k]) > t + 3 * mScrollHeight) { RelativeLayout singleLayout = (RelativeLayout) localLinearLayout.getChildAt(mBottomIndex[k]); if (singleLayout != null && singleLayout.getChildAt(0) != null) { ((FlowView) singleLayout.getChildAt(0)).recycle(); } mBottomIndex[k]--; } if (mPinMark[k] != null && mPinMark[k].get(Math.max(mTopIndex[k] - 1, 0)) != null && mPinMark[k].get(Math.max(mTopIndex[k] - 1, 0)) >= t - 2 * mScrollHeight) { RelativeLayout singleLayout = (RelativeLayout) localLinearLayout.getChildAt(Math.max(-1 + mTopIndex[k], 0)); if (singleLayout != null && singleLayout.getChildAt(0) != null) { ((FlowView) singleLayout.getChildAt(0)).Reload(); } /*((FlowView) localLinearLayout.getChildAt(Math.max( -1 + mTopIndex[k], 0))).Reload();*/ mTopIndex[k] = Math.max(mTopIndex[k] - 1, 0); } } } } } }); mWaterFallContainer = new LinearLayout(mContext); mWaterFallContainer.setBackgroundColor( mContext.getResources().getColor(R.color.backgroud_color)); mWaterFallContainer.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); mWaterFallContainer.setOrientation(LinearLayout.HORIZONTAL); mLayout.addView(mWaterFallContainer); mHandler = new Handler() { @Override public void dispatchMessage(Message msg) { super.dispatchMessage(msg); } @Override public void handleMessage(Message msg) { // 显示图片 switch (msg.what) { case 1: final FlowView v = (FlowView) msg.obj; // int w = msg.arg1; int h = msg.arg2; // Log.d("MainActivity", // String.format( // "获取实际View高度:%d,ID:%d,columnIndex:%d,rowIndex:%d,filename:%s", // v.getHeight(), v.getId(), v // .getColumnIndex(), v.getRowIndex(), // v.getFlowTag().getFileName())); String f = v.getFlowTag().getThumbPicUrl(); // 此处计算列值 int columnIndex; int row; row = v.getId() / mColumnCount; columnIndex = v.getId() % mColumnCount; // int columnIndex = GetMinValue(mColumnHeight); v.setColumnIndex(columnIndex); mColumnHeight[columnIndex] += h; mPins.put(v.getId(), f); mIViews.put(v.getId(), v); View view = null; view = LayoutInflater.from(mContext).inflate(R.layout.image_display, null); RelativeLayout singleLayout = (RelativeLayout) view.findViewById(R.id.imagelayout); singleLayout.setPadding(2, 2, 2, 2); singleLayout.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); singleLayout.addView(v); // mWaterFallItems.get(columnIndex).addView(v); TextView nicknameTextView = (TextView) view.findViewById(R.id.content); /*textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setPadding(0, -15, 0, 0);*/ nicknameTextView.bringToFront(); if (v.getId() < mPicList.size()) { // nicknameTextView.setVisibility(View.GONE); singleLayout.setOnClickListener( new OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Intent intent = new Intent(mContext, ShowImageActivity.class); intent.putExtra("imageurl", mPicList.get(v.getId())); intent.putExtra("type", 2); mContext.startActivity(intent); } }); nicknameTextView.setText("sss"); } if (mWaterFallItems.get(columnIndex).getChildCount() > row) { mWaterFallItems.get(columnIndex).addView(singleLayout, row); } else { mWaterFallItems.get(columnIndex).addView(singleLayout); } mLineIndex[columnIndex]++; mPinMark[columnIndex].put(mLineIndex[columnIndex], mColumnHeight[columnIndex]); mBottomIndex[columnIndex] = mLineIndex[columnIndex]; break; } } @Override public boolean sendMessageAtTime(Message msg, long uptimeMillis) { return super.sendMessageAtTime(msg, uptimeMillis); } }; mWaterFallItems = new ArrayList<LinearLayout>(); for (int i = 0; i < mColumnCount; i++) { LinearLayout itemLayout = new LinearLayout(this); LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(mItemWidth, LayoutParams.WRAP_CONTENT); itemLayout.setPadding(3, 3, 3, 3); itemLayout.setOrientation(LinearLayout.VERTICAL); itemLayout.setLayoutParams(itemParam); mWaterFallItems.add(itemLayout); mWaterFallContainer.addView(itemLayout); } // 第一次加载 AddItemToContainer(0); }
/* * 实例化控件 */ private void initCompent() { setTitleContent(R.drawable.back_btn, true, R.drawable.map_favorite_btn, R.string.my_favorite); if (isShowRighIcon) { mRightBtn.setVisibility(View.VISIBLE); mRightBtn.setOnClickListener(this); } else { mRightBtn.setVisibility(View.GONE); } mLeftBtn.setOnClickListener(this); mSearchBtn.setOnClickListener(this); mListLayout = (LinearLayout) findViewById(R.id.category_linear); mRefreshViewLastUpdated = (TextView) findViewById(R.id.pull_to_refresh_time); mContainer = (MyPullToRefreshListView) findViewById(R.id.container); mListView = mContainer.getList(); mListView.setDivider(mContext.getResources().getDrawable(R.drawable.splite)); mListView.setCacheColorHint(0); mListView.setSelector(mContext.getResources().getDrawable(R.drawable.transparent_selector)); mListView.setOnItemClickListener(this); mListView.setOnCreateContextMenuListener(this); mContainer.setOnChangeStateListener(this); mListView.setHeaderDividersEnabled(false); mListView.setOnScrollListener( new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: // 处理加载更多 if (view.getLastVisiblePosition() == (view.getCount() - 1)) { if (mWeibo != null && mWeibo.page != null && mWeibo.page.hasMore == 1) { if (mFootView != null) { Message message = new Message(); message.what = GlobalParam.SHOW_LOADINGMORE_INDECATOR; message.obj = mFootView; mCheckHandler.sendMessage(message); } } else { mCheckHandler.sendEmptyMessage(GlobalParam.MSG_CHECK_STATE); } } break; default: break; } } @Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {} }); mMapLayout = (LinearLayout) findViewById(R.id.map_category_linear); mRefreshScrollViewLastUpdated = (TextView) findViewById(R.id.scrollview_pull_to_refresh_time); mScrollContainer = (MyPullToRefreshScrollView) findViewById(R.id.scrollview_container); mScrollContainer.setOnChangeStateListener(this); mScrollView = mScrollContainer.getScrollView(); mLayout = mScrollView.getLayout(); }