In app I have ListView when is clicked, new fragment will apear. In new fragment is picture. But some of them are portrait and some of theme are landscape. I want to create other fragment when picture is landscape. Where can I define which fragment I want to open? Any tutorials, examples?

OPEN NEW FRAGMENT:

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

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        Resources res = this.getResources();
        location = res.getStringArray(R.array.streets);
        authorsNicks = res.getStringArray(R.array.authors_nicks);
        imageMain = res.getIntArray(R.array.images);

        if(??????????????)
           return inflater.inflate(R.layout.menu_fragment, container, false);
        else
           return inflater.inflate(R.layout.menu_fragment, container, false);
    }

In XML I have two files: one for PORTRAIT picture and one for LANDSCAPE.

有帮助吗?

解决方案

If you add your fragment to your activity programmatically, you can check to see if your picture is landscape or portrait, and then launch whichever fragment you'd like. See the documentation here:

http://developer.android.com/guide/components/fragments.html#Adding

Your code will look something like this

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if (landscapeImage) {
    LanscapeFragment fragment = new LandscapeFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
} else {
    PortraitFragment fragment = new PortraitFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
}
fragmentTransaction.commit(); 

In order to determine whether your image is landscape or portrait - by which I assume you mean whether the image is wider than it is tall or not, you could load the image into a bitmap, and check its width vs its height.

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
boolean landscape = bm.getWidth() > bm.getHeight();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top