创建图片库

时间:2017-02-05 11:44:54

标签: java android gridview custom-adapter

我想创建一个图片库(包含大量图片)。我需要一个GridView来显示各种类别,并且在每一行中都有一个ImageView和一个TextView。所有图片都加载到drawable文件夹中。根据用户选择的类别,我需要在新活动中显示文件夹的所有图片。我的问题是gridview的适配器。我正在尝试创建一个自定义的但没有成功。我找到的所有例子对我的任务都没用...

1 个答案:

答案 0 :(得分:0)

以下是使用文本创建GridView库的示例代码。

首先,将GridView添加到主布局(例如:activity_main.xml)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <GridView
     android:numColumns="auto_fit"
     android:gravity="center"
     android:columnWidth="100dp"
     android:stretchMode="columnWidth"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/grid"/>
</LinearLayout>

然后,您需要创建一个布局来扩展自定义适配器的视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="50dp"
        android:layout_height="50dp">
    </ImageView>

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="9sp" >
    </TextView>

</LinearLayout> 

创建一个自定义适配器,它将接收图像资源数组和字符串。

public class CustomGrid extends BaseAdapter{
      private Context mContext;
      private final String[] names;
      private final int[] Imageid;

        public CustomGrid(Context c,String[] names,int[] Imageid ) {
            mContext = c;
            this.Imageid = Imageid;
            this.names = names;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View grid;
            LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {

                grid = new View(mContext);
                grid = inflater.inflate(R.layout.grid_single, null);
                TextView textView = (TextView) grid.findViewById(R.id.grid_text);
                ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
                textView.setText(names[position]);
                imageView.setImageResource(Imageid[position]);
            } else {
                grid = (View) convertView;
            }

            return grid;
        }
}

最后将此自定义适配器设置为活动中的GridView适配器:

 public class MainActivity extends Activity
    {
        GridView grid;
        String[] names =
        {
            "String1",
            "String2",
            "String3",
            "String4",
            "String5",
            "String6",
            "String8",
            "String9",
            "String10"
        } ;
        int[] imageId =
        {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7,
            R.drawable.image8,
            R.drawable.image9,
            R.drawable.image10
        };

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            CustomGrid adapter = new CustomGrid(MainActivity.this, names, imageId);
            grid=(GridView)findViewById(R.id.grid);
            grid.setAdapter(adapter);
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id)
                {
                    Toast.makeText(MainActivity.this, "You Clicked at " +names[+ position], Toast.LENGTH_SHORT).show();

                }
            });    
        }
    }

希望这会有所帮助。 :)