我正在尝试将自定义图像适配器与fragment
和Activity
结合使用。当我使用几乎相同的代码只有一个fragment
时,代码工作正常。但当我使用public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridView = (GridView)rootView.findViewById(R.id.thumbnail_grid);
thumbnails = new ImageAdapter(getActivity());
gridView.setAdapter(thumbnails);
return rootView;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mThumbIds ={
R.drawable.test,
R.drawable.test2
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 0; //FOR NOW
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
//test with drawable folder
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
时,图像不再显示。以下是相关代码
注意:缩略图是我的片段类的全局变量
<FrameLayout 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="com.example.ghanghan.popularmovies.MoviesFragment">
<GridView
android:id="@+id/thumbnail_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
</FrameLayout>
和我的布局
.preview-iframe {
width: 100%;
height: 100%;
border: 0;
max-width: 1280px;
max-height: 720px;
margin: auto;
}
答案 0 :(得分:0)
看看你的getCount()
。它应该返回2。
public int getCount() {
return 0; //FOR NOW
}