我正在尝试从MainActivity
调用方法来更新片段中的TextView
。
但是,我在onCreate
中使用MainActivity
方法获得的片段的引用具有空控件(buttons
和textviews
)。
我认为这是因为Fragment在MainActivity
之后被初始化而引用了这个片段。如何使用初始化控件从MainActivity
引用此片段?
MainActivity:
public class MainActivity extends AppCompatActivity {
TabAdapter adapter;
Toolbar toolbar;
ViewPager pager;
SlidingTabLayout tabs;
StopwatchFragment stopwatchFragment;
CharSequence titles[] = {"Stopwatch","Countdown"};
int numOfTabs=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
adapter = new TabAdapter(getSupportFragmentManager(),numOfTabs,titles);
pager = (ViewPager)findViewById(R.id.view_pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout)findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setViewPager(pager);
stopwatchFragment = StopwatchFragment)adapter.getItem(TabAdapter.STOPWATCH_FRAGMENT_POSITION);
}
}
StopwatchFragment:
public class StopwatchFragment extends Fragment {
private static String TAG = "StopwatchFragment";
private TextView tv_elapsed_time;
private TextView tv_elapsed_time_millis;
private long elapsedTime;
private long startTime;
private static final int REFRESH_RATE = 60;
private boolean stopped = false;
private Button btn_lap;
private ListView lv_laps;
private LapsAdapter lapsAdapter;
private List<String> lapsList;
private Handler handler = new Handler();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stopwatch,container,false);
tv_elapsed_time=(TextView)view.findViewById(R.id.elapsed_time);
tv_elapsed_time_millis=(TextView)view.findViewById(R.id.elapsed_time_millis);
lv_laps = (ListView)view.findViewById(R.id.lv_Laps);
lapsList = new ArrayList<String>();
lapsAdapter = new LapsAdapter(getActivity(),R.layout.laps_row,lapsList);
lv_laps.setAdapter(lapsAdapter);
return view;
}
TabAdapter:
public class TabAdapter extends FragmentStatePagerAdapter {
public static final int STOPWATCH_FRAGMENT_POSITION = 0;
public static final int LAPS_FRAGMENT_POSITION = 1;
CharSequence titles[];
int numOfTabs;
public TabAdapter(FragmentManager fm,int numberOfTabs,CharSequence titles[]) {
super(fm);
this.numOfTabs =numberOfTabs;
this.titles = titles;
}
@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
StopwatchFragment stopwatchFragment = new StopwatchFragment();
return stopwatchFragment;
case 1:
CountdownFragment countdownFragment = new CountdownFragment();
return countdownFragment;
default:return null;
}
}
@Override
public int getCount() {
return numOfTabs;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
StopwatchFragment.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:gravity="center_horizontal"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/elapsed_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:text="00 00"
android:textColor="@color/colorAccent"
android:textSize="112sp" />
<TextView
android:id="@+id/elapsed_time_millis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/colorAccent"
android:textSize="24sp" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="360dp"
android:layout_height="360dp"
android:max="500"
android:progress="0"
android:progressDrawable="@drawable/circular" />
</RelativeLayout>
<ListView
android:id="@+id/lv_Laps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp" />
答案 0 :(得分:0)
在StopwatchFragment
中,您需要将初始化控件的代码移至onActivityCreated
方法而不是onCreateView
。我在下面的代码中做了这些更改:
public class StopwatchFragment extends Fragment {
private static String TAG = "StopwatchFragment";
private Button btn_start,btn_stop;
private TextView tv_elapsed_time;
private TextView tv_elapsed_time_millis;
private long elapsedTime;
private long startTime;
private static final int REFRESH_RATE = 60;
private boolean stopped = false;
private Button btn_lap;
private ListView lv_laps;
private LapsAdapter lapsAdapter;
private List<String> lapsList;
private Handler handler = new Handler();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stopwatch,container,false);
tv_elapsed_time=(TextView)view.findViewById(R.id.elapsed_time);
tv_elapsed_time_millis= (TextView)view.findViewById(R.id.elapsed_time_millis);
lv_laps = (ListView)view.findViewById(R.id.lv_Laps);
return view;
}
/**
* This method is called after the onCreateView() method when the host activity is created.
* Activity and fragment instance have been created as well as the view hierarchy of the activity.
* At this point, view can be accessed with the findViewById() method.
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lapsList = new ArrayList<String>();
lapsAdapter = new LapsAdapter(getActivity(),R.layout.laps_row,lapsList);
lv_laps.setAdapter(lapsAdapter);
Log.d(TAG,"Controls initialized");
Log.d(TAG, "onCreateView");
}
请相应更新您的StopwatchFragment
,试一试,如果有帮助请告诉我。
答案 1 :(得分:0)
TabAdapter出现了问题。需要更改构造函数:
public TabAdapter(FragmentManager fm,int numberOfTabs,CharSequence titles[]) {
super(fm);
this.numOfTabs = numberOfTabs;
this.titles = titles;
stopwatchFragment = new StopwatchFragment();
countdownFragment = new CountdownFragment();
}
方法getItem():
@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return stopwatchFragment;
case 1:
return countdownFragment;
default:return null;
}
}