LinearLayout中的双向gridview

时间:2014-10-10 14:24:28

标签: android android-layout gridview android-studio

我遇到一些问题,让双向Gridview库(最初由Jess Andres https://github.com/jess-anders/two-way-gridview)在LinearLayout中工作。

Jess'示例代码本身在布局xml中只有gridview,但是我需要在gridview周围添加其他元素,因此将元素和gridview放在LinearLayout中。 问题是我收到以下错误......

java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to two_way_gridview_gradle.TwoWayAbsListView$LayoutParams
        at two_way_gridview_gradle.TwoWayGridView$HorizontalGridBuilder.onMeasure(TwoWayGridView.java:2872)
        at two_way_gridview_gradle.TwoWayGridView.onMeasure(TwoWayGridView.java:286)

据我所知,问题是充气器试图将参数从TwoWayAbsList分配给LinearLayout参数,但我还没有提示如何强制它只使用TwoWayAbsListView参数

这是我的布局文件......

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

        <TextView
            android:id="@+id/caller_text"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:lines="2"
            android:textSize="17sp"
            android:text="First text block"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"/>

    <two_way_gridview_gradle.TwoWayGridView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#E8E8E8"
        app:cacheColorHint="#E8E8E8"
        app:columnWidth="80dp"
        app:rowHeight="80dp"
        app:gravity="center"
        app:horizontalSpacing="16dp"
        app:verticalSpacing="16dp"
        app:numColumns="auto_fit"
        app:numRows="auto_fit"
        app:scrollDirectionLandscape="horizontal"
        app:scrollDirectionPortrait="horizontal"
        app:stretchMode="spacingWidthUniform"
        />

        <TextView
            android:id="@+id/cancel_text"
            android:layout_width="174dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:lines="2"
            android:textSize="17sp"
            android:text="Second text block"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"/>

</LinearLayout>

以下是我在活动onCreate()方法中夸大视图的方式....

setContentView(R.layout.activity_meetingcaller);
TwoWayGridView gridView = (TwoWayGridView) findViewById(R.id.gridview);
// Instance of ImageAdapter Class
gridView.setAdapter(new GridImageAdapter(this));

1 个答案:

答案 0 :(得分:0)

我一直在搞麻烦以实现双向网格视图,但最终以我的方式实现了它:

<ScrollView
                android:id="@+id/mainscrolView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                >


                <HorizontalScrollView
                    android:id="@+id/rclv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        >



                        <TableLayout
                            android:id="@+id/table"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:stretchColumns="*"
                            >

                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
</ScrollView>

我以编程方式添加TableLayout的子级。喜欢:

TableLayout tv =  findViewById(R.id.table);
 tv.removeAllViewsInLayout();

 TableRow tr = new TableRow(AndroidDatabaseManager.this);

 tr.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
 TextView b3 = new TextView(AndroidDatabaseManager.this);
b3.setText("Child");
tr.addView(b3);

 tv.addView(tr);

                final View vline = new View(AndroidDatabaseManager.this);
                vline.setLayoutParams(new
                        TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,2));
                vline.setBackgroundColor(Color.BLUE);
                tv.addView(vline); 

如果要向单元格添加边框。有一些解决方法。