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
98
深入淺出談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
63
Exploring RxJava2
fantasy1022
0
65
Other Decks in Programming
See All in Programming
ぼくの開発環境2026
yuzneri
0
240
SourceGeneratorのススメ
htkym
0
200
Oxlint JS plugins
kazupon
1
1k
React Native × React Router v7 API通信の共通化で考えるべきこと
suguruooki
0
100
AI & Enginnering
codelynx
0
120
2026年 エンジニアリング自己学習法
yumechi
0
140
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
140
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
440
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
250
Apache Iceberg V3 and migration to V3
tomtanaka
0
170
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
460
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
740
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
920
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
260
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
230
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
Side Projects
sachag
455
43k
Ethics towards AI in product and experience design
skipperchong
2
200
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
350
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Building Adaptive Systems
keathley
44
2.9k
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