2013-04-06 20 views
10

Tôi đang cố thêm PreferenceFragment vào FragmentPagerAdapter.Thêm Ưu tiênPhân tích vào FragmentPagerAdapter

Lớp học của tôi mở rộng FragmentActivity, tôi đã thử FragmentTransaction, như được hiển thị bên dưới cũng như cố gắng thêm vào vùng chứa và dường như không có gì để làm việc. No lỗi được ném, trên thực tế không có gì xảy ra.

Hoạt động chính:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
} 

activity_main.xml:

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 
</android.support.v4.view.ViewPager> 

menu Tùy chọn của tôi lựa chọn:

android.app.FragmentManager fm; 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_settings: 
     JJSettings settings = new JJSettings(); 
     fm = getFragmentManager(); 
     FragmentTransaction fragTrans = fm.beginTransaction(); 
     // I also tried `replace()` here as well. Same 'nothing happens' result. 
     fragTrans.add(settings, "settings"); 
     fragTrans.commit(); 
     return true; 
    case R.id.menu_help: 
     menuHelp(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

Cài đặt của tôiFragment:

public class JJSettings extends PreferenceFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.settings); 
    } 
} 

sở thích của tôi sẽ gắn bó với Fragments nếu có thể, có nghĩa là tôi không muốn mở rộng PreferenceActivity hoặc đưa người sử dụng khác Hoạt động mà các cuộc gọi PreferenceFragment, nếu ở tất cả khả thi. Tôi chỉ hy vọng tôi đã bỏ lỡ điều gì đó trong nghiên cứu của mình.


Sửa

public class SectionsPagerAdapter extends FragmentPagerAdapter { 
    private int _count = 2; 

    public SectionsPagerAdapter(FragmentManager fm) { super(fm); } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     return super.instantiateItem(container, position); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
     case 0: 
      return new JJMainFragment(); 
     case 1: 
      return new JJPendingFragment(); 
     default: 
      return null; 
     } 
    } 

    public void setCount(int count) { this._count = count; } 

    @Override 
    public int getCount() { return this._count; } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
     case 0: 
      return getString(R.string.c_list).toUpperCase(Locale.ENGLISH); 
     case 1: 
      return getString(R.string.c_pending).toUpperCase(Locale.ENGLISH); 
     } 
     return null; 
    } 
} 
+0

Bạn có thể gửi mã SectionsPagerAdapter của bạn? – Karakuri

+0

@Karakuri Added, Không chắc chắn 100% lý do tại sao bạn muốn xem nó, tôi không muốn thêm các thiết lập/sở thích vào 'ViewPager'. – jnthnjns

Trả lời

30

This answer dẫn tôi đến giải pháp sử dụng thư viện hỗ trợ v13, bao gồm một FragmentPagerAdapter sử dụng android.app.Fragments bona-fide để nó có thể hỗ trợ PreferenceFragment.

Giả sử bạn sử dụng Eclipse và chạy trình hướng dẫn ứng dụng mới với "cuộn Tabs + Swipe" Điều hướng (trong đó cung cấp cho bạn soạn sẵn v4 pager), đây là những thay đổi bạn cần phải thực hiện để nâng cấp lên v13:

  • Xóa tệp "android-support-v4.jar" khỏi thư mục libs của bạn
  • Sao chép "android-support-v13.jar" từ SDK_PATH \ extras \ android \ support \ v13; nếu nó không có ở đó, sử dụng quản lý SDK để cài đặt hoặc cập nhật "Thư viện Hỗ trợ Extras/Android"

Sau đó, trong file Java:

  • thay đổi FragmentPagerAdapter nhập khẩu từ v4 để v13
  • thay đổi FragmentActivity đến một Hoạt động đơn giản
  • thay đổi các cuộc gọi đến getSupportFragmentManager để getFragmentManager
  • nhập tất cả các lớp cần thiết từ android.app thay vì android. support.v4
  • (Trừ: bạn vẫn cần phải sử dụng ViewPager v4, nhưng nó tương thích)

tôi đã sao chép mã nguồn được sửa đổi dưới đây, xác minh trên mới nhất Jellybean.

MainActivity.java:

package com.example.pagerwithpreferencesfragment; 

import java.util.Locale; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.os.Bundle; 
import android.preference.PreferenceFragment; 
import android.support.v13.app.FragmentPagerAdapter; // instead of v4.app... 
import android.support.v4.view.ViewPager; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MainActivity extends Activity { // no longer FragmentActivity 

    // these comments are now out-of-date; v13, not v4 
    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getFragmentManager()); // instead of getSupportFragmentMangager 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // this is just to show it compiles 
      if (position == 0) { 
       // you should really make this a public class elsewhere.. 
       return new PreferenceFragment() { 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         addPreferencesFromResource(R.xml.settings_preferences); 
        } 
       }; 
      } 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main_dummy, 
        container, false); 
      TextView dummyTextView = (TextView) rootView 
        .findViewById(R.id.section_label); 
      dummyTextView.setText(Integer.toString(getArguments().getInt(
        ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

} 

settings_preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 

<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <PreferenceCategory 
     android:key="my_category_key" 
     android:title="My Title"> 

     <CheckBoxPreference 
     android:key="pref_key" 
     android:title="Title" 
     android:summary="Summary" 
     android:defaultValue="false" 
     /> 

    </PreferenceCategory> 

</PreferenceScreen> 

enter image description here

+0

Điều này rất thú vị, tôi sẽ phải nhìn vào nó và lấy lại cho bạn. +1 cho bản trình bày, tài liệu và hướng dẫn. Nếu tất cả hoạt động tốt cho tôi thì tôi sẽ chấp nhận câu trả lời của bạn. Cảm ơn. – jnthnjns

+1

Cảm ơn. Tôi thấy một câu hỏi tương tự [ở đây] (http://stackoverflow.com/a/11787090/279175) mà được vào thư mục gốc của vấn đề của bạn; máy nhắn tin v4 không hỗ trợ android.app.Fragment, mà là phiên bản v4 của nó, mà PreferenceFragment không bắt nguồn từ đó. – PseudoNoise

+0

Vâng tôi thấy rằng một vài ngày sau khi tôi đăng câu hỏi này. Tôi sắp xóa câu hỏi nhưng có vẻ như độ phân giải của bạn có thể hoạt động. – jnthnjns

0

Documentation

Tôi nghĩ rằng bạn cần phải xác định một id xem container cho các giao dịch đoạn. Hãy thử cho nó id của ViewPager của bạn. Hoặc, đặt ViewPager bên trong một vùng chứa khác, cung cấp cho id đó và sử dụng nó.

Các vấn đề liên quan