Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
深入淺出談fragment
Search
fantasy
January 30, 2015
Programming
0
94
深入淺出談fragment
有基本知識來介紹frgament元件,到要如何使用fragment,以及需要注意的地方。
fantasy
January 30, 2015
Tweet
Share
More Decks by fantasy
See All by fantasy
Use multi Analytics and Crashlytic smothly
fantasy1022
0
61
Exploring RxJava2
fantasy1022
0
64
Other Decks in Programming
See All in Programming
オンコール⼊⾨〜ページャーが鳴る前に、あなたが備えられること〜 / Before The Pager Rings
yktakaha4
0
130
型で語るカタ
irof
0
200
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
210
Goで作る、開発・CI環境
sin392
0
240
テスト駆動Kaggle
isax1015
1
430
PicoRuby on Rails
makicamel
2
130
Claude Code + Container Use と Cursor で作る ローカル並列開発環境のススメ / ccc local dev
kaelaela
10
6k
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
180
Advanced Micro Frontends: Multi Version/ Framework Scenarios @WAD 2025, Berlin
manfredsteyer
PRO
0
280
What's new in AppKit on macOS 26
1024jp
0
110
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
780
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
640
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
830
How STYLIGHT went responsive
nonsquared
100
5.6k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
How to Ace a Technical Interview
jacobian
278
23k
A designer walks into a library…
pauljervisheath
207
24k
Writing Fast Ruby
sferik
628
62k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Transcript
Easy to understand Fragment
What is fragment? Is it another type of Activity? Does
it replace Activity? Or is it a new component ?
What is fragment? Is it another type of Activity? Does
it replace Activity? Or is it a new component ? It is a modular section of an activity!!
Relationship with Activity Embedded in an activity surely Depend on
activity lifecycle Inside activity’s view hierarchy
Fragment advantage Multi-pane UI support Reuse component Add or remove
component dynamically
When to use fragment
Multi-pane UI support
None
None
None
None
None
How to use fragment 1 <fragment 2 android:id="@+id/list" 3 android:name=“com.new.ExampleFragment"
4 android:layout_width="match_parent" 5 android:layout_height="match_parent" /> Inside the activity’s layout
Add fragment programmatically 1 FragmentManager fm = getFragmentManager(); 2 FragmentTransaction
ft = fm.beginTransaction(); 3 ExampleFragment fragment = new ExampleFragment(); 4 ft.add(R.id.fragment_container, fragment); 5 ft.commit(); 1 <FrameLayout 2 android:id="@+id/fragment_container" 3 android:layout_width="wrap_content" 4 android:layout_height=“wrap_content"/>
Fragment Transactions 1 FragmentTransaction ft = fm.beginTransaction(); 2 ft.add(R.id.fragment_container, fragment);
3 ft.replace(R.id.fragment_container, fragment); Modify fragment 4 ft.show(fragment) 5 ft.hide(fragment) 6 ft.remove(fragment) Modify fragment already in activity Set transition style 7 ft.setTransition(TRANSIT_FRAGMENT_CLOSE) 8 ft.setCustomAnimations(entet,exit)
Fragment Communication Just like activity communication directly?
Fragment Communication Just like activity communication directly? Communicate through activity
!
Main Activity fragment Controller
Main Activity fragment Controller
1 public class ChannelDetailFragment extends Fragment { 2 private OnChannelClickListener
mCallback; 3 4 public interface OnChannelClickListener { 5 public void onChannelClick(int channelNumber); 6 } 7 @Override 8 public void onAttach(Activity activity) { 9 super.onAttach(activity); 10 try { 11 mCallback = (OnChannelClickListener) activity; 12 } catch (ClassCastException e) { 13 throw new ClassCastException(activity.toString() 14 + " must implement OnChannelClickListener"); 15 } 16 } 17 public boolean onClick(View v) { 18 mCallback.onChannelClick(channelNumber); 19 } In fragment file
In activity file 1 public class Activity extends Activity 2
implements OnChannelClickListener { 3 4 @Override 5 public void onChannelClick(int channelNumber){ 6 // do something or open another fragment. 7 } 8 }
Fragment view Use setContentView() to set view?
Fragment view Use setContentView() to set view? 1 @Override 2
public View onCreateView(LayoutInflater inflater, ViewGroup 3 container, Bundle savedInstanceState) { 4 View root = inflater.inflate(R.layout.fragment_layout, 5 container, false); 6 //init other view 7 return root; 8 } Use onCreateView callback to set view !
Fragment tips Toast.makeText(getActivity(), "Mission complete", Toast.LENGTH_LONG); Get context from activity
Add to BackStack 1 FragmentManager fm = getFragmentManager(); 2 FragmentTransaction ft = fm.beginTransaction(); 3 ft.replace(R.id.fragment_container, fragment); 4 ft.addToBackStack(null);
Import v4 or origin version 1 import android.support.v4.app.FragmentActivity; 2 import
android.support.v4.app.FragmentManager; 3 import android.support.v4.app.FragmentTransaction; 4 import android.support.v4.app.Fragment; 1 import android.app.Activity; 2 import android.app.FragmentManager; 3 import android.app.FragmentTransaction; 4 import android.app.Fragment;
Use new Fragment() or Fragment.newInstance()? 1 ChannelFragment fragment = new
ChannelFragment(); 2 Bundle args = new Bundle(); 3 args.putInt(CHANNEL_PAGE_INDEX, index); 4 fragment.setArguments(args);
1 public static ChannelFragment newInstance(int index) { 2 ChannelFragment fragment
= new ChannelFragment(); 3 Bundle args = new Bundle(); 4 args.putInt(CHANNEL_PAGE_INDEX, index); 5 fragment.setArguments(args); 6 return fragment; 7 } ChannelFragment.newInstance(index); Use static factory method for better!
Thank you! Fantasy