Domanda

I have this gridview. is it possible to have numbers on the side of gridview like in excel 1,2,3 or a, b, c ,d

my code

<?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"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"   
        android:numColumns="4" 
        android:stretchMode="columnWidth"  />

</RelativeLayout>

something i want to achieve

enter image description here

È stato utile?

Soluzione

You'll have to add numbering to each of the cells and only display it when it is the leftmost cell.

Cell layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/row_number"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- The rest of your layout -->

    </LinearLayout>

</LinearLayout>

In your Adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = LayoutInflater.from(context).inflate(R.layout.grid_cell);
    TextView rowNumber = (TextView) v.findViewById(R.id.row_number);
    if (position % colNum == 0) { // colNum: number of columns
        int row = position / colNum + 1; // get the row number
        rowNumber.setText(String.valueOf(row));
        rowNumber.setVisibility(View.VISIBLE);
    } else {
        rowNumber.setVisibility(View.GONE);
    }
    return v;
}

Note: For the sake of simplicity I am not using the ViewHolder pattern. But you should!

Altri suggerimenti

// try this way hope this will help you...

grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">

    <TextView
        android:id="@+id/txtGridItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"/>

    <ImageView
        android:id="@+id/imgGridItem"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>

</LinearLayout>

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:stretchMode="columnWidth"  />

</LinearLayout>

MainActivity.java
public class MainActivity extends Activity {

    private GridView gridview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridview = (GridView) findViewById(R.id.gridview);

        ArrayList<String> images = new ArrayList<String>();
        images.add("image1path");
        images.add("image2path");
        images.add("image3path");
        images.add("image4path");
        images.add("image5path");
        images.add("image6path");
        images.add("image7path");
        images.add("image8path");
        images.add("image9path");
        images.add("image10path");
        images.add("image11path");
        images.add("image12path");
        gridview.setAdapter(new GridAdapter(images,this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,String.valueOf(position+1),Toast.LENGTH_SHORT).show();
            }
        });
    }

    class GridAdapter extends BaseAdapter{
        private Context context;
        private ArrayList<String> images;
        public GridAdapter(ArrayList<String> images,Context context){
            this.context = context;
            this.images = images;
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.grid_item,null,false);
                holder.imgGridItem = (ImageView) convertView.findViewById(R.id.imgGridItem);
                holder.txtGridItem = (TextView) convertView.findViewById(R.id.txtGridItem);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            if(position%4 == 0){
                holder.txtGridItem.setVisibility(View.VISIBLE);
                holder.txtGridItem.setText(String.valueOf((position / 4)+1));
            }else{
                holder.txtGridItem.setVisibility(View.GONE);
            }
            holder.imgGridItem.setImageResource(R.drawable.ic_launcher);
            return convertView;
        }

        class ViewHolder{
            ImageView imgGridItem;
            TextView txtGridItem;
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top