Set LinearLayout collapse direction

时间:2016-08-31 12:16:22

标签: android android-layout android-linearlayout

I have a horizontalScrollView with a LinearLayout in it, that has views in it. It looks something like:

|---------------------------------------------------------------
|      |      |      |      |      |      |      |      |      |
|  A   |  B   |  C   |  D   |   E  |   F  |  G   |  H   |   I  |
|      |      |      |      |      |      |      |      |      |
|      |      |      |      |      |      |      |      |      |
----------------------------------------------------------------

The linear layout is wider than the screen, so only some of the items are visible. Let's assume that items C through F are visible.

On some cases I remove many view at once, but at least one of the visible views is not removed. In our case, lets say that view E is going to remain, while other visible items (C,D,F) are going to be removed from layout.

What happens is, all remained views are collapsing to the left to fill the gaps, sometimes causing view E to move to the left and disappear from the visible area. What I want is to make the linear layout shrink around the chosen view (E)

Is there a way to cause items in linear layout to close the gap relative to a certain view or x coordinate? (in my case, relative to view E) This way, items G-I will go to the left, while items A and B will collapse to the right.

I've tried to set different parameters, such as gravity and pivot, but no luck. any suggestions?

EDIT 1:

adding the LayoutTransitioner I am using:

    mTabStripTransitioner = new LayoutTransition();

    AnticipateOvershootInterpolator ao = new AnticipateOvershootInterpolator();
    AnticipateOvershootInterpolator softAo = new AnticipateOvershootInterpolator(1f);
    ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "translationY", 0, 300);
    ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "translationY", 300, 0);

    animIn.addListener(animationListenerIn);
    animOut.addListener(animationListenerOut);

    animOut.setInterpolator(ao);
    animIn.setInterpolator(softAo);


    mTabStripTransitioner.setAnimator(LayoutTransition.APPEARING, animIn);
    mTabStripTransitioner.setDuration(LayoutTransition.APPEARING, MEDIUM_ANIM);
    mTabStripTransitioner.setAnimator(LayoutTransition.DISAPPEARING, animOut);
    mTabStripTransitioner.setDuration(LayoutTransition.DISAPPEARING, LONG_ANIM);
    mTabStripTransitioner.setStagger(LayoutTransition.CHANGE_DISAPPEARING, 50);
    mTabStripTransitioner.setStagger(LayoutTransition.CHANGE_APPEARING, 50);

    tabStrip.setLayoutTransition(mTabStripTransitioner);

3 个答案:

答案 0 :(得分:1)

您面临的“问题”是由于HorizontalScrollView试图保持相同的滚动位置。当您在层次结构中(在当前位置的左侧)制作一些“早期”的视图GONE时,滚动位置将保持不变,但它将不再指向相同的位置内容。

你能做的是:

当您移除(或将visibility设置为GONE)当前位置左侧的View时,您需要manually scroll {{1} BY 已删除的HorizontalScrollView的{​​{1}}。

答案 1 :(得分:0)

To achieve your goal you basically need to set the LinearLayout weight. This will ensure that the remaining space is filled according to the weight value. See here for the android dev example.

The other value you need is the visiblity modifier INVISIBLE and GONE. So instead of removing the Layouts you can set them to INVISBILE to take the current space in account or GONE to free up the space. See here for the documentation

答案 2 :(得分:0)

Simply you can achieve it by using HorizontalRecyclerView.

LinearLayoutManager layoutManager=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList =(RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);

have to call notifyDataSetChanged() if you changed(either added or deleted) the list values.