在每个Viewpager项中执行Thread

时间:2016-07-28 09:26:49

标签: android multithreading android-layout android-viewpager

  • 我在单个viewpager项目中有两个图像。当我点击任何图像时,它会显示一条简单的线条。
  • 我使用线程增长此行,行是给予特定图像的投票百分比,基于点击后在两个图像上绘制的百分比行
  • 但问题是,如果我点击一个图像,则会在该特定图像上绘制单行,并且每个其他viewpager项目都不会在点击时绘制

  • 点击任何图片后我想要两行,所以该怎么做

ivImageOne.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {

        id = arraylist.get(position).get("user_id1");

        feed_id = arraylist.get(position).get("feed_id");
        Log.e("feeeeeee", feed_id);

        View tempView = (View) v.getTag(R.integer.btnminusview);
        Integer pos = (Integer) v.getTag(R.integer.btnminuspos);
        ivRightTick = (ImageView) tempView.findViewById(R.id.ivRightTick);

        ivLeftTick = (ImageView) tempView.findViewById(R.id.ivLeftTick);

        ivLeftTick.setVisibility(View.GONE);
        ivRightTick.setVisibility(View.VISIBLE);
        vOne = (View) tempView.findViewById(R.id.vOne);
        vTwo = (View) tempView.findViewById(R.id.vTwo);

        mThread = new Thread() {
            @Override
            public void run() {

                while (pStatus < pcount1) {
                    pStatus += 10;

                    handler.post(new Runnable() {

                        @Override
                        public void run() {

                            android.view.ViewGroup.LayoutParams params = vOne.getLayoutParams();
                            params.height = pStatus;
                            params.width = 10;
                            Log.d("pppppppppppppp", params.height + "");
                            vOne.setLayoutParams(params);

                            // vOne.setLayoutParams(new WindowManager.LayoutParams(1,pStatus));
                            if (pStatus == pcount1) {
                                stopThread(mThread);
                            }
                        }
                    });
                    try {
                        Thread.sleep(19); //thread will take approx 2.5 seconds to finish
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };


        mThread2 = new Thread() {
            @Override
            public void run() {

                while (pStatus2 > MIN_VALUE) {
                    pStatus2 += 10;

                    handler2.post(new Runnable() {

                        @Override
                        public void run() {

                            android.view.ViewGroup.LayoutParams params = vTwo.getLayoutParams();
                            params.height = pStatus;
                            params.width = 10;
                            Log.d("pppppppppppppp", params.height + "");
                            vTwo.setLayoutParams(params);
                            // vOne.setLayoutParams(new WindowManager.LayoutParams(1,pStatus));
                            if (pStatus2 == pcount2) {
                                stopThread(mThread2);
                            }

                        }
                    });
                    try {
                        Thread.sleep(19); //thread will take approx 2.5 seconds to finish
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };

        mThread.start();
        mThread2.start();
    }
});


ivFeedImageTwo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //  giveVote(2);

        id = arraylist.get(position).get("user_id2");
        feed_id = arraylist.get(position).get("feed_id");

        View tempView = (View) v.getTag(R.integer.btnminusview);
        Integer pos = (Integer) v.getTag(R.integer.btnminuspos);
        ivLeftTick = (ImageView) tempView.findViewById(R.id.ivLeftTick);
        ivRightTick = (ImageView) tempView.findViewById(R.id.ivRightTick);
        ivLeftTick.setVisibility(View.VISIBLE);
        ivRightTick.setVisibility(View.GONE);
        vOne = (View) tempView.findViewById(R.id.vOne);
        vTwo = (View) tempView.findViewById(R.id.vTwo);

        mThread = new Thread() {
            @Override
            public void run() {

                while (pStatus < pcount2) {
                    pStatus += 10;

                    handler.post(new Runnable() {

                        @Override
                        public void run() {

                            android.view.ViewGroup.LayoutParams params = vTwo.getLayoutParams();
                            params.height = pStatus;
                            params.width = 10;
                            Log.d("pppppppppppppp", params.height + "");
                            vTwo.setLayoutParams(params);
                            // vOne.setLayoutParams(new WindowManager.LayoutParams(1,pStatus));
                            if (pStatus == pcount2) {
                                stopThread(mThread);
                            }

                        }
                    });
                    try {

                        Thread.sleep(19); //thread will take approx 2.5 seconds to finish
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


            }
        };

        mThread2 = new Thread() {
            @Override
            public void run() {

                while (pStatus2 > MIN_VALUE) {
                    pStatus2 += 10;

                    handler2.post(new Runnable() {

                        @Override
                        public void run() {
                            android.view.ViewGroup.LayoutParams params = vOne.getLayoutParams();
                            params.height = pStatus;
                            params.width = 10;
                            Log.d("pppppppppppppp", params.height + "");
                            vOne.setLayoutParams(params);
                            // vOne.setLayoutParams(new WindowManager.LayoutParams(1,pStatus));
                            if (pStatus2 == pcount1) {
                                stopThread(mThread2);
                            }
                        }
                    });

                    try {
                        Thread.sleep(19); //thread will take approx 2.5 seconds to finish
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };

        mThread2.start();
        mThread.start();
    }
});

1 个答案:

答案 0 :(得分:0)

以下是您处理紧急问题的方法:

在您的活动/片段上编码一个方法,用于启动两行的动画。然后设置两个相同的OnClickListener s,每个图像上一个,只需调用动画方法。

我更关心的是你使用Thread来制作动画。这不是Android的方式。您应该使用动画师对视图进行视觉更改。代码可以简单如下:

        float pct = ... // ratio of votes to total votes, from 0.0 to 1.0

        vOne.setScaleX(0F);
        vOne.animate().setDuration(150).scaleXBy(pct).start();

请查看Android开发者网站和网络其他地方的一些动画教程。