动态更改Fragment的容器高度

时间:2014-09-24 05:54:11

标签: android android-fragments android-listview android-viewpager

我想在我的PageViewer的滚动事件中从我的片段类更改容器的高度(ListView)。

我已尝试过的内容: -

1.LayoutParams

             ViewGroup.LayoutParams params = view1.getLayoutParams();
             params.height = mContainerHeight+ Math.max(a,b);
             view1.requestLayout();

此处view1是我的容器。

2.直接获取容器的片段类(通过FindViewbById),因为我的视图处于不同的(父)布局,我们无法从不同的布局获取视图。

基于以上实施的更多调整。

我可能做错了,所以我想告诉你我想要实现的目标。我的容器高度应该增加,以填充Actionbar和我的ListView之间的空白区域。当我向上滚动容器高度应该增加。

发生了什么。Fragment 操作栏正在向上移动,但Container不是。

我真的很感激一些帮助。

编辑1 代码

AwesomeCardFragment类

public class AwesomeCardFragment extends Fragment {

private int mActionBarTitleColor;
private int mActionBarHeight;
private int mHeaderHeight;
private int mMinHeaderTranslation;
private KenBurnsView mHeaderPicture;
private ImageView mHeaderLogo;
private View mHeader;
private View mPlaceHolderView;
private AccelerateDecelerateInterpolator mSmoothInterpolator;

private int mContainerHeight;
private int mMinContainerTranslation;

private RectF mRect1 = new RectF();
private RectF mRect2 = new RectF();

private AlphaForegroundColorSpan mAlphaForegroundColorSpan;
private SpannableString mSpannableString;

private TypedValue mTypedValue = new TypedValue();

private static final String ARG_POSITION = "position";

// private int position;
ListView mListView;

ViewPager vp;
MainActivity ma;
// Button productButton;
// static String ARG_POSITION = "position";
private int position;

// LinearLayout rl;

public static AwesomeCardFragment newInstance(int position) {
    AwesomeCardFragment f = new AwesomeCardFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    position = getArguments().getInt(ARG_POSITION);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    inflater.inflate(R.layout.activity_slide_main, container, false);

    FrameLayout fl = new FrameLayout(getActivity());
    fl.setLayoutParams(params);
    final int margin = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
                    .getDisplayMetrics());

    mListView = new ListView(getActivity());
    params.setMargins(margin, margin, margin, margin);
    mListView.setLayoutParams(params);
    mListView.setLayoutParams(params);
    mListView.setBackgroundResource(R.drawable.background_card);
    // test
    fl.addView(mListView);
    mSmoothInterpolator = new AccelerateDecelerateInterpolator();
    mHeaderHeight = getResources().getDimensionPixelSize(
            R.dimen.header_height);
    mMinHeaderTranslation = -mHeaderHeight + getActionBarHeight()
            + getActionBarHeight();

    mMinContainerTranslation = mMinHeaderTranslation + getActionBarHeight()
            + getActionBarHeight();
    mHeader = getActivity().findViewById(R.id.header);
    mHeaderPicture = (KenBurnsView) getActivity().findViewById(
            R.id.header_picture);
    mHeaderPicture.setResourceIds(R.drawable.picture0, R.drawable.picture1);
    mHeaderLogo = (ImageView) getActivity().findViewById(R.id.header_logo);

    mActionBarTitleColor = getResources().getColor(
            R.color.actionbar_title_color);

    mSpannableString = new SpannableString(
            getString(R.string.noboringactionbar_title));
    mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(
            mActionBarTitleColor);
    mContainerHeight = container.getHeight();
    setupListView(container);
    return fl;
}

private void setupListView(final ViewGroup view1) {
    ArrayList<String> FAKES = new ArrayList<String>();
    for (int i = 0; i < 1000; i++) {
        FAKES.add("entry " + i);
    }

    mPlaceHolderView = getActivity().getLayoutInflater().inflate(
            R.layout.view_header_placeholder, mListView, false);

    // mPlaceHolderView = getActivity().getLayoutInflater().in
    mListView.addHeaderView(mPlaceHolderView);
    mListView.setAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, FAKES));
    mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            int scrollY = getScrollY();
             ViewGroup.LayoutParams params = view1.getLayoutParams();

        Log.i("wtf", "container height"+mContainerHeight);

        params.height = mContainerHeight+ Math.max(-scrollY,
             mMinHeaderTranslation);

             Log.i("wtf", "container height changed"+params.height);

             view1.requestLayout();

             mHeader.setTranslationY(Math.max(-scrollY,
                    mMinHeaderTranslation));

            ViewGroup.LayoutParams param = (ViewGroup.LayoutParams) view1
                    .getLayoutParams();
            if (param != null) {
                param.height = mContainerHeight
                        + Math.max(-scrollY, mMinHeaderTranslation);

                view1.setLayoutParams(param);
            }

            // view1.setTranslationY(Math.max(-scrollY,
            // mMinHeaderTranslation));

            // header_logo --> actionbar icon
            float ratio = clamp(mHeader.getTranslationY()
                    / mMinHeaderTranslation, 0.0f, 1.0f);
            interpolate(mHeaderLogo, getActionBarIconView(),
                    mSmoothInterpolator.getInterpolation(ratio));

            setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));

        }
    });
}
}

包含Container的主要xml类

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="@dimen/header_height" >

    <com.flavienlaurent.notboringactionbar.KenBurnsView
        android:id="@+id/header_picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/picture0" />

    <ImageView
        android:id="@+id/header_logo"
        android:layout_width="@dimen/header_logo_size"
        android:layout_height="@dimen/header_logo_size"
        android:layout_gravity="center"
        android:src="@drawable/ic_header_logo" />

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:layout_gravity="bottom"
        android:src="@drawable/background_tabs" />
</FrameLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/colors"
    android:layout_below="@+id/header"

    tools:context=".MainActivity" />

<LinearLayout
    android:id="@+id/colors"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="8dip"
    android:layout_marginLeft="4dip"
    android:layout_marginRight="4dip"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FF666666"
        android:onClick="onColorClicked"
        android:tag="#FF666666" />

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FF96AA39"
        android:onClick="onColorClicked"
        android:tag="#FF96AA39" />

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FFC74B46"
        android:onClick="onColorClicked"
        android:tag="#FFC74B46" />

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FFF4842D"
        android:onClick="onColorClicked"
        android:tag="#FFF4842D" />

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FF3F9FE0"
        android:onClick="onColorClicked"
        android:tag="#FF3F9FE0" />

    <ImageView
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="#FF5161BC"
        android:onClick="onColorClicked"
        android:tag="#FF5161BC" />
</LinearLayout>

1 个答案:

答案 0 :(得分:-1)

您希望动态控制您的片段,然后您必须使用DialogFragment而不是Fragment

看看: http://developer.android.com/reference/android/support/v4/app/DialogFragment.html http://developer.android.com/reference/android/app/DialogFragment.html

您现在可以包含布局参数,以控制高度和范围。宽度动态。