0

グリッドビューで画像を表示する必要があるタブを作成しようとしているので、ライブラリの1つから通常のタブを作成し、メインアクティビティコード。グリッドビュー画像をタブフラグメントで表示する方法

class TestAdapter extends FragmentPagerAdapter { 
public TestAdapter (FragmentManager manager) { 
    super(manager); 
} 

@Override 
public Fragment getItem(int i) { 
    Fragment fragment=null; 
    if (i==0){ 
     fragment=new One(); 
    } 
    if (i==1){ 
     fragment=new Two(); 
    } 
    if (i==2){ 
     fragment=new Three(); 
    } 
    return fragment; 
} 

@Override 
public int getCount() { 
    return 3; 
} 
@Override 
public CharSequence getPageTitle(int position) { 
    String title = new String(); 
    if (position==0){ 
     return "tab1"; 
    } 

    if (position==1){ 
     return "tab2"; 
    } 
    if (position==2){ 
     return "tab3"; 
    } 
    return title; 
    } 
} 

myフラグメントクラス。私が活動にグリッドビューを作ることができています

public class MyAdapter extends BaseAdapter { 
public Integer [] imageIDs =  
{ 
    R.drawable.img1, 
    R.drawable.img2, 
    R.drawable.img3, 
    R.drawable.img4, 
    R.drawable.img5, 
    R.drawable.img6, 
    R.drawable.img7, 
    R.drawable.img8, 
    R.drawable.img9, 
    R.drawable.img10 
}; 

private Context context; 
public MyAdapter(Context c){ 
    context=c; 
} 

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

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

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

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

    return imageView; 
    } 
} 

に従うよう

public class One extends Fragment { 
    public Integer [] imageIDs =  { 
    R.drawable.img1, 
    R.drawable.img2, 
    R.drawable.img3, 
    R.drawable.img4, 
    R.drawable.img5, 
    R.drawable.img6, 
    R.drawable.img7, 
    R.drawable.img8, 
    R.drawable.img9, 
    R.drawable.img10 
}; 

public One() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_one,container,false); 
    GridView gridView = (GridView) view.findViewById(R.id.gridView); 
    gridView.setAdapter(new MyAdapter(view.getContext())); // uses the view to get the context instead of getActivity(). 
    return view; 
} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 

は、私は私のアダプタを設定しているが、その中で、私はMainActivityクラスで画像を宣言するために使用しますが、ここで私は取得していますエラー。私はアダプターが画像を呼び出すので、アダプターで宣言しました。これは、誤りなく正しくコンパイルされているが、ので、これを修正する方法を

04-23 10:39:11.950 571-571/? E/Vold: Error reading configuration (No such file or directory)... continuing anyways 

として、実行時エラー エラーを示しているとした私は、画像を宣言しなければなりませんの。

+0

が重複する可能性の中に示すように、ImageViewのとのTextViewが含まれているレイアウトファイルで、この

public class MyAdapter extends BaseAdapter { private Context mContext; private final String[] gridViewString; private final int[] gridViewImageId; public MyAdapter(Context context, String[] gridViewString, int[] gridViewImageId) { mContext = context; this.gridViewImageId = gridViewImageId; this.gridViewString = gridViewString; } @Override public int getCount() { return gridViewString.length; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View convertView, ViewGroup parent) { View gridViewAndroid; LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { gridViewAndroid = new View(mContext); gridViewAndroid = inflater.inflate(R.layout.gv_services_image_text, null); TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.TxtViewservices); ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.ImgViewservices); textViewAndroid.setText(gridViewString[i]); imageViewAndroid.setImageResource(gridViewImageId[i]); } else { gridViewAndroid = (View) convertView; } return gridViewAndroid; } } 

のようになります。タブ](http://stackoverflow.com/questions/23493527/grid-layout-within-tabs) – miken32

答えて

0

以下のように画像を定義してから、タブを選択したことに応じてグリッドビュー画像またはテキストビューでフラグメントを拡大します。 activity_gv_servicesがグリッド表示をホストとgv_servicesレイアウトファイルである

public class CardsFragment extends Fragment { 
    private static final String ARG_POSITION = "position"; 

    private int position; 

    GridView servicesGridView; 

    String[] gridViewString = { 
      "PROTECTION", "Child Registration", "Repatriation", "rsd", "REFERRAL", "Resettlement",}; 

    int[] gridViewImageId = { 
      R.drawable.protection, R.drawable.childregistration, 
      R.drawable.repatriation, R.drawable.rsd, 
      R.drawable.refferal, R.drawable.resettlement,}; 

    //Create a new instance of a fragment at a specific pisition 
    public static CardsFragment newInstance(int position) { 
     CardsFragment f = new CardsFragment(); 
     Bundle b = new Bundle(); 
     b.putInt(ARG_POSITION, position); 
     f.setArguments(b); 
     return f; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     position = getArguments().getInt(ARG_POSITION); 
    } 

    //Populate the fragment with TextViews and grid view images 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     if (position == 1) { 

      View rootView = inflater.inflate(R.layout.activity_gv_services, container, false); 

      // Here we inflate the layout we created above 
      GridView gridView = (GridView) rootView.findViewById(R.id.gv_services); 
      gridView.setAdapter(new GV_ServicesAdapter(getActivity().getApplicationContext(),gridViewString, gridViewImageId)); 
      return rootView; 
     }else { 
      FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
        FrameLayout.LayoutParams.MATCH_PARENT); 

      FrameLayout fl = new FrameLayout(getActivity()); 
      fl.setLayoutParams(params); 

      final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources() 
        .getDisplayMetrics()); 
      TextView v = new TextView(getActivity()); 
      params.setMargins(margin, margin, margin, margin); 
      v.setLayoutParams(params); 
      v.setLayoutParams(params); 
      v.setGravity(Gravity.CENTER); 
      v.setBackgroundResource(R.drawable.background_card); 
      v.setText("CARD " + (position + 1)); 

      fl.addView(v); 
      return fl; 
     } 
    } 
} 

グリッドビュー自体です。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<GridView 
android:id="@+id/gv_services" 
    android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:columnWidth="110dp" 
android:gravity="center" 
android:numColumns="auto_fit" /> 

</LinearLayout> 

を示すように、その後、あなたのMyAdapterクラスがgv_services_image_textは、[グリッドレイアウトの

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gv_services_image_text" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical"> 

<ImageView 
android:id="@+id/ImgViewservices" 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:layout_marginTop="15dp" /> 

<TextView 
android:id="@+id/TxtViewservices" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginTop="5dp" 
android:textSize="12sp" /> 

</LinearLayout> 
+0

このスタックフローを見てみましょう[リンク](http:// stackoverflow。 com/questions/23493527/grid-layout-within-tabs)。同じ問題のように見え、彼らはそれをとてもうまく説明しました。あなたが選別されることを願って – crakama

関連する問題