android.widget.GridView.getViewTreeObserver()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(181)

本文整理了Java中android.widget.GridView.getViewTreeObserver()方法的一些代码示例,展示了GridView.getViewTreeObserver()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GridView.getViewTreeObserver()方法的具体详情如下:
包路径:android.widget.GridView
类名称:GridView
方法名:getViewTreeObserver

GridView.getViewTreeObserver介绍

暂无

代码示例

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(this);
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  @Override
  public void onGlobalLayout() {
    if (mAdapter.getNumColumns() == 0) {
      final int numColumns = (int) Math.floor(
          mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
      if (numColumns > 0) {
        final int columnWidth =
            (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
        mAdapter.setNumColumns(numColumns);
        mAdapter.setItemHeight(columnWidth);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
          mGridView.getViewTreeObserver()
              .removeOnGlobalLayoutListener(this);
        } else {
          mGridView.getViewTreeObserver()
              .removeGlobalOnLayoutListener(this);
        }
      }
    }
  }
});

代码示例来源:origin: stackoverflow.com

final GridView imageContainer = // your GridView
imageContainer.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
      {
      @Override
      public void onGlobalLayout() 
        {
        imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener( this );
        View lastChild = imageContainer.getChildAt( imageContainer.getChildCount() - 1 );
        imageContainer.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
        }
      });

代码示例来源:origin: chengzichen/KrGallery

@Override
  public boolean onPreDraw() {
    fixLayoutInternal();
    if (listView != null) {
      listView.getViewTreeObserver().removeOnPreDrawListener(this);
    }
    return true;
  }
});

代码示例来源:origin: stackoverflow.com

GridView calendarView = (GridView)findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver viewTreeObserver = calendarView.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() { 
    this.calendarView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    View v = calendarView.getChildAt(position);

    if (v!=null) {
      v.setSelected(true);
    }
  }
});

代码示例来源:origin: chengzichen/KrGallery

private void fixLayout() {
  if (listView != null) {
    ViewTreeObserver obs = listView.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
      @Override
      public boolean onPreDraw() {
        fixLayoutInternal();
        if (listView != null) {
          listView.getViewTreeObserver().removeOnPreDrawListener(this);
        }
        return true;
      }
    });
  }
}

代码示例来源:origin: caohaoping/PhotosWall

public void initEvent() {
  mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
  mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);
  // 监听获取图片的宽高
  mPhotoWallView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      // 计算列数
      final int numColumns = (int) Math.floor(mPhotoWallView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
      if (numColumns > 0) {
        int columnWidth = (mPhotoWallView.getWidth() / numColumns) - mImageThumbSpacing;
        mWallAdapter.setItemSize(columnWidth);
        mPhotoWallView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
    }
  });
}

代码示例来源:origin: caohaoping/PhotosWall

@Override
  public void onGlobalLayout() {
    // 计算列数
    final int numColumns = (int) Math.floor(mPhotoWallView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
    if (numColumns > 0) {
      int columnWidth = (mPhotoWallView.getWidth() / numColumns) - mImageThumbSpacing;
      mWallAdapter.setItemSize(columnWidth);
      mPhotoWallView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
  }
});

代码示例来源:origin: stackoverflow.com

gridview1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
      gridview1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      gwight = gridview1.getWidth(); 
      ghight = gridview1.getHeight();

代码示例来源:origin: stackoverflow.com

GridView.getViewTreeObserver.addOnGlobalLayoutListener(getLayoutListener(mGridView))
private static ViewTreeObserver.OnGlobalLayoutListener getLayoutListener(final GridView mGridView) {
  ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      View view = mGridView.getChildAt(0);
      if(view != null) {
        if(!view.isSelected()) {
          view.setSelected(true);
        } else {
          // remove the listener after the first time.
          if (Build.VERSION.SDK_INT < 16) {
            mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
          } else {
            mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
          }
        }
      }
    }
  };

  return listener;
}

代码示例来源:origin: WuXiaolong/WoChat

@TargetApi(VERSION_CODES.JELLY_BEAN)
  @Override
  public void onGlobalLayout() {
    final int numColumns = (int) Math.floor(mGridView
        .getWidth()
        / (mImageThumbSize + mImageThumbSpacing));
    if (numColumns > 0) {
      final int columnWidth = (mGridView.getWidth() / numColumns)
          - mImageThumbSpacing;
      mAdapter.setItemHeight(columnWidth);
      if (BuildConfig.DEBUG) {
        Log.d(TAG,
            "onCreateView - numColumns set to "
                + numColumns);
      }
      if (Utils.hasJellyBean()) {
        mGridView.getViewTreeObserver()
            .removeOnGlobalLayoutListener(this);
      } else {
        mGridView.getViewTreeObserver()
            .removeGlobalOnLayoutListener(this);
      }
    }
  }
});

代码示例来源:origin: zxfnicholas/CameraSDK

@Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  public void onGlobalLayout() {
    final int width = mGridView.getWidth();
    final int height = mGridView.getHeight();
    // mGridWidth = width;
    // mGridHeight = height;
    final int desireSize = getResources().getDimensionPixelOffset(R.dimen.image_size);
    final int numCount = width / desireSize;
    final int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
    int columnWidth = (width - columnSpace*(numCount-1)) / numCount;
    mImageAdapter.setItemSize(columnWidth);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
      mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }else{
      mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
  }
});

代码示例来源:origin: huangfangyi/FanXin

@TargetApi(VERSION_CODES.JELLY_BEAN)
  @Override
  public void onGlobalLayout() {
    final int numColumns = (int) Math.floor(mGridView
        .getWidth()
        / (mImageThumbSize + mImageThumbSpacing));
    if (numColumns > 0) {
      final int columnWidth = (mGridView.getWidth() / numColumns)
          - mImageThumbSpacing;
      mAdapter.setItemHeight(columnWidth);
      if (BuildConfig.DEBUG) {
        Log.d(TAG,
            "onCreateView - numColumns set to "
                + numColumns);
      }
      if (Utils.hasJellyBean()) {
        mGridView.getViewTreeObserver()
            .removeOnGlobalLayoutListener(this);
      } else {
        mGridView.getViewTreeObserver()
            .removeGlobalOnLayoutListener(this);
      }
    }
  }
});

代码示例来源:origin: easemob/kefu-android-demo

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
          @Override
          public void onGlobalLayout() {
            final int numColumns = (int) Math.floor(mGridView
                .getWidth()
                / (mImageThumbSize + mImageThumbSpacing));
            if (numColumns > 0) {
              final int columnWidth = (mGridView.getWidth() / numColumns)
                  - mImageThumbSpacing;
              mAdapter.setItemHeight(columnWidth);
//                            if (BuildConfig.DEBUG) {
//                                Log.d(TAG,
//                                        "onCreateView - numColumns set to "
//                                                + numColumns);
//                            }
              if (Utils.hasJellyBean()) {
                mGridView.getViewTreeObserver()
                    .removeOnGlobalLayoutListener(this);
              } else {
                mGridView.getViewTreeObserver()
                    .removeGlobalOnLayoutListener(this);
              }
            }
          }
        });

代码示例来源:origin: WuXiaolong/WoChat

mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
      @TargetApi(VERSION_CODES.JELLY_BEAN)

代码示例来源:origin: easemob/kefu-android-demo

mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

代码示例来源:origin: huangfangyi/FanXin

mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
      @TargetApi(VERSION_CODES.JELLY_BEAN)

代码示例来源:origin: FreedomZZQ/YouJoin-Android

mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

代码示例来源:origin: sphinx02/AppStarter

mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

代码示例来源:origin: reloZid/android-anuto

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_change_map);
  mAdapter = new MapsAdapter(this, mMapRepository, mHighScores);
  arrow_up = (ImageView) findViewById(R.id.arrow_up);
  arrow_down = (ImageView) findViewById(R.id.arrow_down);
  grid_maps = (GridView) findViewById(R.id.grid_maps);
  grid_maps.setOnItemClickListener(this);
  grid_maps.getViewTreeObserver().addOnScrollChangedListener(this);
  grid_maps.post(new Runnable() {
    @Override
    public void run() {
      updateArrowVisibility();
    }
  });
  grid_maps.setAdapter(mAdapter);
}

相关文章

GridView类方法