在Android中同时显示两个视图

时间:2014-09-18 18:11:51

标签: java android view

我在Android应用程序中有这个代码:

private void showMyViews() {

    mAnimation.cancel();
    mAnimation.reset();
    mAnimateionView.clearAnimation();
    mAnimationView.setVisibility(View.GONE);
    mOtherViewToHide.setVisibility(View.GONE);

    mFirstViewToShow.setVisibility(View.VISIBLE);
    mSecondViewToShow.setVisibility(View.VISIBLE);

} 

但有时候mSecondViewToShow会在mFirstViewToShow之前出现一点点。我怎么能轻易地强制这些视图同时出现?

部分代码是:

public class mFragment extends Fragment implements OnClickListener, com.squareup.picasso.Callback {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.mFragment, null);
    mFirstViewToShow = (RelativeLayout) view
            .findViewById(R.id.mFirstViewToShow);
    mSecondViewToShow = (RelativeLayout) view
            .findViewById(R.id.mSecondViewToShow);

    ...
    ...
    ...

    if (isConnected()) {

        animateMagnifier();
        updateUserLocation();

    } else {

    }

}
private void animateMagnifier() {

    Thread mThread = new Thread(new Runnable() {

        @Override
        public void run() {

            AppLog.Log(TAG, "ver si la lupa sirve");

            if (getActivity() != null) {

                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        mAnimationView.setVisibility(View.VISIBLE);
                        animeMagnifier = AnimationUtils
                                .loadAnimation(getActivity(),
                                        R.anim.translate_center_amim);
                        mAnimationView.startAnimation(animeMagnifier);

                        // Code use to repeat the animation
                        // See
                        // http://stackoverflow.com/questions/4480652/android-animation-does-not-repeat

                        animeMagnifier
                                .setAnimationListener(new AnimationListener() {

                                    @Override
                                    public void onAnimationStart(
                                            Animation animation) {
                                    }

                                    @Override
                                    public void onAnimationRepeat(
                                            Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(
                                            Animation animation) {
                                        animeMagnifier = AnimationUtils
                                                .loadAnimation(
                                                        getActivity(),
                                                        R.anim.translate_center_amim);
                                        animeMagnifier
                                                .setAnimationListener(this);
                                        mAnimationView
                                                .startAnimation(animeMagnifier);
                                    }
                                });
                    }
                });

            }
        }
    });
    mThread.start();
    try {
        mThread.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void updateUserLocation() {

    ...
    ...
    ...
    sendDataToServer();

}

private void sendDataToServer() {

    ...
    ...
    ...
    findPerson();

}

private void findPerson() {

    new BackGroundTaskForFindPerson().execute();

}

private class BackGroundTaskForFindMatch extends
        AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        try {


            findPeopleResponse = mServices.makeHttpRequest(
                    Constant.find_people, Constant.methodeName,
                    findPeopleValuePairList);
            Gson gson = new Gson();
            ...
            ...

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

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        try {
            if (success) {

                if (mFindPeople.getErrNum() == 2) {

                    // no one found

                    mFirstViewToShow.setVisibility(View.GONE);
                    mSecondViewToShow.setVisibility(View.GONE);

                    messageTextView.setText(R.string.no_one_near);


                } else if (mFindPeople.getErrNum == 3) {
                    ...
                } else {

                    ....

                }
            } else {

                messageTextView.setText(R.string.no_one_new);

            }
        } catch (Exception e) {
            ....
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

}

1 个答案:

答案 0 :(得分:0)

将它们设置为INVISIBLE而不是GONE,这样它们将被附加到您的布局但隐藏。

目前,必须先将它们添加到您的视图层次结构中,然后才能显示,这可能是导致延迟的原因。