Android 如何在ViewPager片段之间共享数据?
我刚开始android编程,所以我的代码看起来很难看。它是从各种教程拼凑而成的,几乎满足了我的需要。快速解释我需要它做什么: 我有一个带有两个选项卡的活动。一个选项卡在整个屏幕上实现OnClickListener,它在每次单击屏幕时递增一个计数器。如果屏幕从上到下或从下到上滑动,计数器将重置为0。现在,我一直在努力解决的问题是:当我滑动到下一个选项卡时,我希望该计数器的当前值被传递到下一个片段,在那里它将用于计算 有人能帮我吗 我也很乐意接受任何有助于我清理当前代码的评论 干杯 这是我目前的代码: 主要活动的xml:Android 如何在ViewPager片段之间共享数据?,android,Android,我刚开始android编程,所以我的代码看起来很难看。它是从各种教程拼凑而成的,几乎满足了我的需要。快速解释我需要它做什么: 我有一个带有两个选项卡的活动。一个选项卡在整个屏幕上实现OnClickListener,它在每次单击屏幕时递增一个计数器。如果屏幕从上到下或从下到上滑动,计数器将重置为0。现在,我一直在努力解决的问题是:当我滑动到下一个选项卡时,我希望该计数器的当前值被传递到下一个片段,在那里它将用于计算 有人能帮我吗 我也很乐意接受任何有助于我清理当前代码的评论 干杯 这是我目前的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<de.regenistdoof.sporecounter.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
计数器片段的xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the fragment to be displayed for a section associated with a tab -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<TextView
android:id="@+id/counter"
android:text="@string/counter_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textSize="50sp" />
</LinearLayout>
编辑:
以下是使解决方案适用于我的代码的重要部分:
其思想是:FragmentCounter增加计数器-->设置MainActivity-->MainActivity调用FragmentCalculations-->FragmentCalculations更新文本视图中的updateCall方法中的值
主要活动:
package de.regenistdoof.sporecounter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
private FragmentCalculations fragcalc;
// get latest count from FragmentCounter and call updateCall method in FragmentCalculations
public void setLastCount(int count) {
((FragmentCalculations)fragcalc).updateCall(count);
}
碎片计数器:
这段代码进入我用来增加计数器的按钮的onclicklistener:
((MainActivity)getActivity()).setLastCount(count);
碎片计算:
public class FragmentCalculations extends Fragment{
public static EditText numberOfSquaresRef, countedCellsRef;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calculation, container, false);
countedCellsRef = (EditText) view.findViewById(R.id.counted_cells);
public void updateCall(int count){
Log.d(TAG, "fragcalc value: " + count);
countedCellsRef.setText(""+count);
}
您可以将最后一个值存储在MainActivtiy中 例如: 在主要活动中:
package de.regenistdoof.sporecounter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
private FragmentCalculations fragcalc;
// get latest count from FragmentCounter and call updateCall method in FragmentCalculations
public void setLastCount(int count) {
((FragmentCalculations)fragcalc).updateCall(count);
}
private int lastCount;
public int getLastCount() {
return this.lastCount;
}
public void setLastCount(int newValue) {
this.lastCount = newValue;
}
在你的片段中:
//Set the value:
((MainActivity) getActivity()).setLastCount(999);
//Get the value:
int lastCount = ((MainActivity) getActivity()).getLastCount();
这将为您迁移到StackOverflow,因为它与编程相关。在发布问题之前,请先阅读每个StackExchange站点的范围。啊,对不起,我登录了StackOverflow,但在我的个人资料中玩的时候,我就到了这里。我可以自己移动它吗,或者我应该关闭它并在那里打开一个新问题吗?不要重复。我已经投票决定移动它,但是如果你想,你可以标记它并为mods写一个简短的说明。我标记了它,谢谢你的提示。管理活动中的计数器,并将片段注册为从活动中更新的侦听器。谢谢,伙计,这样我就可以将我的值转移到另一个片段。但现在的问题是,我需要调用某种操作,比如在片段中单击按钮,以从mainActivity获取值。我希望在我滑动到片段时,或者当我增加另一个片段中的计数器时,值会自动更新。我知道这可能是不可能的,因为在启动应用程序时,viewpager会填充这两个片段,以使其可切换。谢谢你更好地解释了我想做什么:!单击片段1会增加计数器,然后我想滑动到片段2。“newValue”将自动从mainActivity中提取最新的计数器值,然后单击“按钮”将使用此最新的“newValue”执行计算。为此,您可以使用一个界面(您可以找到更多关于此的信息)或展开当前方法。例如,将以下内容添加到片段:
public void updateCall(int newValue){//Update data…}
并将其添加到活动中的方法:public void setLastCount(int newValue){this.lastCount=newValue;((DummyFragment)示例)。updateCall(newValue);}
Thansk。我以前用一个接口试过,也得到了类似的结果。我可以将消息传递给活动,但无法将其转发到片段,因为片段位于viewpager中,因此我无法通过“findFragmentByID”获取它们。我稍后再试试你的其他建议。很幸运,我无法实现您的解决方案。我不知道如何用我的值正确地替换((DummyFragment)示例).updateCall(newValue)
行。我有这样的((FragmentCalculations)).updateCall(newValue)代码>,但Android Studio在“FragmentCalculations”位加了下划线,并表示“Expression expected”。