public class HomeFragment extends Fragment {
private Context context;
private View convertView;
private GridView gridview;
private ListView listView;
private HomeListAdapter homeListAdapter;
private HomeFourGridsAdapter fourGridsAdapter;
private ViewPager viewPager;
private int currentItem;
private int[] imageResId;
private ScheduledExecutorService scheduledExecutorService;
private List<ImageView> imageViews;
private String[] titles;
private List<View> dots;
protected final static int INIT_NEWS_IMGS = 101;
protected final static int INIT_NEWS_LIST = 102;
protected final static int INIT_NEWS_BTNS = 103;
private TextView tv_title;
public HomeFragment() {
}
public HomeFragment(Context context) {
this.context = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置Fragment不重建
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
currentItem = 0;
convertView = inflater.inflate(R.layout.fragment_home, null);
initNewsList();
initImgBtns();
initNewsViewPage();
return convertView;
}
@Override
public void onStart() {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 3, 3,
TimeUnit.SECONDS);
super.onStart();
}
@Override
public void onStop() {
scheduledExecutorService.shutdown();
super.onStop();
}
// 初始化滑动图片
private void initNewsViewPage() {
imageResId = new int[] { R.drawable.gallery_photo_1,
R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
R.drawable.gallery_photo_8, R.drawable.gallery_photo_3 };
titles = new String[imageResId.length];
titles[0] = "11111";
titles[1] = "22222";
titles[2] = "33333";
titles[3] = "44444";
titles[4] = "55555";
imageViews = new ArrayList<ImageView>();
for (int i = 0; i < imageResId.length; i++) {
ImageView imageView = new ImageView(this.context);
imageView.setImageResource(imageResId[i]);
imageView.setScaleType(ScaleType.CENTER_CROP);
imageViews.add(imageView);
}
dots = new ArrayList<View>();
dots.add(convertView.findViewById(R.id.v_dot0));
dots.add(convertView.findViewById(R.id.v_dot1));
dots.add(convertView.findViewById(R.id.v_dot2));
dots.add(convertView.findViewById(R.id.v_dot3));
dots.add(convertView.findViewById(R.id.v_dot4));
tv_title = (TextView) convertView.findViewById(R.id.tv_title);
tv_title.setText(titles[0]);
viewPager = (ViewPager) convertView.findViewById(R.id.vp);
viewPager.setAdapter(new HomeNewsViewPaperAdapter(imageResId,
imageViews));
viewPager.setOnPageChangeListener(new NewsViewPageChangeListener());
}
// 初始化新闻列表
private void initNewsList() {
listView = (ListView) convertView.findViewById(R.id.home_news_listview);
homeListAdapter = new HomeListAdapter(this.context, new int[] {
drawable.app_logo, drawable.app_logo }, new String[] { "1111",
"1111" }, new String[] { "2222", "2222" });
Message message = handler.obtainMessage();
message.what = this.INIT_NEWS_LIST;
handler2.handleMessage(message);
}
// 初始化首页的四个按钮
private void initImgBtns() {
gridview = (GridView) convertView.findViewById(R.id.home_grilView);
fourGridsAdapter = new HomeFourGridsAdapter(this.context, new int[] {
drawable.app_logo, drawable.app_logo, drawable.app_logo,
drawable.app_logo });
Message message = handler.obtainMessage();
message.what = this.INIT_NEWS_BTNS;
handler2.handleMessage(message);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(currentItem);
};
};
private Handler handler2 = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case HomeFragment.INIT_NEWS_LIST:
listView.setAdapter(homeListAdapter);
homeListAdapter.notifyDataSetChanged();
break;
case HomeFragment.INIT_NEWS_BTNS:
gridview.setAdapter(fourGridsAdapter);
fourGridsAdapter.notifyDataSetChanged();
break;
}
};
详细解决方案
Fragment为什么不加载ListView,请问咋回事,
热度:83 发布时间:2016-04-28 06:01:24.0
相关解决方案
- ListView 中加入 EditText后的焦点有关问题
- android ListView 中的onItemClick Intent 没法跳转
- asp.net ListView 控件导出EXCEL档 出现一点有关问题身份证号后3位全部变成0,怎么解决哦
- C# ListView Item 焦点获取 有关问题 (winform) 控件高手别走。
- .Net 控件: ListView + Treeview + CheckBox,该如何处理
- ListView 怎么设置控件某行的高度
- ListView,该怎么解决
- 必须在 ListView“ListView2”下指定项占位符
- ListView 控件怎么增加 编辑,删除,更新按钮
- listview 与 repeater 实施效率是不是一样高
- ListView+DataPager 分页性能怎么
- ASP listview 动态更改selectCommand,该怎么处理
- asp.net3.5 listview 如何样鼠标放在行上整行高亮显示 选择
- gridview,datalist,listview,repeater获取选中行的有关问题
- ListView+DataPager如何实现只取当前页记录并实现分页
- ListView 绑定ImageList 的 有关问题.
- listview 图标为何有时会出现排列图形乱的现象,该如何处理
- 怎么让richtextbox,表格,listview 等控件的背景透明?或半透明
- ListView 绑定 更改通知解决思路
- ListView Item 与子控件焦点有关问题
- JAVA SWING中如何 实现 ListView 类似 的 控件
- listview 有最大宽度限制吗
- 片断合并(fragment merging)
- ListView-BaseAdapter
- fragment+ViewPager+listview更新显示与更新问题
- android listview NFC标签扫描解决思路
- android listview 绑定数据的有关问题
- android listview 加图片和文字有关问题
- 请教要使用什么控件或方法,实现不同颜色字体的 listview?(如图)
- Android ListView 有关问题汇总