1

我正在使用 Timertask 使用 viewpager 滚动图像。我需要显示所有图像,然后它会自动移动到类别(无点击操作)。

    public class GalleryActviity extends AppCompatActivity {
        Timer timer;
        LinearLayout images_lay;
        ArrayList<String> arraylist = new ArrayList<String>();
        List<String> tempimages = new ArrayList<String>();
        ViewPager   mPager ;
       private static int currentPage = 0
        List<String> dealimages = new ArrayList<>();
        ArrayList<DetailImage> detail_images = new ArrayList<DetailImage>();
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gallery);
            images_lay =(LinearLayout)findViewById(R.id.images_lay);
            mPager = (ViewPager) findViewById(R.id.pager);
            Intent in = getIntent();
            Log.v("Tag_resid",""+in.getStringExtra("restid"));
            String restid = in.getStringExtra("restid");
            restaurntrestid(restid);

        }
        private void restaurntrestid(String restid) {
            ServiceClient serviceClient = ServiceUtil.getServiceClient();

            serviceClient.restaurntrestid(restid, restidcallback);

        }

        Callback<JsonObject> restidcallback = new Callback<JsonObject>() {

            @Override
            public void success(final JsonObject cusinerestaurantsinfo, Response response) {

                imagesDeatail(cusinerestaurantsinfo);

            }

            @Override
            public void failure(RetrofitError error) {

            }
        };

        private void imagesDeatail(JsonObject cusinerestaurantsinfo) {
            try {
                JsonArray restaurant_imagesarray = cusinerestaurantsinfo.get("restaurant_images")
                        .getAsJsonArray();
                for (int i = 0; i < restaurant_imagesarray.size(); i++) {
                    String url = restaurant_imagesarray.get(i).getAsJsonObject().get("url").getAsString();
                    String type = restaurant_imagesarray.get(i).getAsJsonObject().get("type").getAsString();
                    if(!arraylist.contains(type)){
                        arraylist.add(type);
// type means category like food, menu, logo...etc(dynamic data)

                    }
                    dealimages.add(url);
                    DetailImage detail = new DetailImage();
                    detail.setType(type);
                    detail.setUrl(url);
                    detail_images.add(detail);

                }
                mPager.setAdapter(new DealAdapter(GalleryActviity.this, dealimages));
                imageRotator(1);

                imageshow();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        public void imageRotator(int seconds) {
            currentPage = 0;
            timer = new Timer();
            timer.scheduleAtFixedRate(new ImageRotateTask(), 0, seconds * 3000);


        }

        class  ImageRotateTask extends TimerTask {

            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    public void run() {


                        if (currentPage == dealimages.size() ) {

                           timer.cancel();


                            //Something here
                        }
                        else {

                            mPager.setCurrentItem(currentPage++, true);
                        }

                    }
                });


            }

        }
        private void imageshow(){
            for(int i = 0; i < arraylist.size(); i++) {
                final Button txtview = new Button(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(0, 0, 0, 0);
                if(i == 0){
                    txtview.setText("All");
                    txtview.setBackgroundColor(getResources().getColor(R.color.navigationBarwhite));
                    txtview.setTextColor(getResources().getColor(R.color.colorPrimary));
                }
              else {

                    txtview.setText(arraylist.get(i));
                    txtview.setBackgroundColor(getResources().getColor(R.color.navigationBarwhite));
                    txtview.setTextColor(getResources().getColor(R.color.navigationBarColor));
                }
                txtview.setLayoutParams(layoutParams);
                txtview.setTextSize(12);
                txtview.setAllCaps(false);
                txtview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!txtview.getText().toString().equalsIgnoreCase("All")){
                            //image_display(txtview.getText().toString().trim());
                            txtview.setBackgroundColor(getResources().getColor(R.color.navigationBarwhite));
                            txtview.setTextColor(getResources().getColor(R.color.colorPrimary));



                        }
                        else   if(txtview.getText().toString().equalsIgnoreCase("All")){
                            imageRotator(1);
                            mPager.setAdapter(new DealAdapter(GalleryActviity.this, dealimages));

                        }

                    }
                });

                images_lay.addView(txtview);
            }
        }
    }

在这里,我在“全部”部分显示所有图像。如何显示每个类别的剩余图像。我添加了将显示图像的屏幕截图。“全部”表示将在此部分显示的每个类别类型的图像。

在这里,我可以显示“全部”部分中的所有图像,现在如何移动以根据类别自动显示图像。

在此处输入图像描述

4

0 回答 0