我正在尝试实现图像网格。我通过参考这个链接在一个活动中做到了这一点。 现在,如果我尝试在扩展 Fragment 的类中实现相同的东西,我已经做到了
package com.example.emergency1;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.GridView;
public class City1 extends Fragment {
public static Fragment newInstance(Context context) {
City1 f = new City1();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.city1, null);
GridView gv1=(GridView)getView().findViewById(R.id.gridview);
gv1.setAdapter(new ImageAdapter(this));
return root;
}
}
和适配器
package com.example.emergency1;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context ctx;
public ImageAdapter(Context c)
{
ctx=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.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
ImageView iv;
if (convertView == null) { // if it's not recycled, initialize some attributes
iv = new ImageView(ctx);
iv.setLayoutParams(new GridView.LayoutParams(350,350));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
private Integer[] pics={
R.drawable.sample_0,R.drawable.sample_1,
R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_4,R.drawable.sample_5,
R.drawable.sample_6,R.drawable.sample_7
};
}
在这里,我在线上遇到错误
gv1.setAdapter(new ImageAdapter(this));
因为我在适配器类中没有相应的构造函数。我该如何解决这个问题。您能否也将我重定向到某个链接,我可以在其中了解有关如何在片段中实现活动 UI 元素的更多信息?谢谢