17

Tôi đang cố gắng tùy chỉnh bố cục phân đoạn bằng cách trả lại phân cấp chế độ xem của riêng mình từ onCreateView(LayoutInflater, ViewGroup, Bundle). Điều này làm tăng lượt xem tùy chỉnh của tôi, nhưng dường như ngăn xếp các lượt xem thay vì lạm phát trong, tôi thấy mọi thứ cùng một lúc. Bất kỳ trợ giúp được đánh giá cao.Android + ListFragment với hệ thống phân cấp chế độ xem tùy chỉnh

MyActivity.java:

public class MyActivity extends FragmentActivity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { 
      ArrayListFragment list = new ArrayListFragment(); 
      getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 
     } 
    } 

    public static class ArrayListFragment extends ListFragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.main, container); 
      return super.onCreateView(inflater, container, savedInstanceState); 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      String List[] = {"Larry", "Moe", "Curly"}; 
      setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List)); 
     } 
    } 
} 

main.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:paddingLeft="8dp" 
    android:paddingRight="8dp"> 

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="THIS IS A BUTTON" /> 

    <ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00FF00" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false" /> 

    <TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FF0000" 
    android:text="No data" /> 
</LinearLayout> 
+0

Cũng cần lưu ý, tôi đang sử dụng android-support-v4. Từ các tài liệu android, nó nêu rõ: "ListFragment có một cách bố trí mặc định mà bao gồm một đơn danh sách view Tuy nhiên, nếu bạn muốn, bạn có thể tùy chỉnh cách bố trí đoạn bởi trở về hệ thống phân cấp cái nhìn riêng của bạn từ onCreateView (. Để thực hiện việc này, phân cấp chế độ xem của bạn phải chứa đối tượng ListView với id "@android: id/list" (hoặc liệt kê nếu nó ở mã ). " – worked

Trả lời

33

Tôi đã không trả lại cái nhìn mới trong phương pháp onCreateView của lớp ListFragment. Ví dụ:

 @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.main, null); 
      return view; 
     } 

Tất cả đều hoạt động tốt ngay trong vùng đất của Android!

+4

Dòng nắm tay trong 'onCreateView' của bạn là không cần thiết (nó không thực sự làm bất cứ điều gì ...) Các inflater đã được thông qua trong (mà bạn sử dụng). – ashughes

+1

Điểm tốt ... được cập nhật. – worked

+0

Bạn có thể cung cấp thêm chi tiết? –

1
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 

Danh sách không được chấp nhận, nó đang tìm kiếm một đoạn.

2

Tôi nghĩ rằng bạn muốn tốt hơn

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View root = inflater.inflate(R.layout.main, container, false); 
return root; 
} 
0

sử dụng mô hình này, hoạt động hoàn hảo!

inflater.inflate(R.layout.list_layout, container, false); 

công khai Xem thổi phồng (XmlPullParser phân tích cú pháp, ViewGroup gốc, boolean attachToRoot)

attachToRoot Cho dù hệ thống phân cấp tăng cao nên được gắn liền với các tham số gốc? Nếu sai, root chỉ được sử dụng để tạo lớp con đúng của LayoutParams cho khung nhìn gốc trong XML.

2

cách tối ưu hóa và bắn đang

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.list_alphabet, container, false); 
    } 
Các vấn đề liên quan