简体   繁体   中英

Why do my Custom GridView show only one column?

I'v tried making a Custom GridView that adds proggramatically TextView items. The TextViews needs to act as an ImageView only that it has a text within it.

But for some odd reason it shows me only 1 column out of 4 (but if its too big then 3 is my minimum capcity).

I look through similar cases for people who attempted the same thing, but all the same as it ended failing. All I get is a one column Gridview each time.

I'd like to hear some opinions about it, in fix this issue.

My GridView:

<GridView
    android:id="@+id/gridView"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="73dp"
    android:layout_toEndOf="@+id/imageView15"
    android:columnCount="4"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth" />

My item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:backgroundTint="@android:color/black"
android:alpha="100"
android:layoutDirection="ltr">

<TextView
    android:id="@+id/item"
    android:layout_width="150px"
    android:layout_height="150px"
    android:layout_marginRight="50px"
    android:textColor="@android:color/black"
    android:textSize="10sp" />
</LinearLayout>

And finally the Adapter:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;


public class CustomListAdapter extends BaseAdapter {

    private Activity context;
    private List<TextView> items;
    private static LayoutInflater inflater;

    public CustomListAdapter(Activity context, List<TextView> items) {

        this.context = context;
        this.items = items;
        inflater = (LayoutInflater)     context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return items.size();
    }

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

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

    public class Holder {
        TextView tv;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        inflater = context.getLayoutInflater();

        View rowView = inflater.inflate(R.layout.inventory_item, null);
        Holder holder = new Holder();

        holder.tv = (TextView) rowView.findViewById(R.id.item);
        holder.tv.setText(items.get(position).getText());
        holder.tv.setBackground(items.get(position).getBackground());
        holder.tv.setTextColor(items.get(position).getTextColors());

        return rowView;
    }


}

Thanks in advance.

shows me only 1 column out of 4

you need to use: android:numColumns="4"

in your code:

 <GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:background="@color/color_white"
    android:columnWidth="100dp"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:numColumns="4"
    android:padding="5dp"
    android:scrollbars="none|horizontal"
    android:stretchMode="columnWidth"></GridView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM