Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
深入淺出談fragment
Search
fantasy
January 30, 2015
Programming
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
深入淺出談fragment
有基本知識來介紹frgament元件,到要如何使用fragment,以及需要注意的地方。
fantasy
January 30, 2015
More Decks by fantasy
See All by fantasy
Use multi Analytics and Crashlytic smothly
fantasy1022
0
63
Exploring RxJava2
fantasy1022
0
66
Other Decks in Programming
See All in Programming
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
気持ちぃ〜角丸 Squircle
ryu1013
0
310
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
970
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
0
130
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
Railsバージョンを4倍にした話
optim
0
450
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
3.9k
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
770
contenteditable と日本語入力に向き合う
colorful12
0
200
JIS keyboard support in Omarchy
lef237
0
130
Featured
See All Featured
Ethics towards AI in product and experience design
skipperchong
2
440
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
A Soul's Torment
seathinner
8
3.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Amusing Abliteration
ianozsvald
1
350
Odyssey Design
rkendrick25
PRO
2
880
The Limits of Empathy - UXLibs8
cassininazir
2
720
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
570
Paper Plane (Part 1)
katiecoart
PRO
2
12k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.3k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
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