【问题标题】:How to implement timer into Automatically image slide inside the fragment?如何将计时器实现到片段内的自动图像幻灯片中?
【发布时间】:2023-03-09 17:53:01
【问题描述】:

如何实现定时器到片段内的自动图像幻灯片? 我使用了 Fragment 和 CustomSwipeAdapter,但我不知道在哪里放置计时器以使图像自动滑动。是在 Fragment 中还是在 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);
    }

}

【问题讨论】:

标签: android android-fragments android-adapter timertask


【解决方案1】:
Handler h = new Handler();
int delay = 15000; //1 second
Runnable runnable;
private int[] pagerIndex = {-1};
@Override
public void onStart() {


    h.postDelayed(new Runnable() {
        public void run() {
           pagerIndex[0]++;
    if (pagerIndex[0] >= adapter.getCount()) {
        pagerIndex[0] = 0;
    }

    viewPager.setCurrentItem(pagerIndex[0]);
            runnable=this;

            h.postDelayed(runnable, delay);
        }
    }
    , delay);

    super.onStart();
}

【讨论】:

    【解决方案2】:

    您可以在Fragment 中启动定时任务。 尚未验证此代码,但它应该可以正常工作。 创建您的滑动任务:

    final long delay = 2000;
    Handler handler = new Handler();
    private int[] pagerIndex = {-1};
    private Runnable swipeTask = new Runnable() {
        @Override
        public void run() {
    
            pagerIndex[0]++;
            if (pagerIndex[0] >= adapter.getCount()) {
                pagerIndex[0] = 0;
            }
    
            viewPager.setCurrentItem(pagerIndex[0]);
            handler.postDelayed(this, delay);
        }
    };
    

    在您的片段的onCreateView() 中,在设置好viewPager、适配器等之后,

    task.run();
    

    【讨论】:

    • postDelayed 有错误。无法解析方法(匿名 java.lang .Runnable,long)
    • @Candy083 处理程序应该是 android.os.Handler,而不是 java.util.logging.Handler
    【解决方案3】:
    private int currentPage = 0;
    final long DELAY = 1000;//delay in milliseconds before auto sliding starts.
    final long PERIOD = 4000; //time in milliseconds between sliding.
    
    private void autoScroll(){
        final Handler handler = new Handler();
        final Runnable Update = new Runnable() {
            public void run() {
                if (currentPage == YourImageList.size()) {
                    currentPage = 0;
                }
                viewPager.setCurrentItem(currentPage++, true);
            }
        };
    
        timer = new Timer(); // creating a new thread
        timer .schedule(new TimerTask() { // task to be scheduled
    
            @Override
            public void run() {
                handler.post(Update);
            }
        }, DELAY_MS, PERIOD_MS);
    }
    

    在您的片段的 onCreateView() 中调用 autoScroll(),您的视图分页适配器已设置。

    viewPager.setAdapter(ViewPagerAdapter)
    autoScroll()
    

    如果您必须在片段之间进行更改,请记住在片段的 onDetach() 方法中取消计时器,如下所示以避免任何问题。

     @Override
    public void onDetach() {
        super.onDetach();
        if(timer != null) {
           timer.cancel();
            timer = null;
        }
    }
    

    希望这会对某人有所帮助。因为我个人在移动到另一个片段并返回到同一个片段时遇到了滑动问题,因为我在移动到另一个片段之前没有取消计时器。

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多