我从谷歌名称“viewpager 遇到 swipey tabs”中实现了这个 swipey tabs 代码。现在对于片段活动,每个活动都使用 xml 布局进行设置。我希望这个活动显示一个网格视图。我尝试实现在http://developer.android.com上解释的网格视图示例,但它为 gridview.setAdapter 上的 findgridviewbyid 和 imageadapter 构造函数提供了一些错误。我如何实现网格视图以仅在此片段中显示图像。
public class StockTabFragment extends Fragment {
public static Fragment newInstance(String title) {
StockTabFragment f = new StockTabFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.stock_tab_fragment, null);
return root;
}
private ViewGroup createGridView(ViewGroup root) {
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(StockTabFragment.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
Stock_Tab_Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>