我想在不改变碎片的情况下刷过很多抽认卡

时间:2014-07-16 17:17:34

标签: java android eclipse android-layout android-fragments

我想用抽认卡学习日语。为此,我创建了三个片段:

这是“开始片段”: Start Fragment

当我点击“Words”时,我会转到“Fragment01”: Fragment01

然后,点击“N5”将我带到“Fragment02”,这是我所有单词的显示位置: Fragment02

这是我的主要活动:

package com.example.testapp2;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        // Create new fragment and transaction
        FragmentManager fm = getSupportFragmentManager(); // getSupportFragmentManager() doesn't work
        FragmentTransaction transaction = fm.beginTransaction();

        StartFragment startFragment = new StartFragment();

        transaction.add(R.id.fragment_placeholder, startFragment);
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //refresh_word();

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.article, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings)
        {
            /*Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);*/
            Toast toast = Toast.makeText(this, "Did'ya think there'd be somthin'?", Toast.LENGTH_LONG);
            toast.show();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public Word[] japanese_words = {};//HERE I HAVE DELETED ALL THE WORDS BECAUSE YOU  WOULD HAVE SCROLLED FOR TOO LONG, BUT IT WORKS FINE

    int word_counter = 0;

    public void onSelectFragment(View view)
    {
        //I deleted the contents because it works too (it basically switches between StartFragment, Fragment01 and Fragment02)
    }

    public void refresh_word()//THIS WORKS TOO, SO I DELETED IT
    {
        // refresh the word on screen so that it matches the word counter and
        // the preferences of the user
    }

    public boolean canContinue()
    {
        //WORKS TOO
    }

    public void prev_word(View view)
    {
        //WORKS TOO
    }

    public void next_word(View view)
    {
        //WORKS TOO
    }
}

这是我的Fragment_02 xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textViewHiragana"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/msg"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/textViewEnglish"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="@string/msg"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textViewKanji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/msg"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="@string/msg"
            android:textSize="25sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:contentDescription="@string/description" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="prev_word"
            android:text="@string/prev" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="next_word"
            android:text="@string/next" />
    </LinearLayout>

</LinearLayout>

一切正常,应该是:

当我点击下一个时,它显示下一个单词,而之前的单词显示前一个单词没有更改碎片

我希望能够删除“下一个”和“上一个”按钮,而不是刷过我的文字(我总共有700个单词)

我一直在寻找解决方案几天,但我发现的唯一一件事就是改变片段

如何在不改变碎片的情况下翻转单词?

1 个答案:

答案 0 :(得分:0)

您可以使用像https://github.com/pakerfeldt/android-viewflow

这样的库来浏览简单视图

您必须为它提供一个适配器,它将为给定的数据列表生成视图。