Thursday, April 24, 2014

Fragment Why not load ListView, ask how it happened, hastily


 
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 imageViews;
private String [] titles;
private List 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);
/ / Set Fragment not rebuild
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 ();
}

/ / Initialize the sliding picture
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 ();

for (int i = 0; i ImageView imageView = new ImageView (this.context);
imageView.setImageResource (imageResId [i]);
imageView.setScaleType (ScaleType.CENTER_CROP);
imageViews.add (imageView);
}

dots = new ArrayList ();
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 ());
}

/ / Initialize the news list
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);
}

/ / Initialize the four buttons
Homeprivate 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;
}
};
};

/ / Picture Scroll threads
private class ScrollTask ​​implements Runnable {

public void run () {
synchronized (viewPager) {
currentItem = (currentItem + 1)% imageViews.size ();
handler.obtainMessage () sendToTarget ();.
}
}

}

/ / Listener touch slide images
private class NewsViewPageChangeListener implements OnPageChangeListener,
OnClickListener {
private int oldPosition = 0;

public void onPageSelected (int position) {
currentItem = position;
tv_title.setText (titles [position]);
. dots.get (oldPosition) setBackgroundResource (R.drawable.dot_normal);
. dots.get (position) setBackgroundResource (R.drawable.dot_focused);
oldPosition = position;
}

public void onPageScrollStateChanged (int arg0) {

}

public void onPageScrolled (int arg0, float arg1, int arg2) {

}

@ Override
public void onClick (View v) {
/ / Jump
}
}

}

 
public class HomeListAdapter extends BaseAdapter {

Context context;
int [] imgs;
String [] titles;
String [] contents;
int count;

public HomeListAdapter (Context context, int [] imgs, String [] titles,
String [] contents) {
this.context = context;
this.imgs = imgs;
this.titles = titles;
this.contents = contents;
}

@ Override
public int getCount () {
/ / TODO Auto-generated method stub
return count;
}

@ 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) {
Log.e ("", "listView");
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from (this.context). inflate (
R.layout.item_home_newslist, null);
holder = new ViewHolder ();
holder.imgView = (ImageView) convertView.findViewById (R.id.row_img);
holder.title = (TextView) convertView.findViewById (R.id.row_title);
holder.content = (TextView) convertView
. FindViewById (R.id.row_content);
convertView.setTag (holder);
} Else {
holder = (ViewHolder) convertView.getTag ();
}

holder.imgView.setBackgroundColor (imgs [position]);
holder.title.setText (titles [position]);
holder.content.setText (contents [position]);
return convertView;
}

private class ViewHolder {
TextView title;
TextView content;
ImageView imgView;
}

}


Below there is a list, but did not load .....
First time Fragment, I could not be loaded if not ListFragment ListView, I do not know why there can not be loaded, how to write is not loaded, and no abnormal, debugging Adapter, do not jump into getView inside.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Constructors HomeListAdapter's assignment to count
Reply:
cited a floor hjywyj reply: constructor
HomeListAdapter to count in the assignment

Can!!! My God, that was the reason, thank you very much, and finally there is progress in the ..., knot stickers, and give you a

No comments:

Post a Comment