के लिए विकसित करते समय Android, आप अपना लक्ष्य (या न्यूनतम) sdk को 4 (API 1.6) पर सेट कर सकते हैं और समर्थन जोड़ने के लिए Android संगतता पैकेज (v4) जोड़ सकते हैं Fragments। कल मैंने ऐसा किया और Fragmentsएक कस्टम वर्ग के डेटा की कल्पना करने के लिए सफलतापूर्वक लागू किया।
मेरा सवाल यह है: Fragmentsएक कस्टम ऑब्जेक्ट से केवल एक दृश्य प्राप्त करने के लिए विरोध करने के लिए उपयोग करने के लिए क्या लाभ है , और अभी भी एपीआई 1.5 का समर्थन कर रहा है?
उदाहरण के लिए, कहो कि मेरे पास वर्ग Foo.java है:
public class Foo extends Fragment {
/** Title of the Foo object*/
private String title;
/** A description of Foo */
private String message;
/** Create a new Foo
* @param title
* @param message */
public Foo(String title, String message) {
this.title = title;
this.message = message;
}//Foo
/** Retrieves the View to display (supports API 1.5. To use,
* remove 'extends Fragment' from the class statement, along with
* the method {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)})
* @param context Used for retrieving the inflater */
public View getView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.foo, null);
TextView t = (TextView) v.findViewById(R.id.title);
t.setText(this.title);
TextView m = (TextView) v.findViewById(R.id.message);
m.setText(this.message);
return v;
}//getView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.foo, null);
TextView t = (TextView) v.findViewById(R.id.title);
t.setText(this.title);
TextView m = (TextView) v.findViewById(R.id.message);
m.setText(this.message);
return v;
}//onCreateView
}//Foo
बनाने के लिए और एक गतिविधि में काम करने के लिए दोनों तरीके बहुत सरल हैं, जो कहते हैं, एक List<Foo>को प्रदर्शित करना है (उदाहरण के लिए, प्रोग्रामेटिक रूप से प्रत्येक को जोड़ना ScrollView), तो Fragmentsवास्तव में ये सभी उपयोगी हैं, या क्या वे केवल एक अति-महिमा वाले सरलीकरण हैं एक दृश्य प्राप्त करना, जैसे कि ऊपर दिए गए कोड के माध्यम से?