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

The following examples show how to use android.widget.ListView#getLayoutParams() . 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: listviewutil.java    From Favorite-Android-Client with Apache License 2.0 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight() + 50;
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -1));
    listView.setLayoutParams(params);
}
 
Example 2
Source File: NewsDetailActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
/**
 * 使用自定义的Listview不用调用该方法
 * */
public void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
Example 3
Source File: ListViewUtil.java    From RxAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
 
Example 4
Source File: MaterialDialog.java    From pius1 with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setListViewHeightBasedOnChildren(ListView listView)
   {
       ListAdapter listAdapter = listView.getAdapter();
       if (listAdapter == null)
{
           // pre-condition
           return;
       }

       int totalHeight = 0;
       for (int i = 0; i < listAdapter.getCount(); i++)
{
           View listItem = listAdapter.getView(i, null, listView);
           listItem.measure(0, 0);
           totalHeight += listItem.getMeasuredHeight();
       }

       ViewGroup.LayoutParams params = listView.getLayoutParams();
       params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
       listView.setLayoutParams(params);
   }
 
Example 5
Source File: AboutActivity.java    From pivaa with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Shrink listview height
 * @param listView
 * @param adapter
 */
public void setListViewHeightBasedOnChildren(ListView listView, AboutAdapter adapter) {
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight() + 180;
        Log.i("htbridge", "listItem.getMeasuredHeight()  = " + listItem.getMeasuredHeight() );
    }

    Log.i("htbridge", "totalHeight = " + totalHeight);

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();

}
 
Example 6
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 7
Source File: ViewUtils.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * 计算ListView的高度, 重置ListView的高度.
 * 
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(ListView listView)
{
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null)
	{
		return;
	}

	View listItem = null;
	int totalHeight = 0;
	for (int i = 0; i < listAdapter.getCount(); i++)
	{
		listItem = listAdapter.getView(i, null, listView);
		listItem.measure(0, 0);
		totalHeight += listItem.getMeasuredHeight();
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	listView.setLayoutParams(params);
}
 
Example 8
Source File: MainActivity.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
public void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高   注意:LinearLayout才有measure方法 所以,此setListViewHeightBasedOnChildren方法只适用ListView的父布局为LinearLayout
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
Example 9
Source File: SendScreen.java    From smartcoins-wallet with MIT License 6 votes vote down vote up
public void setListViewHeightBasedOnChildren(ListView listView, int childCount) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }
    int totalHeight = 0;
    for (int i = 0; i < childCount; i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        int px = listItem.getMeasuredHeight();
        totalHeight += px * 1.2;
    }
    Log.d(TAG, String.format("total height: %d", totalHeight));
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (5 - 1));
    listView.setLayoutParams(params);
}
 
Example 10
Source File: CommentUtil.java    From Social with Apache License 2.0 6 votes vote down vote up
/**
 * 根据item设置listview高度
 * */
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
 
Example 11
Source File: ViewUtils.java    From opencdk-appwidget with Apache License 2.0 6 votes vote down vote up
/**
 * 计算ListView的高度, 重置ListView的高度.
 * 
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(ListView listView)
{
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null)
	{
		return;
	}

	View listItem = null;
	int totalHeight = 0;
	for (int i = 0; i < listAdapter.getCount(); i++)
	{
		listItem = listAdapter.getView(i, null, listView);
		listItem.measure(0, 0);
		totalHeight += listItem.getMeasuredHeight();
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	listView.setLayoutParams(params);
}
 
Example 12
Source File: BuyTicketInfoFragment.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_buy_ticket_info, null);
	lvInfos = (ListView) v.findViewById(R.id.buyTicketInfo_lvInfos);
	MyDatabase myDB = new MyDatabase(this.getActivity());
	mAdapter= new SimpleAdapter(this.getActivity(), mLstDatas, R.layout.item_buy_ticket_info,
			new String[]{MyDatabase.KEY, MyDatabase.VALUE},
			new int[]{R.id.item_buy_ticket_info_tvQuestion, R.id.item_buy_ticket_info_tvAnswer}
			);
	lvInfos.setAdapter(mAdapter);
	myDB.closeDB();
	notifyAdapterDataChanged(myDB.getTicketInfos(0));
	MyUtils.setListViewHeightBasedOnChildren(lvInfos);  //设置ListView全部显示
	ViewGroup.LayoutParams params = lvInfos.getLayoutParams();
	
	params.height += 3000;   //方法不太准,人为校正高度
	lvInfos.setLayoutParams(params);
	sv1 = (ScrollView)v.findViewById(R.id.buyTicketInfo_sv1);
	sv1.smoothScrollTo(0, 20);
	return v;
}
 
Example 13
Source File: EditDatabaseActivity.java    From AndroidFaceRecognizer with MIT License 5 votes vote down vote up
private void initialize(){
	DisplayMetrics dm = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics(dm);
	
	screenWidth = dm.widthPixels;
	screenHeight = dm.heightPixels;
	
	final TextView headerText = (TextView)findViewById(R.id.dbeditHeaderText);
	RelativeLayout.LayoutParams headerTextParams = (RelativeLayout.LayoutParams)headerText.getLayoutParams();
	headerTextParams.leftMargin = screenHeight/8;
	headerText.setLayoutParams(headerTextParams);
	headerText.setTextSize(TypedValue.COMPLEX_UNIT_DIP,(float)screenHeight/45);
	headerText.setText("Face DataBase");
	headerText.setTextColor(Color.LTGRAY);
	headerText.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
	headerText.setTypeface(null, Typeface.BOLD);
	
	
	ListView listView = (ListView)findViewById(R.id.dbeditListView);
	ListAdapter listAdapter = new ListAdapter();
	listView.setAdapter(listAdapter);
	listView.setOnItemClickListener(itemClickListener);
	listView.setVerticalScrollBarEnabled(true);
	listView.setFastScrollEnabled(true);
	
	RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)listView.getLayoutParams();
	params.leftMargin = screenWidth/40;
	params.rightMargin = screenWidth/40;
	//params.topMargin = screenHeight/40;
	listView.setLayoutParams(params);
	listView.setVerticalScrollBarEnabled(false);
}
 
Example 14
Source File: NearbyRecruitFragment.java    From Social with Apache License 2.0 5 votes vote down vote up
public void setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if(listAdapter == null) {
        return;
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
 
Example 15
Source File: SearchEnginePreference.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView = (ListView) getView().findViewById(android.R.id.list);
    int marginTop = getActivity().getResources().getDimensionPixelSize(
            R.dimen.search_engine_list_margin_top);
    MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams();
    layoutParams.setMargins(0, marginTop, 0, 0);
    mListView.setLayoutParams(layoutParams);
    mListView.setAdapter(mSearchEngineAdapter);
    mListView.setDivider(null);
}
 
Example 16
Source File: TabularContextMenuUi.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the view of a context menu. Based off the Context Type, it'll adjust the list of
 * items and display only the ones that'll be on that specific group.
 * @param activity Used to get the resources of an item.
 * @param params used to create the header text.
 * @param items A set of Items to display in a context menu. Filtered based off the type.
 * @param isImage Whether or not the view should have an image layout or not.
 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could exist in this view
 *                 or any other views calculated in the context menu. Used to estimate the size
 *                 of the list.
 * @return Returns a filled LinearLayout with all the context menu items.
 */
@VisibleForTesting
ViewGroup createContextMenuPageUi(Activity activity, ContextMenuParams params,
        List<ContextMenuItem> items, boolean isImage, int maxCount) {
    ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate(
            R.layout.tabular_context_menu_page, null);
    ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_items);

    displayHeaderIfVisibleItems(params, baseLayout);
    if (isImage) {
        // #displayHeaderIfVisibleItems() sets these two views to GONE if the header text is
        // empty but they should still be visible because we have an image to display.
        baseLayout.findViewById(R.id.context_header_layout).setVisibility(View.VISIBLE);
        baseLayout.findViewById(R.id.context_divider).setVisibility(View.VISIBLE);
        displayImageHeader(baseLayout, params, activity.getResources());
    }

    // Set the list adapter and get the height to display it appropriately in a dialog.
    Runnable onDirectShare = new Runnable() {
        @Override
        public void run() {
            mOnShareItemClicked.run();
            mDialog.dismiss();
        }
    };
    TabularContextMenuListAdapter listAdapter =
            new TabularContextMenuListAdapter(items, activity, onDirectShare);
    ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
    layoutParams.height = measureApproximateListViewHeight(listView, listAdapter, maxCount);
    listView.setLayoutParams(layoutParams);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(this);

    return baseLayout;
}
 
Example 17
Source File: ViewUtils.java    From Mp3Cutter with GNU General Public License v3.0 5 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView,
		boolean mIsFlag) {
	// 获取ListView对应的Adapter
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null) {
		return;
	}

	int totalHeight = 0;
	int moneHeight = 0;
	for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
		View listItem = listAdapter.getView(i, null, listView);
		listItem.measure(0, 0); // 计算子项View 的宽高
		totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
		// sysout.println("----------高度----------"+listItem.getMeasuredHeight());
		// if(i==len-1 && (mIsFlag)){ //评论特殊处理
		// moneHeight = listItem.getMeasuredHeight()/2+10;
		// }
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	totalHeight = moneHeight + totalHeight;
	params.height = totalHeight
			+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	// sysout.println("-----总高度----------------"+params.height);
	// listView.getDividerHeight()获取子项间分隔符占用的高度
	// params.height最后得到整个ListView完整显示需要的高度
	listView.setLayoutParams(params);
	// sysout.println("---------ListView高度----------"+listView.getLayoutParams().height);
}
 
Example 18
Source File: DialogHdAccountOldAddresses.java    From bither-android with Apache License 2.0 4 votes vote down vote up
private void initView() {
    setContentView(R.layout.dialog_hd_old_addresses);
    lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(adapter);
    lv.getLayoutParams().height = caculateHeight();
}
 
Example 19
Source File: ProofActivity.java    From Mupdf with Apache License 2.0 4 votes vote down vote up
@Override
public void onIdle()
{
	//  called when page rendering has become idle

	if (mWaitingForSpinner)
	{
		spinner.dismiss();
		mWaitingForSpinner = false;
	}

	if (mWaitingForIdle)
	{
		spinner.dismiss();
		setPageLabel();

		//  get the current page
		DocPageView dpv = (DocPageView)mDocView.getViewFromAdapter(mDocView.getCurrentPage());
		Page page = dpv.getPage();

		//  count the separations
		int numSeparations = page.countSeparations();

		//  set up the list
		mColorList = (ListView)findViewById(R.id.proof_color_list);
		mColorAdapter = new ChooseColorAdapter(getLayoutInflater(), new ColorChangeListener() {
			@Override
			public void onColorChange() {
				mApplyButton.setEnabled(true);
			}
		});
		mColorList.setAdapter(mColorAdapter);

		//  get each one
		for (int i=0; i<numSeparations; i++)
		{
			//  get it
			Separation sep = page.getSeparation(i);
			String name = sep.name;

			//  transform to a color that can be used to colorize icons
			int alpha = (sep.bgra >> 24) & 0xFF;
			int red   = (sep.bgra >> 16) & 0xFF;
			int green = (sep.bgra >> 8 ) & 0xFF;
			int blue  = (sep.bgra >> 0 ) & 0xFF;
			int color = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);

			mColorAdapter.add(new ChooseColorItem(sep.name, color, true, sep));
		}

		mColorList.getLayoutParams().width = getWidestView(getBaseContext(), mColorAdapter);

	}
	mWaitingForIdle = false;
}
 
Example 20
Source File: ArtistDetailActivity.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
public void setHeightofListViewBasedOnContent(ListView listView) {

        ListAdapter mAdapter = listView.getAdapter();

        int totalHeight = 0;

        for (int i = 0; i < mAdapter.getCount(); i++) {

            totalHeight += getResources().getDimension(R.dimen.item_list_height);
            Log.w("HEIGHT" + i, String.valueOf(totalHeight));

        }

        totalHeight = totalHeight +  (listView.getDividerHeight() * (mAdapter.getCount() - 1)) + listView.getPaddingTop();

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

    }