下圖說明了我的活動的複雜子片段添加到Android的一個片段。有多個(在這種情況下是3個)需要動態加載的片段層。使用LinearListView加載重複片段,這是我在此處找到的視圖庫:https://github.com/frankiesardo/LinearListView。這允許列表像ListView一樣加載,但是避免了在ScrollView中有ListView的問題。
下面是一些示例代碼:
line_item_list.xml
<?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"
android:id="@+id/line_item_list">
<TextView
android:layout_width="171dp"
android:layout_height="wrap_content"
android:text="LineItem List"
android:id="@+id/line_item_list_text" />
<com.linearlistview.LinearListView
android:id="@+id/line_item_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:orientation="vertical"
android:showDividers="middle"
app:dividerThickness="16dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"/>
</LinearLayout>
line_item.xml
<?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"
android:id="@+id/line_item">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my line item."
android:id="@+id/line_item_text"
android:layout_gravity="center_horizontal" />
<!-- notes child items go here -->
</LinearLayout>
LineItemFragment.java加載片段與適配器(LINEITEM在此代碼與上圖中的「零件」一起顯示。)
public class LineItemFragment extends Fragment {
LineItemAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearListView lineItems = (LinearListView)container.findViewById(R.id.line_item_wrapper);
adapter = new LineItemAdapter(this.getContext(), getChildFragmentManager());
lineItems.setAdapter(adapter);
return null;
}
}
LineItemAdapter.java
public class LineItemAdapter extends BaseAdapter{
ArrayList<String> lineItems = new ArrayList<String>();
Context context;
FragmentManager fm;
public LineItemAdapter(Context context, FragmentManager fragmentManager) {
this.context = context;
lineItems.add("Item A");
lineItems.add("Item B");
lineItems.add("Item C");
this.fm = fragmentManager;
}
@Override
public int getCount() {
return lineItems.size();
}
@Override
public Object getItem(int position) {
return lineItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.line_item, parent, false);
}
((TextView)convertView.findViewById(R.id.line_item_text)).setText(getItem(position).toString());
// *** The getView method could load the child fragments
return convertView;
}
}
在此之前的文件(***),我目前想的getView方法應加載下一個級別的子片段,但一切我都試過沒有奏效。看起來佈局還沒有膨脹,所以我不能將子視圖添加到它。
我的問題是,「我如何從getView中添加子片段?」或者,也許我正在討論這個錯誤。
這裏是什麼,我已經試過了一個例子:
getView方法(***)在lineItemAdapter.java:
.
.
.
LinearLayout notes = (LinearLayout) ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.line_item, parent, false);
LinearLayout b = (LinearLayout)((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.note_list, notes).getRootView();
NoteListFragment noteListFragment = new NoteListFragment();
fm.beginTransaction().add(b.getId(), noteListFragment).commit();
.
.
.
而且我得到了這樣的一個例外。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 12207
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.myapplication/com.example.myapplication.DetailActivity}: java.lang.IllegalStateException: Fragment does not have a view
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
.
.
.
在此先感謝。
編輯
我需要一個片段添加到XML,即:
line_item.xml ...新的節點:
<fragment android:name="com.example.noteListFragment"
android:id="@+id/note_list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
不同類你不能'getView()'屬於一個片段中添加片段。你從你的活動中創建一個片段。 – mt0s
檢查此答案也http://stackoverflow.com/a/13221546/423980 – mt0s
欣賞快速反應。很有幫助,但您將如何解決從活動中請求片段的多個層的複雜性?我能否將convertview傳遞給Activity中的方法而不會導致內存泄漏,以便可以添加子項? – dwaz