2014-09-24 60 views
0

我正在为我的应用创建一个小型闪存卡类型元素。我已经看过几个解决方案,即FragmentStatePagerAdapter,它的父母FragmentPagerAdapter。这些都不能解决我想解决的问题。他们处理碎片的列表视图。我想要一个片段列表,当我点击一个按钮时,我将移动到列表中的下一个片段,直到完成所有的片段。用于链接片段的Android构造

我已经全部保存了它解决的数据部分。我无法弄清楚如何将我的碎片链接在一起。

要明确的,就是我正在寻找的是:

A-> B-> C-> D->完成并返回活动或完成的片段。 用户显然会使用按钮从片段到片段。

我选择了片段,因为我认为这将是最简单的。我并不反对活动,但我的问题仍然大体相同。

我试过实现FragmentPager的东西,但正如我所说,它并没有满足我的需求。

+0

当我写这个,我想我可能有另一个想法。这将是一个监督片段的类,但我没有把它推广到足以按照我想要的那样工作。 – lilott8 2014-09-24 23:34:12

回答

0

我想出如何与一个相当简单的解决方案做到这一点:

我有我的活动具有一定的变量,以便它知道它是什么片段,随着它膨胀的布局具有的FrameLayout和使用fragmentmanager事务来替换给定的我们所在的片段号。然后我有一个parcelable类,用于定义在实例化时传递给每个片段的flashcard。在活动的布局上,我有3个按钮,“检查”,“正确”,“不正确”,使用View.GONE/View.VISIBLE可以提供我想要的UI体验。在点击“正确”/“不正确”时,我们开始交易并将列表向下移动到下一张牌。

代码:

/** 
    * The activity 
    */ 
    public class VocabTestActivity extends Activity { 

     private int mWordsCorrect = 0; 
     private int mWordsIncorrect = 0; 
     private int mCurrentPosition = 0; 
     private ArrayList<Fragment> mCards = new ArrayList<Fragment>(); 
     private final int FLAG_TOTAL_CARDS = 5; 
     private GrammarDataSource mDataSource; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_practice); 

      final Button buttonCheck = (Button) findViewById(R.id.buttonCheckWordPractice); 
      final Button buttonCorrect = (Button) findViewById(R.id.buttonCorrectWordPractice); 
      final Button buttonIncorrect = (Button) findViewById(R.id.buttonIncorrectWordPractice); 
      final TextView textViewProgressBar = (TextView) findViewById(R.id.textViewProgressBarPractice); 

      this.mDataSource = new GrammarDataSource(this); 

      this.initializeCards(); 

      buttonCheck.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        buttonCheck.setVisibility(View.GONE); 
        buttonCorrect.setVisibility(View.VISIBLE); 
        buttonIncorrect.setVisibility(View.VISIBLE); 
       } 
      }); 

      buttonCorrect.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        mWordsCorrect++; 
        getFragmentManager().beginTransaction().replace(
          R.id.practice_frame, 
          mCards.get(mCurrentPosition++) 
        ).commit(); 
        buttonCheck.setVisibility(View.VISIBLE); 
        buttonCorrect.setVisibility(View.GONE); 
        buttonIncorrect.setVisibility(View.GONE); 
        textViewProgressBar.setText(getString(R.string.practice_progress_bar, mCurrentPosition, FLAG_TOTAL_CARDS)); 
       } 
      }); 

      buttonIncorrect.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        mWordsIncorrect++; 
        getFragmentManager().beginTransaction().replace(
          R.id.practice_frame, 
          mCards.get(mCurrentPosition++) 
        ).commit(); 
        buttonCheck.setVisibility(View.VISIBLE); 
        buttonCorrect.setVisibility(View.GONE); 
        buttonIncorrect.setVisibility(View.GONE); 
        textViewProgressBar.setText(getString(R.string.practice_progress_bar, mCurrentPosition, FLAG_TOTAL_CARDS)); 
       } 
      }); 
     } 

     private void initializeCards() { 
      for(VocabWord v : this.mDataSource.selectFlashCards(FLAG_TOTAL_CARDS)) { 
       VocabTestFragment frag = VocabTestFragment.newInstance(new ParcelableWord(v)); 
       mCards.add(frag); 
      } 
     } 
    } 


    /** 
    * The fragment 
    */ 
    public class VocabTestFragment extends Fragment { 

     private ViewGroup mRoot; 
     public final String TAG = getClass().getSimpleName(); 
     private VocabWord mWord; 

     public static VocabTestFragment newInstance(ParcelableWord w) { 
      VocabTestFragment frag = new VocabTestFragment(); 
      Bundle args = new Bundle(); 
      args.putParcelable("word", w); 
      frag.setArguments(args); 
      return frag; 
     } 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      this.mRoot = (ViewGroup) inflater.inflate(R.layout.fragment_practice_vocab, container, false); 

      ItalianWord word = null; 

      ParcelableWord pw = getArguments().getParcelable("word"); 
      pw.printIt(); 
      word = (ItalianWord) pw.getWord(); 

      TextView tv = (TextView) this.mRoot.findViewById(R.id.word); 
      if(word != null) { 
       tv.setText(word.getmId() + " is the id\t" + word.getmWord()); 
      } else { 
       tv.setText("Word not provided"); 
      } 

      return this.mRoot; 
     } 
    } 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@android:drawable/gallery_thumb" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/word" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical|center_horizontal" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="placeholder" 
     android:layout_weight="4" /> 

</LinearLayout> 

/** 
* practice_activity 
*/ 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="4dip" 
    android:gravity="center_horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textViewProgressBarPractice" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/practice_progress_bar" 
     /> 

    <FrameLayout 
     android:id="@+id/practice_frame" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewProgressBarPractice" 
     > 

    </FrameLayout> 



<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_gravity="end" 
    android:layout_alignParentBottom="true" 
    > 
    <Button 
     android:id="@+id/buttonCheckWordPractice" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Check" 
     /> 

    <Button 
     android:id="@+id/buttonCorrectWordPractice" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:visibility="gone" 
     android:text="Correct" 
     /> 
    <Button 
     android:id="@+id/buttonIncorrectWordPractice" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:visibility="gone" 
     android:text="Incorrect" 
     /> 

    </LinearLayout> 
</RelativeLayout> 
+0

有明显的错误处理丢失,但逻辑是重要的。 – lilott8 2014-09-25 22:48:02

2

你正在制作的碎片有多动态?如果存在一定数量的可互换元素,则可以尝试在主活动中创建委托函数,以根据一组参数打开片段。更好的是,你可以让你的片段变得模块化,这样你就只能根据你给他们的东西只有几个不同状态的片段。

public void onCardWithIdSelected(int id, String param1, String param2, ...) { 
    Fragment fragment = NULL; 
    if(id == 0) { 
     fragment = cardFragment.newInstanceFromParams(param1, param2, ...); //this will pass the parameters onto the desired fragment 
    } 
    else if(id == 1) { 
     fragment = cardFragment.newInstanceFromParams(param1, param2, ...); //this will pass the parameters onto the desired fragment 
    } 
    else if(id == 2) { 
     fragment = cardFragment.newInstanceFromParams(param1, param2, ...); //this will pass the parameters onto the desired fragment 
    } 
    //and so on... 
    FragmentTransaction transaction = getSupportFragmentManager() 
      .beginTransaction(); 

    transaction.replace(R.id.content_frame, fragment); 
    transaction.addToBackStack(null); //only do this if you don't want users to be able to go back 

    // Commit the transaction 
    transaction.commit(); 
} 

然后,每当你想从一个移动到不同的片段,您只需拨打的主要活动此功能与您的需要的参数。