Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ViewPager,滚动选项卡+;刷卡_Java_Android_Tabs_Android Viewpager_Adapter - Fatal编程技术网

Java ViewPager,滚动选项卡+;刷卡

Java ViewPager,滚动选项卡+;刷卡,java,android,tabs,android-viewpager,adapter,Java,Android,Tabs,Android Viewpager,Adapter,我有一些选项卡,其中有一些视图,如编辑文本,复选框,单选按钮等等。我使用ViewPager在这些选项卡之间滑动 这是我的代码: 活动\u 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/pag

我有一些选项卡,其中有一些
视图
,如
编辑文本
复选框
单选按钮
等等。我使用ViewPager在这些选项卡之间滑动

这是我的代码:

活动\u 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">

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>
fragment\u main\u dummy.xml1个选项卡xml的示例

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin"
              tools:context=".MainActivity$DummySectionFragment"
              android:orientation="vertical">

    <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="Mda"/>

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/field1"
            android:tag="Muda"
            android:text="lalalala"/>

    <CheckBox
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New CheckBox"
            android:id="@+id/checkBox"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="76dp"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_gravity="left|center_vertical"
            android:layout_marginLeft="158dp"/>

    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton"
            android:layout_gravity="left|center_vertical"/>

    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2"
            android:layout_gravity="left|center_vertical"/>

    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton3"
            android:layout_gravity="left|center_vertical"/>

</LinearLayout>

问题:

我想做的是在更改页面后从视图中收集所有输入。当用户更改ViewPage并转到第二个选项卡时,我需要获取他在上一个选项卡中编写的所有信息

我可以通过设置primaryItem获得当前页面片段的视图,但无法获得上一个选项卡的视图。怎样才能做到呢

提前谢谢。

给你的东西不多

保存普通视图的状态必须手动完成,唯一能够为您保存状态的视图是TextView及其子视图。因此,由于EditText是一个文本视图,这是可能的

要尝试的属性:

setFreezesText()或android:freezesText for xml

(注意:仅当TextView具有ID时才起作用)

除了动态状态(如光标位置)外,此选项还控制此文本视图在冻结到冰柱时是否保存其全部文本内容。默认情况下,这是false,不保存文本。如果文本视图中的文本未保存在持久性存储中的其他位置(如内容提供程序中),则设置为true,以便在以后解冻视图时,用户不会丢失其数据

Android文档参考:

您还可以尝试更改“视图寻呼机”缓存的视图数限制。这样做可以使更多视图保持活动状态,而无需重新创建视图,因此视图中的数据不会丢失

您可以使用查看寻呼机的以下方法来实现这一点:

setOffScreenPageLimit(int)

Android文档参考:

现在,保存所有选项卡中的所有数据可以通过多种不同的方式完成,例如,您可以使用getItem方法中的args包来存储第一个选项卡的数据,并将其传递给第二个选项卡

 @Override
    public Fragment getItem(int position) {

        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putString("key",myTextView.getText().toString());
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }
现在,虚拟片段的新实例将使用键“key”存储来自该TextView的文本,您可以使用片段的getArguments()方法检索它

例如:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

     //here is your arguments
     Bundle bundle=getArguments(); 

    //here is your list array 
    String myString=bundle.getString("key");   
 }
您还可以使用SharedReferences存储数据,然后在希望将其存储到数据库中时检索数据

这可能有点笼统,但它对共享pref有一些很好的参考

如果这个答案有用,或者你需要更多帮助,请告诉我

编辑

好吧,我想我找到了一种方法,你可以实现你想做的事情,这有点不成熟,但在找到更好的解决方案之前,它将完成工作。如果我有时间的话,我可能会再仔细研究一下

但现在,你可以做些什么

方法onPageSelected(int位置)是完成大部分工作的地方

@Override
public void onPageSelected(int position) {
            //This makes sure we get the previous position in the view pager and also
            //makes sure we don't go to -1 when you swipe back to the 0th position.

            //The problem here is if you are using action bar tabs, the user can also select the third tab, from the first tab and the index will be pointing to the second tab. I did not go out of my way to handle this case. But with a few checks you can handle the cases.

            //Also without proper checks, swiping backwards will be an issue, so adding those cases may also be necessary.
    int index = position == 0 ? position : position - 1;


    View view = mViewPager.getChildAt(index);

            // View at this point is the previous view in your view pager
            // and you can access your textviews in the following way.

    TextView myTextView = (TextView) view.findViewById(R.id.section_label);


            // If using action bar tabs, set the selected tab.
    mActionBar.setSelectedNavigationItem(position);
}
就像我说的,它非常粗糙,涉及一些检查条件,但它会工作的。当我有时间的时候,我会寻找一种更优雅的方法来做这件事,然后把它发回到这个帖子上


祝您好运

我认为您必须使用setArgument()和getArgument()在片段之间传输数据,因为您将为每个选项卡获得虚拟片段的新实例。我不需要在片段之间传输数据,我只需要在用户开始更改页面时保存该数据。然后将该数据保存在onPageSroll()onPageScrolled()中在一个页面滚动期间调用了很多次,所以您只想将文本保存在EditText中,这样当用户滑回该选项卡时,它仍然存在?这是正确的吗?感谢您的帮助,我的主要问题是何时获取EditText的值,我试图在页面滚动后获取onPageSelected中上一个片段的视图,但我无法,getItem(position)返回没有视图的整个新片段。谢谢@DejanRistic的回复。
@Override
public void onPageSelected(int position) {
            //This makes sure we get the previous position in the view pager and also
            //makes sure we don't go to -1 when you swipe back to the 0th position.

            //The problem here is if you are using action bar tabs, the user can also select the third tab, from the first tab and the index will be pointing to the second tab. I did not go out of my way to handle this case. But with a few checks you can handle the cases.

            //Also without proper checks, swiping backwards will be an issue, so adding those cases may also be necessary.
    int index = position == 0 ? position : position - 1;


    View view = mViewPager.getChildAt(index);

            // View at this point is the previous view in your view pager
            // and you can access your textviews in the following way.

    TextView myTextView = (TextView) view.findViewById(R.id.section_label);


            // If using action bar tabs, set the selected tab.
    mActionBar.setSelectedNavigationItem(position);
}