Gridview编号

时间:2014-05-09 10:43:02

标签: android android-layout gridview

我有这个gridview。是否有可能在gridview一侧有数字,如excel 1,2,3或a,b,c,d

我的代码

<?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>

我想要实现的目标

enter image description here

2 个答案:

答案 0 :(得分:0)

您必须为每个单元格添加编号,并且只有在最左边的单元格时才显示它。

单元格布局

<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>

在适配器中

@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;
}

注意:为简单起见,我没有使用ViewHolder pattern。但你应该!

答案 1 :(得分:0)

// 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;
        }
    }
}