1.以新闻布局为例,首先创建新闻标题fragment 和 新闻内容fragment
1)首先创建手机新闻标题的fragment布局文件
fragment_news_list.xml
2)创建手机新闻的fragment
public class NewsListFragment extends Fragment implements AdapterView.OnItemClickListener {
/*
* fragment 定义的接口 用于给activity传递一个listview点击事件
*
* */
public interface OnNewsSelectedListener{
void onNewsSelected(Bundle bundle);
}
private OnNewsSelectedListener mOnNewsSelectedListener;
public NewsListFragment() {
// Required empty public constructor
}
//当引用Fragment生命周期的onAttach()方法时,就自动获取MainActivity的上下文,所以不用再通过该方法传递上下文了
// public void setOnNewsSelectedListener(OnNewsSelectedListener OnNewsSelectedListener){
// mOnNewsSelectedListener = OnNewsSelectedListener;
// }
@Override
public void onAttach(Context context) {
super.onAttach(context);
//内部设置 接口回调
if (context instanceof OnNewsSelectedListener){
mOnNewsSelectedListener = (OnNewsSelectedListener) context;
}else {
throw new IllegalArgumentException("Activity must OnNewsSelectedListener");
}
}
@Override
public void onDetach() {
mOnNewsSelectedListener = null;
super.onDetach();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View ret = inflater.inflate(R.layout.fragment_news_list, container, false);
ListView listView = (ListView) ret.findViewById(R.id.news_list);
//设置点击
listView.setOnItemClickListener(this);
if (listView != null){
ArrayList data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
data.add("News" + i);
}
ArrayAdapter mAdapter = new ArrayAdapter(
getContext(),android.R.layout.simple_list_item_1,
data
);
listView.setAdapter(mAdapter);
}
return ret;
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
if (mOnNewsSelectedListener != null){
Bundle bundle = new Bundle();
bundle.putInt("position",position);
bundle.putLong("id",id);
mOnNewsSelectedListener.onNewsSelected(bundle);
}
}
}
3)创建新闻内容的fragment布局文件
fragment_detail_title.xml
4)创建新闻的内容的fragment
public class DetailFragment extends Fragment {
private TextView mTextTitle;
public DetailFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View ret = inflater.inflate(R.layout.fragment_detail, container, false);
mTextTitle = (TextView)ret.findViewById(R.id.detail_title);
Bundle arguments = getArguments();
if (arguments != null){
long id = arguments.getLong("id");
mTextTitle.setText("id = " + id);
}
return ret;
}
public void setDetailTitle(String title){
mTextTitle.setText(title);
}
}
2.创建UI线程用来放置fragment,考虑到横屏竖屏的问题,需要创建不同的activity_main.xml布局文件
1)竖屏的布局文件activity_main.xml
2)横屏的布局文件activity_main.xml
public class MainActivity extends AppCompatActivity implements NewsListFragment.OnNewsSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onNewsSelected(Bundle bundle) {
Log.d("OnNewsSelected", "bundle: " + bundle);
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragment_detail);
if (fragment != null && fragment.isVisible()){
DetailFragment detailFragment = (DetailFragment) fragment;
detailFragment.setDetailTitle(bundle.toString());
}else{
Intent intent = new Intent(this,DetailActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
1) 示例代码如下:
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// 如果 savedInstanceState == null 代表一次创建 否则代表屏幕发生过旋转
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras != null){
DetailFragment detailFragment = new DetailFragment();
//将从UI线程传递过来的bundle ,通过下面的方法传递到fragment中
detailFragment.setArguments(extras);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction tx = manager.beginTransaction();
tx.replace(R.id.detail_fragment_container,detailFragment);
//在事物提交之后 会进入到 fragment的创建的生命周期
// 不能在这个语句执行的附近
tx.commit();
}
}
}
}