在平板上實(shí)現(xiàn)的效果如下:
左邊使用ListFragment角雷,靜態(tài)加載姻成,調(diào)用setAdapter()設(shè)置Adapter即可。右邊使用了動(dòng)態(tài)加載上遥。Activity通過實(shí)現(xiàn)在ListFragment中的內(nèi)部接口刽肠,這樣Fragment就可以調(diào)用該回調(diào)方法將數(shù)據(jù)傳給Activity溃肪。
- 首先用于內(nèi)容顯示的數(shù)據(jù)類
package com.example.myactionbardemo.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* Created by W on 2016/7/30.
*/
public class StartContent {
//定義一個(gè)內(nèi)部類
public static class Star{
public Integer id;
public String name;
public String desc;
public Star(Integer id, String name, String desc) {
this.id = id;
this.name = name;
this.desc = desc;
}
@Override
public String toString() {
return name;
}
}
public static List<Star> ITEMS = new ArrayList<Star>();
public static Map<Integer, Star> ITEM_MAP = new HashMap<Integer, Star>();
static {
addItem(new Star(1, "易烊千璽", "跳舞擔(dān)當(dāng)"));
addItem(new Star(2, "王源", "特別可愛"));
addItem(new Star(3, "王俊凱", "隊(duì)長"));
}
public static void addItem(Star star){
ITEMS.add(star);
ITEM_MAP.put(star.id, star);
}
}
- 然后布局視圖,左邊添加了StarListFragment音五,右邊是一個(gè)FrameLayout 的容器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<fragment
android:name="com.example.myactionbardemo.StarListFragment"
android:id="@+id/star_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/star_list_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
- 左邊的ListFragment代碼如下
package com.example.myactionbardemo;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.myactionbardemo.model.StartContent;
/**
*
* Created by W on 2016/7/30.
*/
public class StarListFragment extends ListFragment {
private Callbacks mCallbacks;
//定義一個(gè)回調(diào)接口惫撰,在Activity中需要實(shí)現(xiàn)
//該Framgent將通過該接口與他它所在的Activity交互
public interface Callbacks{
void onItemSelectedListener(Integer id);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ArrayAdapter()里的第二個(gè)參數(shù)是系統(tǒng)提供的布局,第三個(gè)是布局里TextView的id
setListAdapter(new ArrayAdapter<StartContent.Star>(getActivity()
, android.R.layout.simple_list_item_activated_1,
android.R.id.text1, StartContent.ITEMS));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof Callbacks)){
throw new IllegalStateException("必須實(shí)現(xiàn)Callbacks接口");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//回調(diào)該接口放仗,傳入了id
mCallbacks.onItemSelectedListener(StartContent.ITEMS.get(position).id);
}
}
- StarDetailFragmentjava
package com.example.myactionbardemo;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myactionbardemo.model.StartContent;
/**
*
* Created by W on 2016/7/30.
*/
public class StarDetailFragment extends Fragment{
public static final String ITEM_ID = "item_id";
StartContent.Star star;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ITEM_ID)){
star = StartContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));
}
}
@Nullable
@Override
//傳入布局
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_start_detail, container, false);
if (star != null){
TextView textView = (TextView) view.findViewById(R.id.star_name);
textView.setText(star.name);
TextView textView1 = (TextView) view.findViewById(R.id.star_desc);
textView1.setText(star.desc);
}
return view;
}
}
- 顯示在StarDetailFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/star_name"
android:padding="16dp"/>
<TextView
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/star_desc"
android:padding="16dp"/>
</LinearLayout>
- 最后的Activity實(shí)現(xiàn)了從ListFrgment到StarDetailFragment的數(shù)據(jù)傳遞润绎。
package com.example.myactionbardemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
*
* Created by W on 2016/7/30.
*/
//實(shí)現(xiàn)了內(nèi)部接口
public class ListFragmentActivity extends AppCompatActivity implements StarListFragment.Callbacks{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_star_two);
}
@Override
public void onItemSelectedListener(Integer id) {
Bundle bundle = new Bundle();
bundle.putInt(StarDetailFragment.ITEM_ID, id);
StarDetailFragment fragment = new StarDetailFragment();
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.star_list_detail,fragment).commit();
}
}
使用內(nèi)部接口來進(jìn)行Fragment和Activity傳遞數(shù)據(jù)撬碟,真的很奇妙诞挨,編程就是如此奇妙啊啊啊