我有什么 ::附加到活动的单个列表片段
我想要实现的目标 ::我想在旋转屏幕时阻止片段重新加载
到目前为止我做了什么 ::我在stackoverflow上做了一些r / d,发现我必须使用setRetainInstance(true);
以防止片段在方向更改时重新加载屏幕
问题 ::
setRetainInstance(true);
方向更改屏幕重新播放片段mFragSortingHome.java
public class mFragSortingHome extends SherlockListFragment implements View.OnTouchListener {
Button ratingButtonId,priceButtonId,distanceButtonId;
String queryString="SELECT * FROM buffets ORDER BY rating ASC";
ArrayList<HashMap<String, String>> arrayListBuffet=new ArrayList<HashMap<String,String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment1, container, false);
//Setting the NavigationTag from AppControllerApplication class
AppController.CURRENT_FRAGMENT="mFragSortingHome";
setRetainInstance(true);
return view;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
WillYouQuitAppDialog();
}
return false;
}
public void WillYouQuitAppDialog(){
//GPS-Dialog
WillYouQuitAppDialog quitAlert;
try {
quitAlert = new WillYouQuitAppDialog();
quitAlert.show(getActivity().getSupportFragmentManager(), "GpsAlert_Tag");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ratingButtonId=(Button) getActivity().findViewById(R.id.ratingButtonId);
priceButtonId=(Button) getActivity().findViewById(R.id.priceButtonId);
distanceButtonId=(Button) getActivity().findViewById(R.id.distanceButtonId);
ratingButtonId.setOnTouchListener(this);
priceButtonId.setOnTouchListener(this);
distanceButtonId.setOnTouchListener(this);
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragRatingAscendingListTag"), "FragRatingAscendingListTag").addToBackStack(null).commit();
ratingButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
priceButtonId.setBackgroundResource(R.drawable.tab_button_default);
distanceButtonId.setBackgroundResource(R.drawable.tab_button_default);
CharSequence styledItemName1,styledItemName2,styledItemName3;
styledItemName1=setMultiStylingSpan("(ASC)",new ForegroundColorSpan(Color.parseColor("#00aeef")));
styledItemName1=TextUtils.concat(ratingButtonId.getText()+"\n",setMultiStylingSpan(styledItemName1,new RelativeSizeSpan(0.6f)));
ratingButtonId.setText(styledItemName1);
styledItemName2=setMultiStylingSpan("(DESC)",new ForegroundColorSpan(Color.parseColor("#00aeef")));
styledItemName2=TextUtils.concat(priceButtonId.getText()+"\n",setMultiStylingSpan(styledItemName2,new RelativeSizeSpan(0.6f)));
priceButtonId.setText(styledItemName2);
styledItemName3=setMultiStylingSpan("(DESC)",new ForegroundColorSpan(Color.parseColor("#00aeef")));
styledItemName3=TextUtils.concat(distanceButtonId.getText()+"\n",setMultiStylingSpan(styledItemName3,new RelativeSizeSpan(0.6f)));
distanceButtonId.setText(styledItemName3);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
new LoadNextPage().execute();
/*FragmentAdapter adapter=new FragmentAdapter(getActivity(),R.layout.display_buffet_adapater, arrayListBuffet);
setListAdapter(adapter);*/
}
protected class LoadNextPage extends AsyncTask<Integer, Void, Integer>{
@Override
protected Integer doInBackground(Integer... arg0){
//// dA=new DatabaseAdapter(getActivity());
DatabaseHandler mHelper = new DatabaseHandler(getActivity().getApplicationContext());
SQLiteDatabase db = mHelper.getWritableDatabase();
//String selectQuery = "SELECT * FROM buffets ORDER BY rating ASC";
Cursor cursor = db.rawQuery(queryString, null);
HashMap<String, String> mapObj;
///
try {
cursor.moveToFirst();
if(cursor.moveToFirst()){
do{
mapObj = new HashMap<String, String>();
mapObj.put("buf_off_id", cursor.getString(1));
mapObj.put("from_time", cursor.getString(2));
mapObj.put("to_time", cursor.getString(3));
mapObj.put("online_price", cursor.getString(4));
mapObj.put("reserved_price", cursor.getString(5));
mapObj.put("buf_image", cursor.getString(6));
mapObj.put("res_name", cursor.getString(7));
mapObj.put("rating", cursor.getString(8));
mapObj.put("latitude", cursor.getString(9));
mapObj.put("longitude", cursor.getString(10));
mapObj.put("buf_type_name", cursor.getString(11));
mapObj.put("from_date", cursor.getString(12));
mapObj.put("to_date", cursor.getString(13));
mapObj.put("city_id", cursor.getString(14));
mapObj.put("city_name", cursor.getString(15));
mapObj.put("meal_type_id", cursor.getString(16));
mapObj.put("meal_type_name", cursor.getString(17));
mapObj.put("distance", cursor.getString(18));
mapObj.put("buf_desc", cursor.getString(19));
Log.d("<-->MapObject<-->", mapObj.toString());
arrayListBuffet.add(mapObj);//Add to arraylist onject(One row i each iteration)
}while(cursor.moveToNext());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
db.close();
}
//cursor.close();
Log.d("<-->ArrayListObject<-->", arrayListBuffet.toString());
return null;
}
@Override
protected void onPostExecute(Integer offset){
FragmentAdapter adapter=new FragmentAdapter(getActivity(),R.layout.display_buffet_adapater, arrayListBuffet);
setListAdapter(adapter);
}
}
//Detecting onTouch for the tabButtons
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(v.getId()==R.id.ratingButtonId)
{
ratingButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
priceButtonId.setBackgroundResource(R.drawable.tab_button_default);
distanceButtonId.setBackgroundResource(R.drawable.tab_button_default);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
ratingButtonId.setBackgroundResource(R.drawable.tab_button_focus);
//v.setBackgroundResource(R.drawable.buttonpressed);
if(AppController.RATING_LIST_FLAG==false){
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragRatingDescendingListTag"), "FragRatingDescendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY rating DESC";
AppController.RATING_LIST_FLAG=true;
arrayListBuffet.clear();
new LoadNextPage().execute();
}else{
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragRatingAscendingListTag"), "FragRatingAscendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY rating ASC";
AppController.RATING_LIST_FLAG=false;
arrayListBuffet.clear();
new LoadNextPage().execute();
}
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
ratingButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
v.invalidate();
break;
}
}
}else if(v.getId()==R.id.priceButtonId){
ratingButtonId.setBackgroundResource(R.drawable.tab_button_default);
priceButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
distanceButtonId.setBackgroundResource(R.drawable.tab_button_default);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
priceButtonId.setBackgroundResource(R.drawable.tab_button_focus);
//v.setBackgroundResource(R.drawable.buttonpressed);
if(AppController.PRICE_LIST_FLAG==false){
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragPriceAscendingListTag"), "FragPriceAscendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY online_price ASC";
AppController.PRICE_LIST_FLAG=true;
arrayListBuffet.clear();
new LoadNextPage().execute();
}else{
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragPriceDescendingListTag"), "FragPriceDescendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY online_price DESC";
AppController.PRICE_LIST_FLAG=false;
arrayListBuffet.clear();
new LoadNextPage().execute();
}
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
priceButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
//v.setBackgroundResource(R.drawable.buttonnormal);
v.invalidate();
break;
}
}
}else if(v.getId()==R.id.distanceButtonId) {
ratingButtonId.setBackgroundResource(R.drawable.tab_button_default);
priceButtonId.setBackgroundResource(R.drawable.tab_button_default);
distanceButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
distanceButtonId.setBackgroundResource(R.drawable.tab_button_focus);
//v.setBackgroundResource(R.drawable.buttonpressed);
if(AppController.DISTANCE_LIST_FLAG==false){
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragDistanceAscendingListTag"), "FragDistanceAscendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY distance ASC";
AppController.DISTANCE_LIST_FLAG=true;
arrayListBuffet.clear();
new LoadNextPage().execute();
}else{
//getFragmentManager().beginTransaction().replace(R.id.sortBodyContainerId, FragSortParentViewAsList.newInstance("FragDistanceDescendingListTag"), "FragDistanceDescendingListTag").addToBackStack(null).commit();
queryString="SELECT * FROM buffets ORDER BY distance DESC";
AppController.DISTANCE_LIST_FLAG=false;
arrayListBuffet.clear();
new LoadNextPage().execute();
}
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
distanceButtonId.setBackgroundResource(R.drawable.tab_button_pressed);
v.invalidate();
break;
}
}
}
return false;
}
public static CharSequence setMultiStylingSpan(CharSequence text, CharacterStyle... cs){
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for (CharacterStyle c : cs){
ssb.setSpan(c, 0, text.length(), 0);
}
text = ssb;
return text;
}
}
答案 0 :(得分:1)
您唯一需要做的就是..转到您的ANdroid清单文件并将以下代码放在范围内
android:configChanges="orientation|screenSize"