我想製作一個選項卡,我必須在gridview中顯示圖片,所以我從庫中的一個製作了正常選項卡,並製作了一個適配器以在我的主活動代碼中顯示圖片。如何在選項卡片段中顯示gridview圖片
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;
}
}
我的片段類。
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);
}
我已經把我的適配器遵循
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;
}
}
我能夠做一個網格視圖中的活動,但在我使用的MainActivity
類申報圖像,但在這裏我得到一個錯誤。 Sso我在適配器中聲明,因爲適配器將調用圖像。這是正確的編譯沒有任何錯誤,但它顯示運行時錯誤 誤差
04-23 10:39:11.950 571-571/? E/Vold: Error reading configuration (No such file or directory)... continuing anyways
那麼如何解決這個問題,並做了我必須聲明的圖像。
內[網格佈局的可能的複製佈局文件標籤](http://stackoverflow.com/questions/23493527/grid-layout-within-tabs) – miken32