The Fragment and Custom View can achieve the similar function, I know that fragment is more re-usable comparing with custom view, any other benefits/enhancements for using Fragment? Is fragment supposed to replace Custom View, or just a enhancement for some specific purpose?
For instance, the code below is fragment:
public class TestFragment extends Fragment { private TextView tv_name; private Button btn_play; private Button btn_delete; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.testfragment, container, false); } @Override public void onStart() { super.onStart(); tv_name = (TextView)getView().findViewById(R.id.tv_name); btn_play = (Button)getView().findViewById(R.id.btn_play); btn_delete = (Button)getView().findViewById(R.id.btn_delete); } }
The code for custom view:
public class TestCustomView extends LinearLayout { private TextView tv_name; private Button btn_play; private Button btn_delete; public TestCustomView(Context context, AttributeSet attrs){ super(context, attrs); setOrientation(LinearLayout.HORIZONTAL); setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); tv_name = new TextView(context); addView(tv_name); btn_play = new Button(context); addView(btn_play); btn_delete = new Button(context); addView(btn_delete); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.testfragment, container, false); } }
Both TestFragment
and TestCustomView
can create a view consisting of TextView
and Buttons
, and use tags of Framelayout/fragment
and com.packagename.TestCustomView
to declare in the activity's xml layout file, but what's the advantages to use Fragment?
Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.
Because an Android fragment is not a view, adding it to an activity looks somewhat different than adding a view (e.g. TextView ). A fragment is added to a ViewGroup inside the activity. The fragment's view is displayed inside this ViewGroup .
Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.
According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.
Fragment can be used in different scenarios but most used are:
There are cases where fragment are complete pain in the neck, then there are cases where they can achieve results quicker.
For some custom and more flexible situations fragments can get cluttered and managing them would be difficult. So dealing with views directly can be really handy and more helpful for some cases. But everything is based on requirements.
Note View
has its own life cycle too and can store/recreate saved instance state. A little bit more work but it has the option too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With