Java Code Examples for android.widget.ListView#getParent()

The following examples show how to use android.widget.ListView#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListViewUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = getListViewHeightBasedOnChildren(listView);
        listView.setLayoutParams(params);

        System.out.println("params.height:" + params.height);

        LinearLayout linearLayout = (LinearLayout) listView.getParent();

        // android.widget.LinearLayout.LayoutParams params2 = new
        // android.widget.LinearLayout.LayoutParams(
        // LayoutParams.MATCH_PARENT, params.height);
        //
        // params2.setMargins(10, 0, 10, 10);

        linearLayout.setLayoutParams(params);
    }
 
Example 2
Source File: ListViewUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * 通过listview的元素的高度来显示listview
 *
 * @param listView
 * @param attHeight
 */
public static void setListViewParentHeightBasedOnChildren(
        ListView listView, int attHeight) {
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = (int) (getListViewHeightBasedOnChildren(listView)
            + attHeight + 0.5);
    listView.setLayoutParams(params);

    System.out.println("params.height:" + params.height);

    LinearLayout linearLayout = (LinearLayout) listView.getParent();

    // android.widget.LinearLayout.LayoutParams params2 = new
    // android.widget.LinearLayout.LayoutParams(
    // LayoutParams.MATCH_PARENT, params.height);
    //
    // params2.setMargins(10, 0, 10, 10);

    linearLayout.setLayoutParams(params);

}
 
Example 3
Source File: ListViewUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * @param
 * @return void 返回类型
 * @Title setListViewParentHeight
 * @Description 重新设置listview父控件的高度
 */
public static void setListViewParentHeight(ListView listView, int height) {

    LinearLayout linearLayout = (LinearLayout) listView.getParent();
    android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
            android.widget.LinearLayout.LayoutParams.MATCH_PARENT, height);
    linearLayout.setLayoutParams(params);

}
 
Example 4
Source File: PullToRefreshBaseListFragment.java    From ONE-Unofficial with Apache License 2.0 5 votes vote down vote up
@Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View layout = super.onCreateView(inflater, container, savedInstanceState);

	ListView lv = (ListView) layout.findViewById(android.R.id.list);
	ViewGroup parent = (ViewGroup) lv.getParent();

	// Remove ListView and add PullToRefreshListView in its place
	int lvIndex = parent.indexOfChild(lv);
	parent.removeViewAt(lvIndex);
	mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
	parent.addView(mPullToRefreshListView, lvIndex, lv.getLayoutParams());

	return layout;
}
 
Example 5
Source File: PostListFragment.java    From Broadsheet.ie-Android with MIT License 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = super.onCreateView(inflater, container, savedInstanceState);

    // Get original ListView and Frame
    ListView originalLv = (ListView) layout.findViewById(android.R.id.list);
    ViewGroup frame = (ViewGroup) originalLv.getParent();

    // Remove old ListView
    frame.removeView(originalLv);

    // Create new PullToRefreshListView and add to Frame
    mPullRefreshListView = new PullToRefreshListView(getActivity());
    frame.addView(mPullRefreshListView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            fetchPosts(null);
        }
    });

    boolean pauseOnScroll = false;
    boolean pauseOnFling = true;
    PauseOnScrollListener listener = new PauseOnScrollListener(ImageLoader.getInstance(), pauseOnScroll,
            pauseOnFling);
    mPullRefreshListView.setOnScrollListener(listener);

    return layout;
}
 
Example 6
Source File: SettingsCardFragment.java    From Locate-driver with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getView() == null) {
        return;
    }

    final ListView listView = (ListView) getView().findViewById(android.R.id.list);
    final View parent = (View) listView.getParent();

    ViewTreeObserver viewTreeObserver = parent.getViewTreeObserver();
    if (viewTreeObserver.isAlive() == false) {
        return;
    }

    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                parent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            int width = parent.getWidth() - listView.getPaddingLeft() - listView.getPaddingRight();

            Adapter adapter = listView.getAdapter();
            if (adapter != null) {
                int height = 0;
                for (int i = 0; i < adapter.getCount(); i++) {
                    View item = adapter.getView(i, null, listView);
                    item.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), 0);
                    height += item.getMeasuredHeight();
                }

                FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.settings_preferences_framelayout);
                ViewGroup.LayoutParams param = frame.getLayoutParams();
                param.height = height + (listView.getDividerHeight() * (adapter.getCount()));
                frame.setLayoutParams(param);
            }
        }
    });

}