এর জন্য বিকাশ করার সময় Android, আপনি আপনার টার্গেট (বা সর্বনিম্ন) এসডিকে 4 (এপিআই 1.6) এ সেট করতে এবং এর জন্য সমর্থন যুক্ত করতে অ্যান্ড্রয়েড সামঞ্জস্যতা প্যাকেজ (ভি 4) যুক্ত করতে পারেন Fragments। গতকাল আমি এটি করেছি এবং Fragmentsকাস্টম ক্লাসের ডেটা ভিজ্যুয়ালাইজ করতে সফলভাবে প্রয়োগ করেছি implemented
আমার প্রশ্নটি হ'ল: Fragmentsকোনও কাস্টম অবজেক্ট থেকে কেবল ভিউ পাওয়ার বিপরীতে ব্যবহার করা এবং এখনও API 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সত্যই সমস্ত দরকারী, বা এগুলি কেবল মাত্র একটি অতি-প্রশংসিত সরলকরণ উপরের কোডের মাধ্যমে যেমন একটি ভিউ পাচ্ছেন?