我的viewpager适配器在另一个片段中(即主片段中),即我在另一片段中使用了viewpager。所以viewpager适配器有2个片段。
我从主片段连续获取心率值,需要将其发送到视图页管理器适配器,然后查看页管理器适配器将此值发送到此处对文本视图进行分段和更新。
主片段是 i 初始化片段页面进一步用更新的心率值:-(读取数据)样本页面适配器).passdata(值);
使用接口更新值:-
public interface readingdata
{
void passdata(int value);
}
//适配器代码:-
public class SamplePagerAdapter extends FragmentStatePagerAdapter implements readingdata {
private final Random random = new Random();
private int mSize = 2;
private int heart_rate;
FragmentManager fm;
private Map<Integer, String> mFragmentTags;
public SamplePagerAdapter(FragmentActivity activity, FragmentManager supportFragmentManager, int heart) {
super(supportFragmentManager);
fm = supportFragmentManager;
mFragmentTags = new HashMap<Integer, String>();
}
@Override
public int getCount() {
return mSize;
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
if (position == 0) {
f = new MyFragment().newInstance(heart_rate);
} else if (position == 1) {
f = new SecondFragment();
}
return f;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object object = super.instantiateItem(container, position);
if (object instanceof Fragment) {
Fragment fragment = (Fragment) object;
String tag = fragment.getTag();
mFragmentTags.put(position, tag);
}
return object;
}
public Fragment getFragment(int position) {
Fragment fragment = null;
String tag = mFragmentTags.get(position);
if (tag != null) {
fragment = fm.findFragmentByTag(tag);
}
return fragment;
}
@Override
public void passdata(int value) {
heart_rate=value;
}
}
//片段代码被textview定期更新
public class MyFragment extends Fragment{
private int heart_rate;
private ArcProgress arc_progress;
private TextView tv_heartrate;
private Handler handler;
private Runnable runnable;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveBundle) {
view = inflater.inflate(R.layout.ecg_layout, container, false);
handler=new Handler();
arc_progress = (ArcProgress) view.findViewById(R.id.arc_progress);
tv_heartrate = (TextView) view.findViewById(R.id.tv_heart_rate);
handler=new Handler();
handler.post(runnable = new Runnable() {
@Override
public void run() {
MyFragment myFragment=new MyFragment();
arc_progress.setProgress(heart_rate);
tv_heartrate.setText(String.valueOf(heart_rate));
handler.postDelayed(this, 1000);
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public static Fragment newInstance(int heartvalue) {
MyFragment f = new MyFragment();
f.heart_rate = heartvalue;
return f;
}
}
那么我应该如何在片段内持续更新文本视图呢?
Take Help from this and save reference to view you are inflating
package com.mtg.workapp_v2.listing.wanted.add_wanted;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.mtg.classes.AddressView;
import com.mtg.classes.ListingVisibilityLayout;
import com.mtg.utils.CommonMethods;
import com.mtg.workapp.R;
public class WantedBasicInfoFragment extends Fragment implements View.OnClickListener{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
View FragmentView ;
Context myContext;
TextView text_continue;
EditText edit_title;
EditText edit_description;
private OnFragmentInteractionListener mListener;
//this is add by ahsan according to new design
ListingVisibilityLayout listingVisibilityLayout = null;
public WantedBasicInfoFragment() {
}
public static WantedBasicInfoFragment newInstance(String param1, String param2) {
WantedBasicInfoFragment fragment = new WantedBasicInfoFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentView= inflater.inflate(R.layout.fragment_wanted_basic, container, false);
init();
return FragmentView;
}
/********************************************************************************/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
/********************************************************************************/
public void setListener(OnFragmentInteractionListener listener) {
this.mListener = listener;
}
/********************************************************************************/
public void setInitValues()
{
}
/***********************************************************************************/
public void updateScreenData()
{
edit_title.setText(wantedInformation.csName);
edit_description.setText(wantedInformation.csDescription);
}
/***********************************************************************************/
public void init()
{
myContext=getActivity();
text_continue=(TextView) FragmentView.findViewById(R.id.text_continue);
edit_title = (EditText) FragmentView.findViewById(R.id.edit_title);
edit_description = (EditText) FragmentView.findViewById(R.id.edit_description);
text_continue.setOnClickListener(this);
edit_title.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
wantedInformation.csListingLanguage = CommonMethods.getInputLanguage(myContext);
}
@Override
public void afterTextChanged(Editable s) {
wantedInformation.csListingLanguage = CommonMethods.getInputLanguage(myContext);
}
});
initVisibilitySpinner();
}
/***********************************************************************************/
public void initVisibilitySpinner()
{
listingVisibilityLayout = new ListingVisibilityLayout(myContext);
View visibilityView = (View)FragmentView.findViewById(R.id.id_visibility_layout);
listingVisibilityLayout.ListingVisibilityInit(visibilityView);
}
/***********************************************************************************/
@Override
public void onClick(View view) {
int itemID = view.getId();
switch (itemID) {
case R.id.text_continue:
{
moveToNextScreen(true);
}
}
}
/***********************************************************************************/
public void moveToNextScreen(boolean isContinueClicked)
{
String csName = edit_title.getText().toString().trim();
String csDescription = edit_description.getText().toString().trim();
if(csName.length() <= 0 && csDescription.toString().length() <= 0)
{
CommonMethods.showMessageBox("", getResources().getString(R.string.id_please_enter_name_and_description), myContext);
return;
}
else if(csName.length() <= 0)
{
CommonMethods.showMessageBox("", getResources().getString(R.string.id_please_enter_name), myContext);
return;
}
else if(csDescription.length() <= 0)
{
CommonMethods.showMessageBox("", getResources().getString(R.string.id_please_enter_description), myContext);
return;
}
else if(listingVisibilityLayout.selectedProfileVisibility.csOptionID.equalsIgnoreCase("-1"))
{
CommonMethods.showMessageBox("", getResources().getString(R.string.id_select_visibility_option_msg), myContext);
return;
}
wantedInformation.csName = csName;
wantedInformation.csDescription = csDescription;
wantedInformation.visibilityOption = listingVisibilityLayout.selectedProfileVisibility;
//MH: If continue clicked then continue Button listener will be called
//MH: If only tick is clicked in Edit Mode then onUpdate listener will be called
//MH: and data will be updated before sent to api
//MH: The below statements are repeated in all fragments of Wanted
if(isContinueClicked)
mListener.continueButtonPressed(wantedInformation);
else
mListener.onUpdate(wantedInformation);
}
}
create Method
public void updateScreenData(String text)
{
tv_heartrate.setText(text);
}
in fragment and then call this method from activity
make sure that fragment reference is not null whenever call this method
在主片段中
private static HeartRateListener heartRateListener;
public static void setHeartRateListener(HeartRateListener listener){
heartRateListener = listener;
}
public static interface HeartRateListener{
void onHeartRate(int yourValue);
}
// Send your continuously updated value
heartRateListener.onHeartRate(yourValue);
在ViewPager片段中(在onViewCreated()内)
MainFragment.setHeartRateListener(new MainFragment.HeartRateListener() {
@Override
public void onHeartRate(int yourValue) {
// Update your textview with yourValue
}
});
我正在尝试在我的应用程序的三个主屏幕(提要、论坛)之间实现一个简单的滑动操作 在我的情况下,片段并不是同一类的所有实例,而是完全不同的。 M 问题存在于片段页面适配器中。我不确定如何返回 getItem 方法中的每个片段。 我已经尝试了以下内容,但它除了作为有效的返回语句之外没有,并且仍然期待一个: 这是我的一个片段的例子: 这是我的主要活动: 我还尝试定义一个变量电流片段,为每个当语句重新分配它
我有一个ViewPagerContainer片段,在应用程序启动时加载。ViewPagerContainer片段将两个选项卡(选项卡A和选项卡B)添加到操作栏。选项卡B有两个项目的列表视图。 我所做的:我在选项卡B片段中的列表视图项上附加了一个click listener,这样当用户单击一个项时,它会在第一个片段(即选项卡B下)内打开另一个片段(子片段)。 我陷入困境的地方:当用户按下后退按钮时,
我正在写一个使用片段的简单示例。在主体活动中,有两个片段。第一个片段有一些文本视图,第二个片段有一些按钮。当用户点击textview时,文本打开按钮将被更改。 activity_main.xml mainactivity.java contentFrag.java 我没有编写控制按钮的操作,只是生成onclick事件。但是,我弄错了。请帮助我修复错误,并继续完成我的任务。
下面你可以找到fragment类,我在那个片段中有一个RecycerView,当用户长时间按下RecycerView中的一个项目时,我试图打开一个上下文菜单。 我能够显示上下文菜单,并为一个片段的单个实例获取选定项的位置,当我滑动到下一个片段时,我得到了相同的索引位置,当我进一步调试时,我发现我得到了前一个片段上的RecycerView的相同适配器。当我记录RecycerView的适配器大小时,可
我试着在发帖前在里面搜索,但我不能理解如何刷新片段中托管的ViewPager的内容。你能帮帮我吗? 谢谢你抽出时间 } 这是pageviewer中的imageview的类
问题内容: 我搜索了许多看起来像这样的问题,但没有找到我的答案。 我有一个活动,该活动具有3个可通过操作栏访问的选项卡。我通过添加3个片段来实现这一点,这些片段使我自定义视图扩展了视图类。 在数据库更改的那一刻,我尝试通过调用invalidate()/ postinvalidate()刷新选项卡中的视图,但这不起作用。正如我考虑的其他许多选项一样,调用片段的onCreateView也是如此。 但是