Android 如何将progressbar添加到ActionBarSherlock
是否可以将progressbar添加到ActionBarSherlock? 我需要在特定时间显示和隐藏它。但它必须位于ActionBarSherlock内部 我的班级代码。您可以看到,我使用了requestWindowFeature(Window.FEATURE\u PROGRESS),但这没有任何区别。同样的结果。 :Android 如何将progressbar添加到ActionBarSherlock,android,actionbarsherlock,Android,Actionbarsherlock,是否可以将progressbar添加到ActionBarSherlock? 我需要在特定时间显示和隐藏它。但它必须位于ActionBarSherlock内部 我的班级代码。您可以看到,我使用了requestWindowFeature(Window.FEATURE\u PROGRESS),但这没有任何区别。同样的结果。 : 公共类RecipeActivity扩展了SherlockActivity{ 私有字符串图片; 私有字符串TAG=“RecipeActivity”; 私人影像查看图片; 私人互惠
公共类RecipeActivity扩展了SherlockActivity{
私有字符串图片;
私有字符串TAG=“RecipeActivity”;
私人影像查看图片;
私人互惠一般互惠;
私家菜谱;
私有字符串[]图片;
私有数组列表步骤;
//私人影像查看视星;
私有数据库;
私有应用程序;
私有文本视图视图描述;
私有文本视图用户视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口功能\进度);
SetProgressBarInDeterminateVibility(真);
试一试{
setContentView(R.layout.recipe_页面);
getSupportActionBar().setDisplayShowTitleEnabled(false);
recipeGeneral=(recipeGeneral)getIntent().getSerializableExtra(“配方”);
Log.d(标记,“recipeGeneral=“+recipeGeneral.toString());
picture=Constants.URL+recipeGeneral.getImg_URL();
日志d(标签,“图片:+图片);
viewPicture=(ImageView)findViewById(R.id.picture);
新建下载ImageTask()。执行(图片);
viewsdescription=(TextView)findViewById(R.id.recepy\u description);
TextView视图标题=(TextView)findViewById(R.id.recepy\u标题);
TextView ratingView=(TextView)findViewById(R.id.txtTitle3);
userView=(TextView)findViewById(R.id.author\u name);
application=(NyamApplication)getApplication();
db=application.getDB();
if(getIntent().getBooleanExtra(“isFavorites”,false)=true){
Log.d(标记“isFavorites=“+getIntent().getBooleanExtra”(“isFavorites”,false));
viewDescription.setText(((配方)recipeGeneral.getDescription());
viewTitle.setText(recipeGeneral.getTitle());
Log.d(标记,“Title=“+recipeGeneral.getTitle());
ratingView.setText(String.valueOf(recipeGeneral.getFavorites_by());
Log.d(标记,“Rating=“+String.valueOf(recipeGeneral.getFavorites_by()));
userView.setText(((配方)recipeGeneral.getUser());
Log.d(标记,“User=”+((Recipe)recipeGeneral.getUser());
steps=db.getStepsByRecipeId(recipeGeneral.getId());
如果(步骤!=null){
((配方)recipeGeneral).设置步骤(步骤);
}
}否则{
Log.d(标记“isFavorites=“+getIntent().getBooleanExtra”(“isFavorites”,false));
Object[]params=new Object[]{this,Constants.URL+recipeGeneral.getPath()+Constants.JSON};
新建AsyncHttpGetRecipe().execute(参数);
新建AsyncHttpGetSteps().execute(参数);
viewTitle.setText(recipeGeneral.getTitle());
ratingView.setText(recipeGeneral.getFavorites_by());
Log.d(标记,“Rating=“+recipeGeneral.getFavorites_by());
}
}捕获(例外e){
e、 printStackTrace();
}
}
私有类DownloadImageTask扩展了AsyncTask{
位图=空;
@凌驾
受保护位图doInBackground(字符串…str){
试试{
InputStream in=new java.net.URL(picture.openStream();
位图=BitmapFactory.decodeStream(新的SANIInputStream(in));
//viewPicture.setImageBitmap(位图);
}
捕获(例外e){
e、 printStackTrace();
}
返回位图;
}
@凌驾
受保护的void onPostExecute(位图结果){
super.onPostExecute(结果);
viewPicture.setBackgroundDrawable(新位图绘制(位图));
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
com.actionbarsherlock.view.MenuInflater充气器=getSupportMenuInflater();
充气器。充气(R.menu.item\u page\u菜单,(com.actionbarsherlock.view.menu)菜单);
返回super.onCreateOptions菜单(菜单);
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.icon_little3:
日志d(标记“进一步”);
if(getIntent().getBooleanExtra(“isFavorites”,false)==false){
如果(配方!=null&&steps!=null){
配方。设置步骤(步骤);
db.addRecipeToFavorites(配方);
}
}
}
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.菜单\u进一步:
Log.d(标签“在菜单中”);
Intent stepIntent=新的Intent(此,StepActivity1.class);
if(getIntent().getBooleanExtra(“isFavorites”,false)=true){
Log.d(标记“id:+recipeGeneral.getId());
Log.d(标记“desc:+recipeGeneral.getTitle());
stepIntent.putExtra(“Recipe1”,recipeGeneral);
}否则{
Log.d(标记“id:+recipe.getId());
Log.d(标记“desc:+recipe.getTitle());
配方。设置步骤(步骤);
stepIntent.putExtra(“Recipe1”,配方);
}
startActivity(stepIntent);
返回true;
案例R.id.菜单\购物车:
Toast.makeText(这个“Cart”,10000).show();
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
类AsyncHttpGetRecipe扩展了AsyncTask{
@凌驾
受保护配方doInBackground(对象…参数){
RecipeRecipeAsync=null;
试一试{
recipeAsync=apifacture.getRecipe((上下文)参数[0],(字符串)参数[1]);
}捕获(JSONException e){
e、 printStackTrace();
}
配方=recipeAsync;
返回往复式同步;
}
@凌驾
PostExecute上受保护的void(Recipe recipeTemp){
viewDescription.setText(recipeTemp.getDescription());
public class RecipeActivity extends SherlockActivity {
private String picture;
private String TAG = "RecipeActivity";
private ImageView viewPicture;
private RecipeGeneral recipeGeneral;
private Recipe recipe;
private String [] pictures;
private ArrayList<Step> steps;
//private ImageView viewStar;
private DataBaseFactory db;
private NyamApplication application;
private TextView viewDescription;
private TextView userView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
try {
setContentView(R.layout.recipe_page);
getSupportActionBar().setDisplayShowTitleEnabled(false);
recipeGeneral = (RecipeGeneral)getIntent().getSerializableExtra("Recipe");
Log.d(TAG, "recipeGeneral = " + recipeGeneral.toString());
picture = Constants.URL + recipeGeneral.getImg_url();
Log.d(TAG,"picture : " + picture);
viewPicture = (ImageView)findViewById(R.id.picture);
new DownloadImageTask().execute(pictures);
viewDescription = (TextView)findViewById(R.id.recepy_description);
TextView viewTitle = (TextView)findViewById(R.id.recepy_title);
TextView ratingView = (TextView)findViewById(R.id.txtTitle3);
userView = (TextView)findViewById(R.id.author_name);
application = (NyamApplication)getApplication();
db = application.getDB();
if (getIntent().getBooleanExtra("isFavorites", false) == true) {
Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));
viewDescription.setText(((Recipe)recipeGeneral).getDescription());
viewTitle.setText(recipeGeneral.getTitle());
Log.d(TAG, "Title = " + recipeGeneral.getTitle());
ratingView.setText(String.valueOf(recipeGeneral.getFavorites_by()));
Log.d(TAG, "Rating = " + String.valueOf(recipeGeneral.getFavorites_by()));
userView.setText(((Recipe)recipeGeneral).getUser());
Log.d(TAG, "User = " + ((Recipe)recipeGeneral).getUser());
steps = db.getStepsByRecipeId(recipeGeneral.getId());
if (steps != null) {
((Recipe)recipeGeneral).setSteps(steps);
}
} else {
Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));
Object [] params = new Object[] {this, Constants.URL + recipeGeneral.getPath()+ Constants.JSON};
new AsyncHttpGetRecipe().execute(params);
new AsyncHttpGetSteps().execute(params);
viewTitle.setText(recipeGeneral.getTitle());
ratingView.setText(recipeGeneral.getFavorites_by());
Log.d(TAG, "Rating = " + recipeGeneral.getFavorites_by());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class DownloadImageTask extends AsyncTask<String,Void,Bitmap> {
Bitmap bitmap = null;
@Override
protected Bitmap doInBackground(String... str) {
try{
InputStream in = new java.net.URL(picture).openStream();
bitmap = BitmapFactory.decodeStream(new SanInputStream(in));
//viewPicture.setImageBitmap(bitmap);
}
catch(Exception e){
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
viewPicture.setBackgroundDrawable(new BitmapDrawable(bitmap));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.item_page_menu, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.icon_little3:
Log.d(TAG,"further");
if (getIntent().getBooleanExtra("isFavorites", false) == false) {
if (recipe != null && steps != null) {
recipe.setSteps(steps);
db.addRecipeToFavorites(recipe);
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_further:
Log.d(TAG,"In menu_further");
Intent stepIntent = new Intent(this, StepActivity1.class);
if (getIntent().getBooleanExtra("isFavorites", false) == true) {
Log.d(TAG,"id: " + recipeGeneral.getId());
Log.d(TAG,"desc: " + recipeGeneral.getTitle());
stepIntent.putExtra("Recipe1", recipeGeneral);
} else {
Log.d(TAG,"id: " + recipe.getId());
Log.d(TAG,"desc: " + recipe.getTitle());
recipe.setSteps(steps);
stepIntent.putExtra("Recipe1", recipe);
}
startActivity(stepIntent);
return true;
case R.id.menu_cart:
Toast.makeText(this, "Cart", 10000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class AsyncHttpGetRecipe extends AsyncTask<Object, String, Recipe> {
@Override
protected Recipe doInBackground(Object... params) {
Recipe recipeAsync = null;
try {
recipeAsync = ApiFactory.getRecipe((Context)params[0], (String)params[1]);
} catch (JSONException e) {
e.printStackTrace();
}
recipe = recipeAsync;
return recipeAsync;
}
@Override
protected void onPostExecute(Recipe recipeTemp) {
viewDescription.setText(recipeTemp.getDescription());
Log.d(TAG, "Description = " + recipeTemp.getDescription());
userView.setText(recipeTemp.getUser());
Log.d(TAG, "User = " + recipeTemp.getUser());
}
}
class AsyncHttpGetSteps extends AsyncTask<Object, String, ArrayList<Step>> {
@Override
protected ArrayList<Step> doInBackground(Object... params) {
ArrayList<Step> stepsAsync = null;
try {
stepsAsync = ApiFactory.getSteps((Context)params[0], (String)params[1]);
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return stepsAsync;
}
@Override
protected void onPostExecute(ArrayList<Step> stepsAsync) {
steps = stepsAsync;
}
}
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setProgressBarIndeterminateVisibility(false);
public class Progress extends SherlockActivity {
Handler mHandler = new Handler();
Runnable mProgressRunner = new Runnable() {
@Override
public void run() {
mProgress += 2;
//Normalize our progress along the progress bar's scale
int progress = (Window.PROGRESS_END - Window.PROGRESS_START) / 100 * mProgress;
setSupportProgress(progress);
if (mProgress < 100) {
mHandler.postDelayed(mProgressRunner, 50);
}
}
};
private int mProgress = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
//This has to be called before setContentView and you must use the
//class in com.actionbarsherlock.view and NOT android.view
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progress);
findViewById(R.id.go).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mProgress == 100) {
mProgress = 0;
mProgressRunner.run();
}
}
});
}
}
setSupportProgressBarIndeterminateVisibility(true);
public class MyActivity extends SherlockFragmentActivity {
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.my_layout);
//...
setSupportProgressBarIndeterminateVisibility(true);
}
}