我有这个:
MainActivity.java:
package activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.example.pierre.tan.R;
import adapter.FragmentDrawer;
import hotchemi.android.rate.AppRate;
import hotchemi.android.rate.OnClickButtonListener;
import model.Arrets;
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
public static Arrets arrets = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppRate.with(this)
.setInstallDays(5) // default 10, 0 means install day.
.setLaunchTimes(10) // default 10
.setRemindInterval(1) // default 1
.setShowNeutralButton(true) // default true
.setDebug(false) // default false
.setOnClickButtonListener(new OnClickButtonListener() { // callback listener.
@Override
public void onClickButton(int which) {
Log.d(MainActivity.class.getName(), Integer.toString(which));
}
})
.monitor();
// Show a dialog if meets conditions
AppRate.showRateDialogIfMeetsConditions(this);
arrets = new Arrets();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
View searchContainer = findViewById(R.id.search_container);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
drawerFragment.setFocusableInTouchMode(false);
// display the first navigation drawer view on app launch
displayView(0);
}
@Override
public void onBackPressed() {
super.onBackPressed();
new AlertDialog.Builder(this)
.setMessage("Voulez-vous quitter l\'application ?")
.setPositiveButton("oui", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}).setNegativeButton("non", null).show();
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new ArretsFragment();
title = "Arrets";
break;
case 2:
fragment = new AboutFragment();
title = "A propos";
break;
case 3:
fragment = new SettingsFragment();
title = "Paramètres";
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}
我有HomeFragment.java:
package activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.pierre.tan.R;
import java.util.List;
import adapter.CustomListAdapter;
import model.Arrets;
import util.Spfav;
public class HomeFragment extends Fragment {
ListView favoriteList;
Spfav sharedPreference;
List<Arrets> favorites;
Activity activity;
CustomListAdapter productListAdapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
View searchContainer = getActivity().findViewById(R.id.search_container);
final EditText toolbarSearchView = (EditText) getActivity().findViewById(R.id.search);
ImageView searchClearButton = (ImageView) getActivity().findViewById(R.id.search_clear);
searchClearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toolbarSearchView.setText("");
}
});
searchContainer.setVisibility(View.GONE);
sharedPreference = new Spfav();
favorites = sharedPreference.getFavorites(activity);
if (favorites == null) {
Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
} else {
if (favorites.size() == 0) {
Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
}
favoriteList = (ListView) view.findViewById(R.id.list);
if (favorites != null) {
productListAdapter = new CustomListAdapter(activity, favorites);
favoriteList.setAdapter(productListAdapter);
}
favoriteList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.lieu);
String text = textView.getText().toString();
TextView textView2 = (TextView) view.findViewById(R.id.arret);
String libelle = textView2.getText().toString();
Intent i = new Intent(getActivity(), TempsActivity.class);
i.putExtra("text", text);
i.putExtra("libelle", libelle);
startActivity(i);
}
});
favoriteList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id) {
ImageView button = (ImageView) view
.findViewById(R.id.imgbtn_favorite);
String tag = button.getTag().toString();
sharedPreference.removeFavorite(activity,
favorites.get(position));
button.setTag("grey");
button.setImageResource(R.drawable.ic_heart_white);
favorites.remove(favorites.get(position));
Toast.makeText(
activity,
activity.getResources().getString(
R.string.remove_favr),
Toast.LENGTH_SHORT).show();
sharedPreference.saveFavorites(activity, favorites);
productListAdapter.notifyDataSetChanged();
return true;
}
});
// Inflate the layout for this fragment
}
return view;
}
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public void onDetach() {
super.onDetach();
}
}
而且我不知道如何做到这一点:当我在片段中(只是Homefragment)设置一个ne函数到后面按下并且不执行活动的功能(谁为其他片段工作。)
抱歉我的英语不好,如果有人可以帮助我,那就太好了!
答案 0 :(得分:0)
在您的活动中尝试此操作:
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onkeydown");
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//your code here
return true;
}
return super.onKeyDown(keyCode,event);
}
片段有自己的onBackPressed()
处理程序,而不是活动处理程序。这样做是为了通过例如按下来返回先前显示的片段。