1

これについて助けが必要です。向きは横向きの xml ファイルに変更されません。コンテンツ ビューを変更するために onConfiguration Changed を設定すると、すべてのデータが失われます。向きを変更する方法についてのアイデアは、同じデータが使用されますが、xml フォルダーが異なります。xml ファイルはすべて正しく、正しいフォルダーにあります。レイアウト & レイアウトランド

public class ArticleDetailFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);

        Bundle detailBundle = getArguments();
        clickedArticleID = detailBundle.getLong(ArticleFragment.CLICKED_ARTICLE_ID);

        uri = Uri.parse(ArticleContentProvider.CONTENT_URI + "/" + clickedArticleID);

        parent = (SlidingArea) getActivity();

        parent.getSupportActionBar().setDisplayShowCustomEnabled(false);
        parent.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

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



        View V = inflater.inflate(R.layout.kpmg_fragment_article_detail, null);

        reportBar = (LinearLayout) V.findViewById(R.id.kpmg_article_detail_report_bar);
        emailBar = (LinearLayout) V.findViewById(R.id.kpmg_article_detail_email_bar);

        //progressBar = (ProgressBar) V.findViewById(R.id.progress);
        image = (ImageView) V.findViewById(R.id.kpmg_article_detail_image);
        downloadReportImage = (ImageView) V.findViewById(R.id.kpmg_article_detail_download_report_image);
        contactAuthorImage = (ImageView) V.findViewById(R.id.kpmg_article_detail_contact_author_image);

        textTitle = (TextView) V.findViewById(R.id.kpmg_article_detail_title);
        textDate = (TextView) V.findViewById(R.id.kpmg_article_detail_date);
        textCategory = (TextView) V.findViewById(R.id.kpmg_article_detail_category);
        textContent = (TextView) V.findViewById(R.id.kpmg_article_detail_content);
        textPDFSize = (TextView) V.findViewById(R.id.kpmg_article_detail_file_size);

        return V;
    }



    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        parent.getSupportActionBar().setCustomView(R.layout.kpmg_actionbar_article_detail_view);
        parent.getSupportActionBar().setDisplayShowCustomEnabled(true);
        parent.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        String[] projection = { ArticleTable.TABLE_NAME + "." + ArticleTable.ID, 
                ArticleTable.TABLE_NAME + "." + ArticleTable.TITLE,  
                ArticleTable.TABLE_NAME + "." + ArticleTable.DATE_PUBLISHED,  
                ArticleTable.TABLE_NAME + "." + ArticleTable.CONTENT, 
                ArticleTable.TABLE_NAME + "." + ArticleTable.IMAGE_URL,
                ArticleTable.TABLE_NAME + "." + ArticleTable.SLUG,
                ArticleTable.TABLE_NAME + "." + ArticleTable.PDF_SIZE,
                ArticleTable.TABLE_NAME + "." + ArticleTable.PDF_URL,
                ArticleTable.TABLE_NAME + "." + ArticleTable.CONTACT_EMAIL,
                ArticleTable.TABLE_NAME + "." + ArticleTable.IS_FAVOURITE,
                ArticleTable.TABLE_NAME + "." + ArticleTable.SHARE_URL,
                ArticleTable.TABLE_NAME + "." + ArticleTable.VIDEO_URL,
                ArticleCategoryTable.TABLE_NAME + "." + ArticleCategoryTable.CATEGORY_NAME,
                ArticleCategoryTable.TABLE_NAME + "." + ArticleCategoryTable.ICON_NAME};

        String selection = ArticleTable.TABLE_NAME + "." + ArticleTable.ID + "=?";
        String[] selectionArgs = new String[] {"" + clickedArticleID};
        Cursor row = parent.getContentResolver().query(ArticleJoinContentProvider.CONTENT_URI, projection, selection, selectionArgs, null);

        if (row.moveToFirst()) {

            textTitle.setText(row.getString(row.getColumnIndexOrThrow( ArticleTable.TITLE)));
            emailSubject = textTitle.getText().toString();

            String dateStr = row.getString(row.getColumnIndexOrThrow(ArticleTable.DATE_PUBLISHED));
            String formattedDate = AppHelper.calculateRelativeDate(dateStr);
            textDate.setText(formattedDate + " - ");

            textCategory.setText(row.getString(row.getColumnIndexOrThrow( ArticleCategoryTable.CATEGORY_NAME)));

            textContent.setText(row.getString(row.getColumnIndexOrThrow( ArticleTable.CONTENT)));

            textPDFSize.setText(row.getString(row.getColumnIndexOrThrow( ArticleTable.PDF_SIZE)));

            parent.imageLoader.DisplayImage(row.getString(row.getColumnIndexOrThrow( ArticleTable.IMAGE_URL)), image, false);

            //Check if PDF Exists based on slug name

            pdfURL = row.getString(row.getColumnIndexOrThrow(ArticleTable.PDF_URL));
            Log.i("pdf",pdfURL);
            slug = row.getString(row.getColumnIndexOrThrow(ArticleTable.SLUG));

            if (pdfURL == null || pdfURL.equals("")) {
                reportBar.setVisibility(View.GONE);
            }

            contactEmail = row.getString(row.getColumnIndexOrThrow(ArticleTable.CONTACT_EMAIL));

            if (contactEmail == null || contactEmail.equals(""))  {
                emailBar.setVisibility(View.GONE);
            }

            shareURL = row.getString(row.getColumnIndexOrThrow(ArticleTable.SHARE_URL));
            videoURL = row.getString(row.getColumnIndexOrThrow(ArticleTable.VIDEO_URL));
        }

        row.close();

        if (!(pdfURL == null || pdfURL.equals(""))) {

            File file = new File(Environment.getExternalStorageDirectory()+"/" + slug + ".pdf");

            if (file.exists()) {                

                downloadReportImage.setImageResource(R.drawable.kpmg_image_open_report);
                downloadReportImage.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {

                            downloadReportImage.setImageResource(R.drawable.kpmg_image_open_report_pressed);
                            showPdf(slug);

                        } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                            downloadReportImage.setImageResource(R.drawable.kpmg_image_open_report);
                        }
                        return true;
                    }
                });

            } else {

                downloadReportImage.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {

                            if (isNetworkAvailable()) {

                                pDialog = new ProgressDialog(parent);
                                pDialog.setMessage("Downloading file. Please wait...");
                                pDialog.setIndeterminate(false);
                                pDialog.setMax(100);
                                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                pDialog.setCancelable(true);

                                downloadReportImage.setImageResource(R.drawable.kpmg_image_download_report_pressed);
                                new DownloadFileFromURL().execute(pdfURL);

                            }
                            else {
                                Toast toast = Toast.makeText( parent , "You are not connected to the internet. Please check your connection, or try again later",
                                        Toast.LENGTH_SHORT);
                                toast.show();
                            }

                        } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                            downloadReportImage.setImageResource(R.drawable.kpmg_image_download_report);
                        }
                        return true;
                    }
                });

            }

        }

        if (!(contactEmail == null || contactEmail.equals(""))) {

            contactAuthorImage.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {

                        contactAuthorImage.setImageResource(R.drawable.kpmg_image_contact_author_pressed);

                        Intent emailIntent = new Intent(Intent.ACTION_SEND);
                        emailIntent.setType("message/rfc822");
                        emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{contactEmail});
                        emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
                        try {
                            startActivity(Intent.createChooser(emailIntent, "Contact the author"));
                        } catch (android.content.ActivityNotFoundException ex) {
                            Toast.makeText(parent, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                        }   

                    } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                        contactAuthorImage.setImageResource(R.drawable.kpmg_image_contact_author);
                    }
                    return true;
                }
            });
        }

        final View shareDivider = (View) parent.findViewById(R.id.divider1);
        final ImageView shareButton = (ImageView) parent.findViewById(R.id.kpmg_actionbar_image_share);

        if (shareURL == null || shareURL.equals("")) {
            shareDivider.setVisibility(View.GONE);
            shareButton.setVisibility(View.GONE);
        }
        else {

            shareButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
                    i.putExtra(Intent.EXTRA_TITLE, "This is the Title");
                    i.putExtra(Intent.EXTRA_TEXT, shareURL);
                    startActivity(Intent.createChooser(i, "Share URL"));

                }

            }); 
        }

        final ImageView favouriteButton = (ImageView) parent.findViewById(R.id.kpmg_actionbar_image_favourite);

        projection = new String[] {ArticleTable.TABLE_NAME + "." + ArticleTable.ID, ArticleTable.TABLE_NAME + "." + ArticleTable.IS_FAVOURITE};
        selection = ArticleTable.TABLE_NAME + "." + ArticleTable.ID + "=?";
        selectionArgs = new String[] {"" + clickedArticleID};
        Cursor articleRow = parent.getContentResolver().query(ArticleContentProvider.CONTENT_URI, projection, selection, selectionArgs, null);

        articleRow.moveToFirst();

        if (articleRow.getInt(articleRow.getColumnIndexOrThrow( ArticleTable.IS_FAVOURITE )) == 1) {
            Log.i("Status","Is Favourite");
            favouriteButton.setImageResource(R.drawable.kpmg_actionbar_favourite);

        } else {
            Log.i("Status","Is Not Favourite");
            favouriteButton.setImageResource(R.drawable.kpmg_actionbar_not_favourite);

        }

        articleRow.close();

        favouriteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String[] projection = new String[] {ArticleTable.TABLE_NAME + "." 
                + ArticleTable.ID, ArticleTable.TABLE_NAME + "." 
                + ArticleTable.IS_FAVOURITE};
                String selection = ArticleTable.TABLE_NAME + "." + ArticleTable.ID + "=?";
                String[] selectionArgs = new String[] {"" + clickedArticleID};

                Cursor articleRow = parent.getContentResolver().query(ArticleContentProvider.CONTENT_URI, projection, selection, selectionArgs, null);

                articleRow.moveToFirst();

                ContentValues values = new ContentValues();

                if (articleRow.getInt(articleRow.getColumnIndexOrThrow(ArticleTable.IS_FAVOURITE)) == 1) {
                    //Is favourite currently
                    values.put(ArticleTable.IS_FAVOURITE, 0);
                    parent.getContentResolver().update(uri, values, null, null);
                    favouriteButton.setImageResource(R.drawable.kpmg_actionbar_not_favourite);
                } else {
                    //Is not favourite currently
                    values.put(ArticleTable.IS_FAVOURITE, 1);
                    parent.getContentResolver().update(uri, values, null, null);
                    favouriteButton.setImageResource(R.drawable.kpmg_actionbar_favourite);
                }

                articleRow.close();
            }

        }); 

        if (videoURL.length() > 0) {

            image.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoURL)));
                }

            }); 
        }
    }



}
4

2 に答える 2

0

Android : 画面の向きの変更時にアプリケーションの状態を保存しますか? マニフェスト行だけにする必要があります。

そうでない場合は、http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObjectを参照してください。設定変更 (画面の回転など) でオブジェクトをキャプチャしてリロードする方法が説明されています。

于 2012-11-27T17:14:20.537 に答える
0

フラグメントを使用する場合、データを保存することはできませんが、状態を使用することはできます。状態を に保存して、後でまたはonSaveInstanceState()で取得できます。onCreate()onActivityCreate()

その後、FragmentTransaction をバックスタックに追加する必要があります。保存したバンドルの状態を保持できます。

FragmentManager方向を処理し、現在のビューでフラグメントを復元します。

于 2012-11-27T17:14:41.327 に答える