2017-02-05 175 views
0

我想創建一個圖像庫(有很多圖片)。我需要一個GridView來顯示各種類別,並且在每一行中都有一個ImageView和一個TextView。所有圖片都加載到可繪製文件夾中。根據用戶選擇的類別,我需要在新的活動中顯示文件夾的所有圖片。我的問題是用於gridview的適配器。我正在嘗試創建一個自定義的,但很少成功。我發現的所有例子都不適合我的任務...創建一個圖像庫

回答

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(); 

       } 
      });  
     } 
    } 

希望這有助於。 :)

+1

是的,謝謝你,它清除了很多東西:) –