当前位置: 代码迷 >> Android >> Fragment为什么不加载ListView,请问咋回事,
  详细解决方案

Fragment为什么不加载ListView,请问咋回事,

热度:83   发布时间:2016-04-28 06:01:24.0
Fragment为什么不加载ListView,请教怎么回事,急急急

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;
}
};
  相关解决方案