0

Pageviewer.java代碼:如何從活動更新片段/更新片段ui?

//hide status bar/action bar on single tap 
    package com.app.imageswiper; 

    import android.app.ActionBar; 
    import android.app.Dialog; 
    import android.app.Fragment; 
    import android.app.FragmentManager; 
    import android.app.FragmentTransaction; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.net.Uri; 
    import android.os.Handler; 
    import android.preference.PreferenceManager; 
    import android.support.design.widget.TabLayout; 
    import android.support.v4.view.GestureDetectorCompat; 
    import android.support.v4.view.MotionEventCompat; 
    import android.support.v4.view.ViewPager; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.GestureDetector; 
    import android.view.Gravity; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.MotionEvent; 
    import android.view.View; 
    import android.view.Window; 
    import android.view.WindowManager; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.LinearLayout; 
    import android.widget.RelativeLayout; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class Pageviwer extends AppCompatActivity { 

     private Integer[] mImageIds = {R.drawable.page001, R.drawable.page002, 
       R.drawable.page003}; 
     private static final String DEBUG_TAG = "pageviwer "; 
     ImageView imageView; 
     float startXValue = 1; 
     public int num; 
     public int click; 
     protected static final String TAG = "Pageviwer"; 
     private GestureDetector mGestureDetector; 
     public SharedPreferences prefs; 
     public static int Bookmark = 0; 
     public static int Pageno = 0; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      prefs = PreferenceManager.getDefaultSharedPreferences(this); 
      int Bookmark1 = prefs.getInt("Bookmark", 0); 
      Bookmark = Bookmark1; 

      prefs = PreferenceManager.getDefaultSharedPreferences(this); 
      int Pageno1 = prefs.getInt("Pageno", 0); 
      Pageno = Pageno1; 


      setContentView(R.layout.pageviwer); 
      imageView = (ImageView) findViewById(R.id.image_place_holder); 
      imageView.setImageResource(mImageIds[0]); 
      TextView tv1 = (TextView) findViewById(R.id.pageno); 
      tv1.setText("" + (num + 1)); 
      Toast.makeText(getApplicationContext(), "value is " + num, Toast.LENGTH_LONG).show(); 
      setupGestureDetector(); 
      hideActionBar(); // hide action bar after 1 second 
     } 


     private void setupGestureDetector() { 


      mGestureDetector = new GestureDetector(this, 

        new GestureDetector.SimpleOnGestureListener() { 

         //Detecting Swipe/Fling Direction 
         @Override 
         public boolean onFling(MotionEvent e1, MotionEvent e2, 
               float velocityX, float velocityY) { 
          Log.i(TAG, "onFling"); 
          if (e1.getX() < e2.getX()) { 
           Log.d(TAG, "Left to Right swipe performed"); 
           if (num == 2) { 
            num = num; 
           } else { 
            imageView.setImageResource(mImageIds[num + 1]); 
            num = num + 1; 
           } 
           TextView tv1 = (TextView) findViewById(R.id.pageno); 
           tv1.setText("" + (num + 1)); 
           Toast.makeText(getApplicationContext(), "value is " + num, Toast.LENGTH_LONG).show(); 
          } 

          if (e1.getX() > e2.getX()) { 
           Log.d(TAG, "Right to Left swipe performed"); 
           if (num == 0) { 
            num = num; 
           } else { 
            imageView.setImageResource(mImageIds[num - 1]); 
            num = num - 1; 
           } 
           TextView tv1 = (TextView) findViewById(R.id.pageno); 
           tv1.setText("" + (num + 1)); 

           Toast.makeText(getApplicationContext(), "value is " + num, Toast.LENGTH_LONG).show(); 
          } 
          return true; 
         } 

         //detecting single tap 
         @Override 
         public boolean onSingleTapUp(MotionEvent e) { 
          Log.i(TAG, "onSingleTapUp"); 
          Toast.makeText(getApplicationContext(), "Single Tap ", Toast.LENGTH_LONG).show(); 
          //Remove notification bar and action bar 
          if (click == 0) { 
           getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
           getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 
           if (getSupportActionBar() != null) getSupportActionBar().hide(); 
           click = 1; 
          } 
          //Return notification bar and action bar 
          else { 
           getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
           getWindow().getDecorView().setSystemUiVisibility(
             View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
               | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
           if (getSupportActionBar() != null) getSupportActionBar().show(); 
           click = 0; 
          } 
          return true; 
         } 
        }); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { 

      if (mGestureDetector != null) { 
       return mGestureDetector.onTouchEvent(event); 
      } else { 
       return super.onTouchEvent(event); 
      } 

     } 

     public void hideActionBar() { 
      Handler h = new Handler(); 
      h.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        // DO DELAYED STUFF 
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
        if (getSupportActionBar() != null) getSupportActionBar().hide(); 
        click = 1; 
       } 
      }, 1000); // 1000 milliseconds (1 second) 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 

      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 


      if (id == com.app.imageswiper.R.id.action_settings) { 
       Bookmark=1; 
       prefs.edit().putInt("Bookmark", Bookmark).commit(); 
       prefs.edit().putInt("Pageno", num).commit(); 
       Toast.makeText(getApplicationContext(), "Bookmark value is "+Bookmark, Toast.LENGTH_LONG).show(); 



      } 


      return super.onOptionsItemSelected(item); 


     } 
    } 

ViewPagerAdapter代碼:

package com.app.imageswiper; 

/** 
* Created by Jaffer on 14-Apr-16. 
*/ 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class Bookmarks extends Fragment { 
    public SharedPreferences prefs; 
    public static int Bookmark = 0; 
    public static int Pageno = 0; 
    Button sendButton = null; 
    View inflatedView = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     int Bookmark1 = prefs.getInt("Bookmark", 0); 
     Bookmark = Bookmark1; 

     prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     int Pageno1 = prefs.getInt("Pageno", 0); 
     Pageno = Pageno1; 

     this.inflatedView = inflater.inflate(R.layout.bookmarks, container, false); 

     sendButton = (Button) inflatedView.findViewById(R.id.bookmark); 
     if(Bookmark==1){sendButton.setText("Page "+Pageno);} 
     else {sendButton.setText("No Bookmark ");} 

     if(sendButton == null) 
     { 
      Log.d("debugCheck", "HeadFrag: sendButton is null"); 
      return inflatedView; 
     } 
     sendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getActivity(), Pageviwer.class); 
       startActivity(intent); 
      } 
     }); 
     return inflatedView; 
    } 
} 

bookmarks.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" 
    android:id="@+id/bookmarks"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAllCaps="false" 
     style="@style/Widget.AppCompat.Button.Borderless" 
     android:id="@+id/bookmark" 
     android:layout_alignParentBottom="true" /> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@android:color/darker_gray"/> 

</LinearLayout> 

我不知道如何更新從Pageviewer按鈕上的文字。 java的R.id.bookmark按鈕,在菜單項R.id.action_settings的點擊上顯示在書籤片段上。我想這樣做,以便按鈕文本對應於正在書籤的頁面。我嘗試了很多東西,但無濟於事。有人可以幫助我嗎?

在此先感謝

回答

0

在您的片段中創建一個方法來更新您的按鈕的文本。

public void updateText(String newText){ 
    //assuming button is globally declared in your fragment. 
    button.setText(newText); 
} 

相反姿態探測器的你可以使用一個ViewPager它支持滑動到左側和right.You可以找到一個示例實現here

然後在您的活動中,在ViewPager上執行PageChanegListener

viewPager.addOnPageChangeListener(new OnPageChangeListener() { 
    public void onPageScrollStateChanged(int state) {} 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} 

    public void onPageSelected(int position) { 
     // Check if this is the page you want.You have to implement viewpager's adapter with getItem(index) which returns a fragment at that index. 
     BookMarkFragment fragment (BookMarkFragment)mAdapter.getItem(position); 
     fragment.updateText(newText); 
    }; 
}); 
0

你應該保存某個按鈕的狀態。你的字段int Bookmark == 0,所以表達式sendButton.setText("Page "+Pageno);永遠不會執行。將狀態保存在SharedPreferences中並獲取此狀態。像這樣:

prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
int isBookmark = prefs.getInt("Bookmark", 0); 
int mPageno = prefs.getInt("Pageno", 0); 
... 
if(isBookmark == 1){ 
    sendButton.setText("Page " + mPageno); 
} else { 
    sendButton.setText("No Bookmark"); 
}