मेरे पास एक बहुत ही सरल कोड है जहां मैं टैब खंडों के साथ एक्शन बार का उपयोग करता हूं। यह लोड के बाद ठीक काम करता है, लेकिन अभिविन्यास परिवर्तन के बाद यह पागल हो जाता है। पुराना टुकड़ा भी दिखाई देता है (क्यों?)।स्क्रॉलव्यू के साथ एंड्रॉइड एक्शन बार टैब ने अभिविन्यास परिवर्तन के बाद डुप्लिकेट दृश्य बनाया
छवि पर हंगेरियन ग्रंथों के लिए खेद है, लेकिन मुझे उम्मीद है कि इससे कोई फर्क नहीं पड़ता।
मैं कोड संलग्न करता हूं, शायद यह इस समस्या को हल करने में मदद करता है।
मुख्य गतिविधि:
public class Main extends Activity
{
private static ActionBar actionBar;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setup action bar for tabs
actionBar = getActionBar();
actionBar.removeAllTabs();
if (actionBar.getTabCount() == 0)
{
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab().setText(R.string.starter).setTabListener(new TabListener<Starter>(this, "starter", Starter.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.newword).setTabListener(new TabListener<NewWord>(this, "newwod", NewWord.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.feedback).setTabListener(new TabListener<Feedback>(this, "feedback", Feedback.class));
actionBar.addTab(tab);
}
if (savedInstanceState != null)
{
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
TabListener (गूगल उदाहरण के रूप में ही):
public class TabListener<T extends Fragment> implements android.app.ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz)
{
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
// User selected the already selected tab. Usually do nothing.
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// Check if the fragment is already initialized
if (mFragment == null)
{
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else
{
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
if (mFragment != null)
{
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
}
टुकड़ा:
public class Starter extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
setRetainInstance(false);
return inflater.inflate(R.layout.newword, container, false);
}
}
और लेआउट एक्सएमएल:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/newword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/newwordtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/wordhint" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/descriptionwordtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/descriptionhint"
android:inputType="textMultiLine"
android:minLines="4" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/origin"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/origintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/originhint" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/source"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/sourcetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/sourcehint" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<EditText
android:id="@+id/nametext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName" />
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>
</ScrollView>
अग्रिम में धन्यवाद!
के लिए हल किया गया है, केवल एक चीज जो मैं जोड़ूंगा वह यह है कि यह लाइन "mFragment = mActivity.getFragmentManager()। FindFragmentByTag (mTag);" MFragment से पहले onTabUnelected विधि में भी जोड़ा जाना चाहिए! = शून्य जांच। अन्यथा, महान काम किया! – SBerg413
mFragment = mActivity.getFragmentManager()। FindFragmentByTag (mTag); मेरी समस्या भी हल हो गई, कक्षा को आंतरिक और निजी होने की आवश्यकता नहीं है। – Goldorak84