2016-11-16 91 views
0

我正在構建應用程序,希望將我的佈局從8點到12點隱藏起來。我嘗試使用線程,但的AsyncTask我不知道如何給時間8之間週期12隱藏特定時間的佈局

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
    <de.hdodenhof.circleimageview.CircleImageView 
      android:layout_width="40dp" 
      android:layout_height="50dp" 
      android:layout_marginLeft="3dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/vegitables" 
      app:civ_border_color="#80000000" 
      app:civ_border_width="1dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="5dp" 
      android:text="Veg :" 
      android:textColor="#F00" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="150dp"/> 

</LinearLayout> 
+0

您需要設置一個函數來隱藏8-12 Am之間的佈局(使用檢查當前時間),並在託管此佈局的活動的'onResume()'中調用它。 –

+0

'if(getNow is between 8am and 12am){layout.hide()} else {layout.show()}' – Selvin

回答

2

試試這個:

Calendar now = Calendar.getInstance(); 
int currentHour = now.get(Calendar.HOUR_OF_DAY); 
if(currentHour >=7 && currentHour <= 11) 
    rootLayoutReference.setVisibility(View.GONE); 
else 
    rootLayoutReference.setVisibility(View.VISIBLE); 
+0

'HOUR_OF_DAY'不是從零開始的。你可以只使用8和12. –

+0

謝謝@rhari它適用於我... – Manju

+0

接受它,如果它的工作:) – rhari

0

首先上午12點,意味着午夜,而下午12點意味着中午。我假定你的意思是中午,但這種解決方案可以很容易地改變到任何時間界限。當活動變得對用戶(在onResume)可見

public class MyActivity extends AppCompatActivity { 
    private Handler handler; 
    private View layout; 
    private Runnable updateVisibilityCallback; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.my_activity); 

     handler = new Handler(Looper.getMainLooper()); 
     layout = findViewById(R.id.layout); 
     updateVisibilityCallback = new Runnable() { 
      @Override 
      public void run() { 
       updateTextVisibility(); 
      } 
     }; 
    } 

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

     updateTextVisibility(); 
    } 

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

     handler.removeCallbacks(updateVisibilityCallback); 
    } 

    private void updateTextVisibility() { 
     Calendar calendar = Calendar.getInstance(); 
     int hour = calendar.get(Calendar.HOUR_OF_DAY); 

     if (8 <= hour && hour <= 11) { 
      layout.setVisibility(View.GONE); 
     } else { 
      layout.setVisibility(View.VISIBLE); 
     } 

     handler.postDelayed(updateVisibilityCallback, getVisibilityUpdateDelay()); 
    } 

    private long getVisibilityUpdateDelay() { 
     Calendar rightNow = Calendar.getInstance(); 
     int hour = rightNow.get(Calendar.HOUR_OF_DAY); 

     Calendar nextUpdate = Calendar.getInstance(); 
     nextUpdate.set(Calendar.MINUTE, 0); 
     nextUpdate.set(Calendar.SECOND, 0); 
     nextUpdate.set(Calendar.MILLISECOND, 0); 

     if (hour < 8) { 
      nextUpdate.set(Calendar.HOUR_OF_DAY, 8); 
     } else if (hour < 12) { 
      nextUpdate.set(Calendar.HOUR_OF_DAY, 12); 
     } else { 
      nextUpdate.add(Calendar.DATE, 1); 
      nextUpdate.set(Calendar.HOUR_OF_DAY, 8); 
     } 

     return nextUpdate.getTimeInMillis() - rightNow.getTimeInMillis(); 
    } 
} 

每次檢查的時間和更新佈局的可見性:

您可以添加以下到您的活動。此外,你應該跟蹤何時會發生下一次更新 - 計算延遲時間爲8:00或12:00,並使用Handler發佈Runnable。只要您的用戶停止關注活動,就會清除回調。