Android GridView双宽度列

时间:2018-10-05 11:47:53

标签: java android android-gridview

我正在尝试将应用程序中的一列加倍,从下面的照片中,我希望test1跨两个单元格,以便它将填充test1和test2的位置并将test2推到下一个位置。

我已经尝试了几种不同的方法来执行此操作,但是当一切都是静态的而不是更改信息时,似乎更容易做到。我将附上所有代码。我知道有一种扩展方式,但是必须在xml中定义,这显然对我不起作用,因为我的适配器中的代码中生成了不同的单元格。

我已附上以下所有代码。我想让每个不同的单元格保持相同的宽度和高度,而不是希望单元格1的宽度加倍。

谢谢

activity_main.xml

                    <GridView
                    android:id="@+id/gridview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:columnWidth="120dp"
                    android:numColumns="auto_fit"
                    android:verticalSpacing="14dp"
                    android:stretchMode="spacingWidthUniform"
                    />

linearlayout_grid.xml

<example.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@color/reloadedpurple"
    android:elevation="6dp"
    >

    <TextView
        android:id="@+id/textview_book_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        tools:text=""
        android:textStyle="bold"
        android:paddingTop="4dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:gravity="center_horizontal"/>

    <TextView
        android:id="@+id/textview_book_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        tools:text=""
        android:gravity="center_horizontal"/>

</example.MyLinearLayout>

MyLinearLayout.java

public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the key that will make the height equivalent to its width
    }
}

GridAdapter.java

public class GridAdapter extends BaseAdapter {

    private final Context mContext;
    private final Book[] books;

    // 1
    public GridAdapter(Context context, Book[] books) {
        this.mContext = context;
        this.books = books;
    }

    // 2
    @Override
    public int getCount() {
        return books.length;
    }

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

    // 4
    @Override
    public Object getItem(int position) {
        return null;
    }

    // 5
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 1
        final Book book = books[position];

        // 2
        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.linearlayout_grid, null);
        }

        // 3

        final TextView nameTextView = (TextView)convertView.findViewById(R.id.textview_book_name);
        final TextView authorTextView = (TextView)convertView.findViewById(R.id.textview_book_author);


        // 4
        nameTextView.setText(book.getName());
        authorTextView.setText(book.getAuthor());

        return convertView;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            GridView gridView = (GridView)findViewById(R.id.gridview);
            GridAdapter gridAdapter = new GridAdapter(this, books);
            gridView.setAdapter(gridAdapter);
        }
        private Book[] books = {
                new Book("test1", "test1", R.drawable.ic_launcher_background,
                        ""),
                new Book("test2", "test2", R.drawable.ic_launcher_background,
                        ""),
                new Book("test3", "test3", R.drawable.ic_launcher_background,
                        ""),
                new Book("test4", "test4", R.drawable.ic_launcher_background,
                        "")

        };
    }

0 个答案:

没有答案