我使用了Fragment和CustomSwipeAdapter,我不知道把计时器放在哪里,让图像自动滑动。它是在片段内还是在CustomSwipe Adpater中?
这是针对片段的:
public class PrimaryFragment extends Fragment {
public static ViewPager viewPager1;
CustomSwipeAdapter adapter;
public PrimaryFragment() {
// Required empty public constructor
}
private String[] health ={"Abdominal Disorder","Abrasion","Aches","Anxiety","Bruises","Burns or Scalds","Constipation","Coughs" +
"","Cramps","Diarrhea","Dizziness","Dyspepsia","Epilepsy","Fever","Gastric Problem" +
"","High Blood Pressure","Hydrophobia","Tonsillitis"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return inflater.inflate(R.layout.primary_layout,null);
//((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("First Aid and Survival Tips");
View rootView = inflater.inflate(R.layout.primary_layout,container,false);
viewPager1 = (ViewPager) rootView.findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager1.setAdapter(adapter);
ListView lv = (ListView) rootView.findViewById(R.id.oneListView);
ArrayAdapter adapter = new ArrayAdapter(this.getActivity(),android.R.layout.simple_list_item_1,health);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Toast.makeText(getActivity(),health[position],Toast.LENGTH_SHORT).show();
if(position == 0) {
Intent intent = new Intent(view.getContext(), abdominal.class);
startActivity(intent);
}
if(position == 1) {
Intent intent = new Intent(view.getContext(), abrasion.class);
startActivity(intent);
}
if(position == 2) {
Intent intent = new Intent(view.getContext(), aches.class);
startActivity(intent);
}
if(position == 3) {
Intent intent = new Intent(view.getContext(), allergies.class);
startActivity(intent);
}
if(position == 4) {
Intent intent = new Intent(view.getContext(), bruises.class);
startActivity(intent);
}
if(position == 5) {
Intent intent = new Intent(view.getContext(), burns.class);
startActivity(intent);
}
if(position == 6) {
Intent intent = new Intent(view.getContext(), constipation.class);
startActivity(intent);
}
if(position == 7) {
Intent intent = new Intent(view.getContext(), coughs.class);
startActivity(intent);
}
if(position == 8) {
Intent intent = new Intent(view.getContext(), cramps.class);
startActivity(intent);
}
if(position == 9) {
Intent intent = new Intent(view.getContext(), diarrhea.class);
startActivity(intent);
}
// if(position == 10) {
// Intent intent = new Intent(view.getContext(), digestive.class);
// startActivity(intent);
// }
if(position == 10) {
Intent intent = new Intent(view.getContext(), dizziness.class);
startActivity(intent);
}
if(position == 11) {
Intent intent = new Intent(view.getContext(), dyspepsia.class);
startActivity(intent);
}
if(position == 12) {
Intent intent = new Intent(view.getContext(), epilepsy.class);
startActivity(intent);
}
if(position == 13) {
Intent intent = new Intent(view.getContext(), fever.class);
startActivity(intent);
}
if(position == 14) {
Intent intent = new Intent(view.getContext(), gastric.class);
startActivity(intent);
}
if(position == 15) {
Intent intent = new Intent(view.getContext(), highblood.class);
startActivity(intent);
}
if(position == 16) {
Intent intent = new Intent(view.getContext(), hydrophobia.class);
startActivity(intent);
}
if(position == 17) {
Intent intent = new Intent(view.getContext(), tonsilitis.class);
startActivity(intent);
}
}
});
return rootView;
}}
public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resource = {R.drawable.one, R.drawable.two, R.drawable.three};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public int getCount() {
return image_resource.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
/*TextView textView = (TextView) item_view.findViewById(R.id.image_count);*/
imageview.setImageResource(image_resource[position]);
/* textView.setText("" + position);*/
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}