فراخوانی ActionBar در fragment
با سلم من طبق آموزش آقای کاشی زاده actionbar پیاده سازی کردم.مشکلی که دارم بار اول میتونم از button یا image های داخل Actionbar استفاده کنم ولی وقتی بین فرگمنت ها پیمایش میکنم دیگه دکمه ها و ایمیج های Actionbar کار نمیکنه ؟ باید چی کار کنم که تو همه فرگمنت ها بتونم از ایمیج یا دکمه ها استفاده کنم . با تشکر.


دقیق حضور ندارم که آقای کاشی زاده از چه روشی استفاده کردن، ولی برای این کار میتونید اکشن بار رو توی اکتیویتی تعریف کنید و برای ویو ها clickListener بنویسید. این پاسخ با درنظر گرفتن اینکه شما یک اکتیویتی داری و layout های دیگه رو توی فرگمنت نمایش میدی.
اگر قراره اکشن بار در حالت های مختلف نمایش متفاوتی داشته باشه، باید از فرگمنت مورد نظر به اکتیویتی دستور بدید که تغییرات رو اعمال کنه.

مرسی آقای حاج حسینی.من یه اکتیویتی دارم و چند تا فرگمنت از داخل اکتیویتی اکشن بار و اسلاید منو رو فراخونی میکنم برای بار اول برنامه اجرا میشه دکمه ها کار میکنه ولی وقتی بین فرگمنت ها پیمایش میکنم از کار میفته وقتی هم داخل فرگمنت ها لیسنر مینویسم اررور nullpointerexeption میگیره .کد ها رو قرار میدم.
ActivityMain :
public class ActivityMain extends ActionBarActivity {
private static long back_pressed = 0L;
private Typeface typeface;
private Typeface typeface2;
private Typeface typeface3;
private Typeface typeface4;
private TextView txt_ms_void;
private TextView txt_english_msvoid;
private TextView txt_home;
private TextView txt_fav;
private TextView txt_bazar;
private TextView txt_details;
private TextView txt_exit;
private ImageView img_cafebazar_slide_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
/****************************************/
final View actionBarView = G.inflater.inflate(R.layout.action_bar, null);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
ImageView btnFav = (ImageView) findViewById(R.id.btnFav);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivitySearch.class);
G.currentActivity.startActivity(intent);
}
});
btnFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(G.currentActivity, ActivityFavSerials.class);
G.currentActivity.startActivity(intent);
}
});
/***********************************************************/
final DrawerLayout drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
txt_ms_void = (TextView) drawerlayout.findViewById(R.id.txt_ms_void);
txt_english_msvoid = (TextView) drawerlayout.findViewById(R.id.txt_english_msvoid);
txt_home = (TextView) drawerlayout.findViewById(R.id.txt_home);
txt_fav = (TextView) drawerlayout.findViewById(R.id.txt_fav);
txt_bazar = (TextView) drawerlayout.findViewById(R.id.txt_bazar);
txt_details = (TextView) drawerlayout.findViewById(R.id.txt_details);
txt_exit = (TextView) drawerlayout.findViewById(R.id.txt_exit);
typeface = typeface.createFromAsset(getAssets(), "arshia.ttf");
typeface2 = typeface.createFromAsset(getAssets(), "A.Arghavan.ttf");
typeface3 = typeface.createFromAsset(getAssets(), "SultanNormal_ufont.ir.ttf");
typeface4 = typeface.createFromAsset(getAssets(), "HandPrintingPress_Meshed_demo.ttf");
txt_ms_void.setTypeface(typeface3);
txt_english_msvoid.setTypeface(typeface4);
txt_home.setTypeface(typeface);
txt_fav.setTypeface(typeface);
txt_bazar.setTypeface(typeface2);
txt_details.setTypeface(typeface);
txt_exit.setTypeface(typeface);
txt_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_menue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_menue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, ActivityFavSerials.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
}
});
img_cafebazar_slide_menu = (ImageView) drawerlayout.findViewById(R.id.img_cafebazar_slide_menu);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.cafebazar_in_slidemenu);
img_cafebazar_slide_menu.setAnimation(animation);
txt_details.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityMain.this, Activity_details.class);
ActivityMain.this.startActivity(intent);
//ActivitySerials.this.finish();
ActivityMain.this.overridePendingTransition(R.anim.slide_down, android.R.anim.fade_out);
}
});
final Button btnMenu1 = (Button) actionBarView.findViewById(R.id.btnMenu1);
btnMenu1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
drawerlayout.closeDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_menue);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);
btnMenu1.setAnimation(animation);
} else {
drawerlayout.openDrawer(Gravity.RIGHT);
btnMenu1.setBackgroundResource(R.drawable.slide_menue_horozintal);
Animation animation = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate_back);
btnMenu1.setAnimation(animation);
}
}
});
txt_exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
/*************************************************************/
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new PagerAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
این هم یکی از فرگمنت ها :
public class FragmentHorror extends Fragment {
private ViewGroup layout;
private static ListView list;
public static AdapterSerials adapter;
private static TextView txtTest;
public static ArrayList<StructApps> apps= new ArrayList<StructApps>();
public ProgressDialog mProgressDialog;
public String lastFilter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layout = (ViewGroup) inflater.inflate(R.layout.fragment_horror, null);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
list = (ListView) layout.findViewById(R.id.list);
lastFilter = "Horror";
boolean online = isOnline();
if (online) {
Log.i("LOG", "B");
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("لطفا صبر کنید...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
mProgressDialog.setCancelable(false);
requestData("http://" + G.IP + "/app-server2/service.php?action=readfilter");
} else {
Toast.makeText(G.context, "No Internet", Toast.LENGTH_SHORT).show();
}
adapter = new AdapterSerials(apps);
list.setAdapter(adapter);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public void requestData(String uri) {
StringRequest request = new StringRequest(Method.POST, uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
apps.clear();
JSONArray series = new JSONArray(response);
for (int i = 0; i < app.length(); i++) {
JSONObject object = app.getJSONObject(i);
StructApps appss= new StructApps ();
appss.id = object.getLong("app_id");
appss.name = object.getString("app_name");
appss.ratingValue = (float) object.getDouble("app_rating");
appss.logoUrl = object.getString("app_logourl");
appss.year = object.getInt("app_year");
appss.votes = object.getString("app_vote");
apps.add(app);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
mProgressDialog.dismiss();
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError ex) {
Toast.makeText(getActivity(), "خطا سرور", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}) {
Map<String, String> params = new HashMap<String, String>();
@Override
protected Map<String, String> getParams() {
params.put("filter", "" + lastFilter);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
params.put("filter", "" + lastFilter);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
queue.add(request);
}
}

اینم لاگ وقتی من این کد رو به فرگمنم اضافه میکنم .
ImageView btnSearch =(ImageView) findViewById(R.id.btnSearch);
ImageView btnFav =(ImageView) findViewById(R.id.btnFav);
btnSearch.setOnClickListener(newOnClickListener(){
@Override
publicvoid onClick(View v){
Intent intent =newIntent(G.currentActivity,ActivitySearch.class);
G.currentActivity.startActivity(intent);
}
});
btnFav.setOnClickListener(newOnClickListener(){
@Override
publicvoid onClick(View v){
Intent intent =newIntent(G.currentActivity,ActivityFavSerials.class);
G.currentActivity.startActivity(intent);
}
});

دوست عزیز مشکل شما اینجاست که چند اکتیویتی داری. به جای اینکه چندین اکتیویتی بسازی، باید کلا یک اکتیویتی توی برنامت داشته باشی که توش یک اکشن بار و یک FrameLayout باشه.
اکشن بار که وظیفش مشخصه و FrameLayout نقش یک container رو برای شما بازی میکنه، بدین منظور که تمام فرگمنت ها (که در حال حاضر شما از اکتیویتی استفاده کردی) رو توی خودش نمایش میده.
در این حالت، شما همیشه توی اکتیویتی main هستی و توی اون از فرگمنت ها استفاده میکنی.
پس تمام اکتیویتی های دیگت به جز اکتیویتی main رو به فرگمنت تغییر بده، توی اکتیویتی باید فقط 2 متد داشته باشی (فعلا)
1: متد cast کردن ویو ها و onClickListener نوشتن برای ویو های اکشن بار.
2: متد رفتن به MainFragment . یعنی توی اون container باید MainFragment رو لود کنی.
پس از اینکه این کارهارو کردی، به احتمال زیاد با دکمه back به مشکل برمیخوری که میتونی برای اونم تاپیک بزنی و لینکش رو برام بفرستی تا پاسخ بدم.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .