इस विषय पर हर प्रश्न/उत्तर पढ़ा है, लेकिन मैं उनमें से किसी को me.aggghhअसमर्थ सही ढंग से ActionBar टैब पर टुकड़े स्वैप करने के लिए
के लिए काम करने के लिए नहीं मिल सकता है मैं 3 ActionBar टैब
tab1 कॉल/एक टुकड़ा है कि गतिशील रूप से एक "जोड़ें" बटन
बटन क्लिक होने मैं यह अपने आप में 'बटन टुकड़ा' को हटाने और एक टुकड़ा EditTexts
यह काम करता है के एक समूह है कि के साथ इसे बदलना चाहते प्रदर्शित करता है का उपयोग करता है जैसा सोचा था। हालांकि, जब मैं एक और टैब पर क्लिक करता हूं, उदाहरण के लिए टैब 2 जो अपने स्वयं के टुकड़े को कॉल करता है, टैब 1 से एडिटटेक्सेट खंड टूट जाता है और टैब 2 खंड
मुझे लगता है कि मैं समझ रहा हूं कि मैं जो करने की कोशिश कर रहा हूं वह मूल रूप से गलत है कि एक टुकड़ा एक और टुकड़ा नहीं कह सकता खुद को बदलने के लिए? या किसी भी तरह से मैं पिछले टुकड़ों को सही ढंग से हटा नहीं रहा हूं और बैकस्टैक के साथ कुछ करने के लिए .. ???? या एक्सएमएल दिखा रहा है और छुपा ?????
मैं इस पर बहुत नया हूं और पूरी तरह उलझन में हूं! कृपया मदद ... अग्रिम :)
यहाँ मेरी कोड सबसे TabCompat.zip नमूना
का उपयोग करके android साइट http://developer.android.com/training/backward-compatible-ui/index.html से लिया जाता है में धन्यवाद MainActivity.java
package com.example.android.tabcompat;
public class MainActivity extends TabCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
CompatTab tab1Tab = tabHelper.newTab("tab1")
.setText(R.string.tab_tab1)
.setTabListener(new InstantiatingTabListener(this,"A", Tab1Fragment.class));
tabHelper.addTab(tab1Tab);
CompatTab tab2Tab = tabHelper.newTab("tab2")
.setText(R.string.tab_tab2)
.setTabListener(new InstantiatingTabListener(this, "B", Tab2Fragment.class));
tabHelper.addTab(tab2Tab);
CompatTab tab3Tab = tabHelper.newTab("tab3")
.setText(R.string.tab_tab3)
.setTabListener(new InstantiatingTabListener(this,"C", Tab3Fragment.class));
tabHelper.addTab(tab3Tab);
}
/**
* Implementation of {@link CompatTabListener} to handle tab change events. This implementation
* instantiates the specified fragment class with no arguments when its tab is selected.
*/
public static class InstantiatingTabListener implements CompatTabListener {
private final TabCompatActivity mActivity;
private final Class mClass;
/**
* Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param cls The class representing the fragment to instantiate
*/
public InstantiatingTabListener(TabCompatActivity activity, String tag, Class<? extends Fragment> cls) {
mActivity = activity;
mClass = cls;
}
/* The following are each of the ActionBar.TabListener Callback */
@Override
public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
// Check if the fragment is already initialised
Fragment fragment = tab.getFragment();
if (fragment == null) {
// If not, instantiate and add it to the activity
fragment = Fragment.instantiate(mActivity, mClass.getName());
tab.setFragment(fragment);
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
}
else {
// If it exists, simply attach it in order to show it
ft.attach(fragment);
}
}
public void onTabUnselected(CompatTab tab, FragmentTransaction ft) {
Fragment fragment = tab.getFragment();
if (fragment != null) {
ft.detach(fragment);
}
}
@Override
public void onTabReselected(CompatTab tab, FragmentTransaction ft) {
// User selected the already selected tab. Do nothing.
} }
}
Tab1Fragment .java यह वह जगह है जहां 'एड बटन' लोड किया गया है
package com.example.android.tabcompat;
public class Tab1Fragment extends Fragment{
protected int position;
//display the add tab1 button
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Button btn = new Button (getActivity());
btn.setGravity(Gravity.TOP);
btn.setText(R.string.btn_tab1);
btn.setId(1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//when button is clicked replace the button and display the tab1 form fragment
FragmentManager fragmentManager = getFragmentManager();
Tab1FormFragment formFragment = new Tab1FormFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(android.R.id.tabcontent, formFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return btn;
}
}
Tab1FormFragment.java इस टुकड़ा edittext बार देखा गया है जो यह लेआउट फ़ाइल form.xml से हो जाता है का एक रूप को प्रदर्शित
package com.example.android.tabcompat;
public class Tab1FormFragment extends Fragment {
protected static final String ARG_POSITION = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.form, container, false);
return myFragmentView;
}
}
form.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/petForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_tab_photos" />
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_name"
android:inputType="text" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_age"
android:inputType="text" />
<EditText
android:id="@+id/editTextBreed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/field_breed"
android:inputType="text" />
<Spinner
android:id="@+id/spinnerSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sex_arrays"
android:hint="@string/field_sex" />
<Button
android:id="@+id/savePet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save_record" />
</LinearLayout>`
ठीक है, तो थोड़ा और पढ़ा है, http://stackoverflow.com/questions/12114150/action-bar-tabs-having-two-fragments-one-being- गतिशील-in-one-tab और देखें कि मेरे दोनों टुकड़े गतिशील होने की जरूरत है। इसलिए लेआउटपरैम का उपयोग करके गतिशील रूप से बनाए गए फॉर्म के साथ form.xml को बदल दिया है। मुझे अभी भी वें ओवरलैपिंग समस्या है .... sigh – Wenz
मेरे पास एक सुझाव है .. आप संभवतया एक व्यूफ्लिपर का उपयोग करके उसी बटन में अपने बटन खंड और फॉर्म खंड तर्क को जोड़ सकते हैं।व्यूफ्लिपर का पहला बच्चा आपका बटन होगा, जब कोई उपयोगकर्ता बटन पर दबाएगा तो आप अपने फॉर्म खंड को दिखाने के लिए व्यूफ्लिपर की विधि "setDisplayedChild" का उपयोग कर सकते हैं। इस तरह आप टुकड़े को प्रतिस्थापित करने से बचते हैं और आपको अभी भी वही वांछित प्रभाव मिलता है। –
R.id.tabcontent कहाँ रहते हैं? क्या यह मुख्य गतिविधि के लिए लेआउट पर है? – japino