2014-10-06 123 views
0

我是Android新手,我正在使用API​​版本4.4。我使用ViewPager完全按照此鏈接中的描述顯示片段。 http://developer.android.com/training/animation/screen-slide.htmlAndroid ViewPager當前頁面更新發生在下一頁

我的應用程序所做的是讓用戶從相機拍攝照片,當他點擊下一個按鈕時,他可以添加另一個圖像,他可以繼續這樣做(也可以來到上一頁)。我在一個imageView中顯示他的圖片。

我更新片段中onActivityResult方法片段中的imageView。問題是,當我更改imageView(到他的相機圖片)它發生在viewPager中的下一頁,而不是當前頁面。另外我注意到的是我的Fragment類中的onResume函數在頁面第一次加載時會多次觸發。

這是到目前爲止我的代碼,

MainActivity.java

private static final int MAX_IMAGES = 10; 
protected int currentIndex=0; // Which page we are currently in 
private ImageFragment fragment; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    viewPager = (ViewPager)findViewById(R.id.viewPager); 
    pageAdapter = new TicketImageAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(pageAdapter); 

    //LinearLayout uploadImage = ((LinearLayout)findViewById(R.id.uploadImage)); 
    Button nextButton   = (Button)findViewById(R.id.nextBttn); 
    Button saveButton   = (Button)findViewById(R.id.saveButton); 
    Button previousButton  = (Button)findViewById(R.id.previousBttn); 

    nextButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if(fragment != null) 
      { 
       fragment.addTicketImage(); 
       displayTicketImages(); 
      } 

      if(MAX_IMAGES > currentIndex) 
      { 
       currentIndex++; 
       Log.d("Currect Index; ",currentIndex+""); 
       setCurrentItem (currentIndex, true); 
      } 
     } 
    }); 
// Set current page fragment so I can access it's methods 
public void setFragment(TicketImageFragment fragment) 
{ 
    this.fragment = fragment; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //Don't worry about fragment been null, coz it is not. I Set the fragment in the on 
    if(this.fragment != null) 
    { 
     this.fragment.onActivityResult(requestCode, resultCode, data); 
    } 
} 

    public static class TicketImageAdapter extends FragmentPagerAdapter { 

    public TicketImageAdapter(FragmentManager fragmentManager) { 
     super(fragmentManager); 
    } 

    @Override 
    public int getCount() { 
     return MAX_IMAGES; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return new ImageFragment(); 
    } 
} 

}

ImageFragment.java

這是我的普通用戶重定向到相機並在imageView中顯示他的照片,如預覽。

private TextView firstText; 
private MainActivity activity; 
View view; 
ImageView imgPreview; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    view = inflater.inflate(R.layout.activity_fragment,container,false); 

    LinearLayout uploadImageBttn = ((LinearLayout)view.findViewById(R.id.uploadImageButton)); 
    firstText     = ((TextView)view.findViewById(R.id.note)); 



    imgPreview = (ImageView)view.findViewById(R.id.ticketImage); 

    uploadImageBttn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      activity.helper = new CameraHelper(v.getContext()); 
      activity.helper.captureImage(); // Capture image and save it 
     } 
    }); 

    this.activity = (TicketImageActivity)getActivity(); 

    // Set fragment So the activity can call onActivityResult on this fragment 
    this.activity.setFragment(this); 

    return view; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(imgPreview == null) 
     Log.d("imagePreviewView", "NULL"); 
    else 
     Log.d("UploadButton", "NOT NULL"); 

    // if the result is capturing Image 
    if (requestCode == CameraHelper.getCode()) { 
     if (resultCode == Activity.RESULT_OK) { 

      //ImageView imgPreview = (ImageView)view.findViewById(R.id.ticketImage); 
      previewCapturedImage(imgPreview,"this is the image path");// Preview captured image 

     } else if (resultCode == Activity. RESULT_CANCELED) { 
      // user cancelled Image capture 
      Toast.makeText(view.getContext(), 
        "User cancelled image capture", Toast.LENGTH_SHORT) 
        .show(); 
     } else { 
      // failed to capture image 
      Toast.makeText(view.getContext(), 
        "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 
} 
public void previewCapturedImage(ImageView imgPreview,String imagePath) { 
    try { 

     imgPreview.setVisibility(View.VISIBLE); // If the image view is hidden, show it 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 8; 

     try { 
      Bitmap bitmap = BitmapFactory.decodeFile(mImagePath, 
        options); 
      ExifInterface exif = new ExifInterface(mImagePath); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
      Matrix matrix = new Matrix(); 

      if (orientation == 6) { 
       matrix.postRotate(90); 
      } 
      else if (orientation == 3) { 
       matrix.postRotate(180); 
      } 
      else if (orientation == 8) { 
       matrix.postRotate(270); 
      } 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap 
      imgPreview.setImageBitmap(bitmap); 
     } 
     catch (Exception e) { 
      Log.d("Exception :",e.getMessage()); 
     } 
     //date = null; 

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

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="wrap_content" 
    android:layout_height="400dp"/> 

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/previousBttn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="left" 
     android:text="@string/Previous" /> 

    <Button 
     android:id="@+id/saveButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:text="@string/saveButton" /> 

    <Button 
     android:id="@+id/nextBttn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="right" 
     android:text="@string/Next" /> 
</LinearLayout>  

activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="320dp" 
     android:orientation="vertical" 
     android:id="@+id/uploadImageButton" 
     android:background="@drawable/customborder"> 

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/uploadImageText"/> 

     <ImageView 
      android:id="@+id/ticketImage" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:layout_gravity="center"/> 

    </LinearLayout> 

    <EditText 
     android:id="@+id/note" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:hint="@string/enterNote" /> 

圖像捕獲並保存在相冊中工作完全正常。唯一的問題是圖像顯示在錯誤的頁面中。當我在第一頁中捕獲圖像時,它會在第二頁中顯示預覽,並在下一頁中顯示的另一頁中捕獲它時。

任何幫助將不勝感激。謝謝!

+0

'我使用的是pagerView'。你確定? – greenapps 2014-10-06 13:11:02

+0

哦!對不起,我將其更正爲ViewPager。如果還有什麼不對,請告訴我,因爲我是這個領域的新手。謝謝! – 2014-10-06 14:46:24

回答

0
this.activity.setFragment(this); 

你應該在onClick處理適配器。

由於ViewPager總是實例化下一個頁面MainActivity.fragment不會處理實際的片段。如果您想在onActivityResult中使用片段,那麼在調用intent時使用setFragment。

由於編譯器不會接受onClick中的那條語句,所以這樣做容易。我會考慮如何正確地做到這一點。也許你不需要有這個變量,因爲應該有可能確定活動本身中的實際片段。

UPDATE:你可以檢索活動本身正確的片段:

使用madx的Get the current fragment object

答案這對我的作品。

+0

我一定會給這個鏡頭。 onClick處理程序greenapps是什麼意思?我想要在imageView捕捉到照相機圖像後返回到活動狀態,將圖像設置爲imageView。謝謝! – 2014-10-06 14:50:32

+0

之前的四行onClick成員函數。 – greenapps 2014-10-06 15:03:25

+0

啊太棒了!我會在明天報告進度。謝謝您的幫助! – 2014-10-06 16:42:40

0
  1. 首先ViewPager在可能的情況下實例化下一頁,這就是爲什麼您更新可能會影響下一頁。
  2. 請參考MainActivity上的當前片段。

    private Fragment currentFragment; 
    
  3. 更新currentFragment參考與Button點擊。

    public void onClick(View v) { 
    Intent intent = new Intent(getContext(), OtherAcivity.class); 
    ((MainActivity)getActivity).setCurrentFragment(MyFragment.this); 
          startActivityForResult(intent); 
         } 
    
  4. 使用currentFragmentMainActivity refence時更新分段數據。

    ((MainActivity)getActivity).getCurrentFragment().doSomething(); 
    
相關問題