2012-04-26 43 views
2

横のスクロールで1つの列のグリッドビューにイメージサムネイルを表示したいとします。私は多くのパラメータで試合をしましたが、私が間違っていることを理解できません。親切に誰かが私を助けます。 main.xml:アンドロイドのGridViewの横スクロール

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

    <GridView 
     android:layout_width="500dp" 
     android:layout_height="400dp" 
     android:id="@+id/grid" 
     android:columnWidth="300dp" 
     android:padding="5dp" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" 
     android:scrollbars="horizontal" 
     android:stretchMode="spacingWidthUniform"> 
    </GridView> 
</LinearLayout> 

アクティビティコード:

//---the images to display--- 
Integer[] imageIDs = { 
     R.drawable.library, 
     R.drawable.library, 
     R.drawable.library, 
     R.drawable.library, 
     R.drawable.library, 
     R.drawable.library, 
     R.drawable.library     
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    GridView gridView = (GridView) findViewById(R.id.grid); 
    gridView.setAdapter(new ImageAdapter(this)); 
    gridView.setNumColumns(imageIDs.length); 

} 

public class ImageAdapter extends BaseAdapter 
{ 
    private Context context; 

    public ImageAdapter(Context c) 
    { 
     context = c; 
    } 

    public int getCount() { 
     return imageIDs.length; 
    } 

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

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

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView; 
     if (convertView == null) { 
      imageView = new ImageView(context); 
      imageView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     } else { 
      imageView = (ImageView) convertView; 
     } 
     imageView.setImageResource(imageIDs[position]); 
     return imageView; 
    } 
} 

答えて

0

チェックこの、...

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
<HorizontalScrollView 
     android:id="@+id/horizontalScrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" 
     android:scrollbars="none" > 
    <GridView 
     android:layout_width="500dp" 
     android:layout_height="400dp" 
     android:id="@+id/grid" 
     android:columnWidth="300dp" 
     android:padding="5dp" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" 
     android:scrollbars="horizontal" 
     android:stretchMode="spacingWidthUniform"> 
    </GridView> 
    </HorizontalScrollView> 
</LinearLayout> 
+0

。 –

+0

これは、単一のcolumn.iに対してのみ動作しますが、8つのカラムを試しましたが、動作しませんでした。 –

-1

は、このコードを試してみ

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class Test2Activity extends Activity { 
    /** Called when the activity is first created. */ 
    public Integer[] imageIDs = { 
       R.drawable.library, 
       R.drawable.library, 
       R.drawable.library,  
       R.drawable.library,  
       R.drawable.library, 
       R.drawable.library,   
       R.drawable.library       
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      GridView gridView = (GridView) findViewById(R.id.grid); 
      gridView.setAdapter(new ImageAdapter(this, imageIDs)); 
      gridView.setNumColumns(imageIDs.length); 

    } 

    class ImageAdapter extends BaseAdapter 
    { 
     private Context context; 
     Integer[] imageIDs; 

     public ImageAdapter(Context c, Integer[] imageIDResults) 
     { 
      context = c; 
      imageIDs = imageIDResults; 
     } 

     public int getCount() { 
      return imageIDs.length; 
     } 

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

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

     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      ImageView imageView; 
      if (convertView == null) { 
       imageView = new ImageView(context); 
       imageView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      } else { 
       imageView = (ImageView) convertView; 
      } 
      imageView.setImageResource(imageIDs[position]); 
      return imageView; 
     } 
    } 
} 
+0

このソリューションがうまくいけば教えてください。 – sush

+0

このコードはまさに私のものです。私はそれをテストしましたが、horizo​​ntalviewを動作させませんでした – azad

+0

はい私は自分自身のコードを変更し、私のために働いた..アダプターのコンストラクターで変更を行い、結果をアダプターに渡す – sush

0

main.xml

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:scrollbars="horizontal" > 

<GridView 
    android:layout_width="500dp" 
    android:layout_height="400dp" 
    android:id="@+id/grid" 
    android:columnWidth="300dp" 
    android:padding="5dp" 
    android:horizontalSpacing="100dp" 
    android:verticalSpacing="10dp" 
    android:scrollbars="horizontal" 
    android:stretchMode="spacingWidthUniform"> 
</GridView> 
</HorizontalScrollView> 

+1

これを行う前に、2番目の段落をメモしてください。http://developer.android.com/reference/android/widget/Horizo​​ntalScrollView.html – Trevor

0

あなたはこのような何かを試すことができます表示する画像の多くを持っていない場合:ここでは

public class MyHorizontalView extends HorizontalScrollView { 
    private LinearLayout internalWrapper; 

    public StampsCustomView(Context context) { 
     super(context, null); 

    } 

    public StampsCustomView(Context context, AttributeSet attr) { 
     super(context, attr); 

     setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     setFadingEdgeLength(0); 
     this.setHorizontalScrollBarEnabled(false); 
     this.setVerticalScrollBarEnabled(false); 

     internalWrapper = new LinearLayout(context); 
     internalWrapper.setLayoutParams(new LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     internalWrapper.setOrientation(LinearLayout.HORIZONTAL); 
     addView(internalWrapper); 

    } 

    public void addImages(int[] images) { 
     internalWrapper.removeAllViews(); 
     for (int i = 0; i < images.length; i++) { 
      ImageView iv = new ImageView(getContext()); 
      iv.setImageResource(images[i]); 
      internalWrapper.addView(iv); 
     } 

    } 
} 

私はHorizo​​ntalScrollViewを拡張ビューを持っていると私は追加それに対してlinearLayout。

addImagesという機能があり、画像のリソースIDを入力として表示します。 これを修正して、internalWrapperのビューまたは画像を追加できます。

として、あなたは、このビューを使用することができます:あなたはScrollView内側のListView/GridViewのを入れたいことはありません

<com.example.android.MyHorizontalView 
      android:id="@+id/h_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="30dp" 
      android:orientation="horizontal" > 
     </com.example.android.MyHorizontalView> 

と活動

MyHorizontalView hView=(MyHorizontalView)findViewById(R.id.h_view); 
hView.addImages(<-resource ids->); 
関連する問題