- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些选项卡,其中有一些 View
,例如EditText
、CheckBox
、RadioButton
等。并使用ViewPager我在这些选项卡之间滑动。
这是我的代码:
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">
<!--
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>
MainActivity.java
public class MainActivity extends FragmentActivity {
/**
* 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;
private static DBManager dbManager;
private static SharedPreferences myPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbManager = new DBManager();
try {
DBManager.init(getApplicationContext());
} catch (SQLException e) {
Log.e("SQLException: ", e.toString());
}
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
Log.i("Page", "" + i);
// Fragment frag = mSectionsPagerAdapter.getPrimaryItem();
// View v = frag.getView();
// if(v != null)
// Log.i("mda", "haaa");
}
@Override
public void onPageScrollStateChanged(int i) {
Log.i("mda", "hu");
}
});
}
@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 {
private Fragment currentFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getPrimaryItem(){
return currentFragment;
}
@Override
public Fragment getItem(int position) {
// 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 5;
}
@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);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
}
return null;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
currentFragment = (Fragment)object;
}
}
/**
* 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;
}
}
}
fragment_main_dummy.xml 1 个选项卡 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>
问题:
我想要做的是在更改页面后收集 View 中的所有输入。当用户更改 ViewPage 并转到第二个选项卡时,我需要获取他在前一个选项卡中写入的所有信息。
我可以通过设置primaryItem来获取当前页面 fragment 的 View ,但无法获取上一个选项卡的 View 。怎么办?
提前致谢。
最佳答案
给你一些东西,
保存普通 View 的状态必须手动完成,唯一能够为您保存状态的 View 是 TextView 及其 subview 。因此,由于 EditText 是 TextView,因此这是可能的。
您想尝试的属性:
setFreezesText() 或 android:freezesText for xml。
(注释仅在 TextView 有 ID 时才有效)
此控制除了动态状态(例如光标位置)之外,该 TextView 在卡住为冰柱时是否保存其整个文本内容。默认情况下这是 false,不保存文本。如果 TextView 中的文本未保存在持久存储中的其他位置(例如内容提供程序中),则设置为 true,以便以后解冻 View 时用户不会丢失数据。
Android 文档引用: http://developer.android.com/reference/android/widget/TextView.html#setFreezesText%28boolean%29
您可以尝试的其他方法是更改 View 分页器缓存的 View 数限制。这样做可以使更多 View 保持 Activity 状态,而无需重新创建它们,因此它们内部的数据不会丢失。
您可以通过使用 View 寻呼机的以下方法来实现此目的:
setOffScreenPageLimit(int)
Android 文档引用: http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)
现在可以通过多种不同的方式保存所有选项卡中的所有数据,例如,您可以在 getItem 方法中使用 args Bundle 来存储第一个选项卡的数据并将其传递到第二个选项卡。
@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;
}
现在,虚拟 fragment 的新实例将使用键“key”存储 TextView 中的文本,您可以使用 fragment 的 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");
}
您还可以使用 SharedPreferences 来存储数据,然后在需要将其存储到数据库中时检索它。
这可能有点笼统,但它对共享首选项有一些很好的引用。
http://developer.android.com/guide/topics/data/data-storage.html
请告诉我这个答案是否有帮助,或者您需要更多帮助。
编辑:
好吧,我想我找到了一种可以实现您想要做的事情的方法,它有点黑客,但它会完成工作,直到找到更好的解决方案。如果有时间的话我可能会进一步研究一下。
但现在,您可以执行以下操作。
方法 onPageSelected(intposition) 是完成大部分工作的地方。
@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);
}
就像我说的,它非常hacky并且涉及一些检查条件,但它会起作用。当我有时间时,我会考虑寻找一种更优雅的方法来执行此操作,并将其发布回此线程。
祝你好运
关于java - ViewPager,滚动选项卡 + 滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252919/
这是在我的 MainActivity 中用作 BroadcastReceiver 的代码 mRegistrationBroadcastReceiver = new BroadcastRecei
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我想在大部分时间隐藏 UISearchBar,只在用户需要时调用它来显示。 我在 Interface Builder 中放置了一个 UISearchBar 并将其隐藏在 View 后面,当用户单击按钮
我有一个包含 CCMenuItemImage 的菜单(“myMenu”)。我希望此菜单能够检测手指滑动并相应地滑动。 我的问题是 CCMenuItemImage 似乎吸收了触摸事件。当用户触摸 CCM
我正在寻找一个简单的 jQuery 或 Javascript 解决方案,以使导航侧边栏在用户向下滚动页面时顺利跟随用户。像这里一样:http://ucon-acrobatics.com/shop/ 任
我有一个 ListView 控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目。我用 GestureRecognizer类来识别交叉滑动手势,但我还想通过水平移动选择的项目来为这个手势设置动画。
我想将 String 行标记化为标记(存储到 String 表中),并且我只能使用 java.io.*它是为了实现一个计算器。 例如:第一行:1+2+3第二行:1+ 2*3(标记之间有空格) 进入表{
我有一个 ListView 控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目。我用 GestureRecognizer类来识别交叉滑动手势,但我还想通过水平移动选择的项目来为这个手势设置动画。
我有一个导航栏,当单击菜单图标时,它将滑入“#secondary-nav”并隐藏“#primary-nav”。然而 jquery 似乎没有显示“#secondary-menu”。下面提供的是 HTMl
这个问题已经有答案了: how to make a sliding up panel like the Google Maps app? (2 个回答) 已关闭 7 年前。 我正在寻找类似的实现,如下
我有 ViewPager(Slide) 和 3 张图片。共有三个图像是通过 Internet 下载的。如果我将图片换到服务器上的另一台服务器上,链接保持不变,但应用程序中的图片没有改变,仍然是缓存中的
我在 gridview 中创建了两个按钮。 我想达到以下目的,但不知道应该用什么方法? 首先我触摸第一个按钮,将显示 toast 1 msg。通过将我的手指滑到第二个按钮而不抬起我的手指,将显示 to
所以我设置了一个小的 jquery 动画,用户将鼠标悬停在容器上一段时间,这会导致容器 split ,然后显示内部信息。 我不希望鼠标一进入容器就开始动画,所以我在动画上放了一个delay()。现在动
这个问题在这里已经有了答案: Simulate swipe with mouse in javascript (5 个答案) 关闭 7 年前。
我希望我的 Sprite 像在冰上一样滑动。因此,如果他在地面上,那么他可以正常行走,但当他接触冰时,他会滑动,直到有东西阻止他。有谁知道如何才能做到这一点?谢谢 最佳答案 像“Sprite Move
我的代码有几个问题:HTML: Bellevue
我正在尝试实现从 fragment1 过渡到 fragment2 的滑动动画。我正在使用 setCustomAnimations 方法。而且我知道我需要使用框架方法来替换 fragment 。 我的代
我不知道你们是否听说过 app chomp,但应用程序中有一个布局,如下图所示。我想知道他们是如何设置的,我将如何使用它来为我自己的应用程序制作类似的东西。有趣的是,当你滑动时,没有像水平 Scrol
我想检测用户何时在一个单元格占据整个屏幕宽度的 collectionView 中向左或向右滑动。是否可以不添加手势识别器。我试过添加手势识别器,但只有当我们将 collectionView 的 scr
我正在尝试开发一个应用程序来复制类似 tinder 的基于滑动的提要。该应用程序的想法与火种非常相似,也具有向右滑动和向左滑动匹配功能。 到目前为止我做了什么-我在 MongoDB 中创建了一个刷卡集
我是一名优秀的程序员,十分优秀!