在gridview图块上的viewflipper中具有不同高度的两个视图

时间:2016-10-05 23:58:38

标签: android gridview android-viewflipper

我有GridView,每个图块中都有ViewFlipperViewFlipper中的每个视图都有不同的大小,但是当翻转图块时,两个视图都被强制为与GridView列相同的大小。我尝试了什么:

  • 一派
  • 在ViewFlipper上将android:measureAllChildren设置为false仅适用于ListView
  • android:columnWidth未指定
  • 在网格图块布局文件中创建ViewFlipper根标记

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp" android:layout_height="150dp">

<ViewFlipper
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:measureAllChildren="false"
    android:id="@+id/view_flipper_3"
    >

    <RelativeLayout
        android:layout_width="180dp"
        android:layout_height="150dp"
        android:id="@+id/front2"
        android:background="#088980"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/commitmentNumber"
            android:textSize="25dp"
            />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="220dp"
        android:layout_height="190dp"
        android:background="#000000"
        android:id="@+id/back_2"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:id="@+id/commitmentPartOne"
            android:paddingBottom="5dp"
            android:textSize="25sp"
            android:text="Part One"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/commitmentPartTwo"
            android:textSize="25sp"
            android:text="Part Two"
            android:paddingBottom="5dp"
            android:layout_below="@+id/commitmentPartOne"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/commitmentPartThree"
            android:textSize="25sp"
            android:text="Part Three"
            android:layout_below="@+id/commitmentPartTwo"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

    </RelativeLayout>

</ViewFlipper>

</RelativeLayout>

fragment_with_gridview.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sixCommitmentsGridView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:numColumns="2"
    android:gravity="center"
    android:columnWidth="179dp"
    android:stretchMode="columnWidth"
    />
</RelativeLayout>

GridViewAdapter.java:

public class SixCommitmentsGridAdapter extends BaseAdapter {

Context context;
String[] numbers;

public SixCommitmentsGridAdapter(Context context, String[] numbers) {
    this.context = context;
    this.numbers = numbers;
}

@Override
public int getCount() {
    return numbers.length;
}

@Override
public Object getItem(int position) {
    return numbers[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view;
    final ViewFlipper flipper;
    TextView commitmentNumber;

    if(convertView == null){
       LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.six_commitments_grid_item, parent, false);
    }else{
        view = convertView;
    }

    flipper = (ViewFlipper) view.findViewById(R.id.view_flipper_3);
    commitmentNumber = (TextView) view.findViewById(R.id.commitmentNumber);

    commitmentNumber.setText(numbers[position]);
    flipper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position % 2 == 0 || position == 0){
                AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.RIGHT_LEFT, 200);
            }else {
                AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.LEFT_RIGHT, 200);
            }

        }

    });

    return view;
}
}

2 个答案:

答案 0 :(得分:0)

使用ViewFlipperStaggeredGridLayoutManager管理我的意图RecyclerView效果,以实现相同的网格式布局。

答案 1 :(得分:0)

在Main RelativeLayout中包含ViewFlipper,你可以设置高度和宽度! 设置它们“wrap_content”

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">

也许有效。 祝你好运。

更新

使用此库:

https://github.com/etsy/AndroidStaggeredGrid