如何在使用Views
时从我自己的方法访问layout
Fragments
?我在View
设置了Fragment
,如下所示:
public class LeftSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.left, container, false);
TextView whatever = (TextView) rootView.findViewById(R.id.whateverR);
return rootView;
}
}
如何访问此whatever TextView
字段,以便我可以使用自己的方法在那里写下一些内容?就像whatever.setText()
一样,但我无法使用它,因为我不知道如何从Fragment
之外引用它。
编辑:我的方法(它不在LeftSectionFragment
内!),我想引用TextView
的内容如下所示:
private void print(String msg) {
String s = msg;
inc = s.split(",");
incLength = inc.length;
//here refer to whatever TextView, similiary to whatever.setText(inc[0])
}
EDIT2:首次提出片段的主onCreate
方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
mViewPager.setCurrentItem(1);
}
FragmentPagerAdapter:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new LeftSectionFragment();
case 1:
return new MiddleSectionFragment();
case 2:
return new RightSectionFragment();
default:
return new MiddleSectionFragment();
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
int pos = position;
switch (pos) {
case 0:
return "Left";
case 1:
return "Middle";
case 2:
return "Right";
default:
return "Middle";
}
}
}
标签:
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
EDIT3:提出print
的地方:
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
print(readMessage);
break;
答案 0 :(得分:0)
我不知道我的问题是否正确,但我认为你想要这样的事情:
public class LeftSectionFragment extends Fragment {
private TextView mWhatever;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.left, container, false);
mWhatever = (TextView) rootView.findViewById(R.id.whateverR);
return rootView;
}
public void setWhateverText(String text) {
mWhateverText.setText(text);
}
}
在您的活动中,请按以下方式访问:
{
// init your Fragment
LeftSectionFragment leftFragment = LeftSectionFragment.initiate();
}
private void print(String msg) {
String s = msg;
inc = s.split(",");
incLength = inc.length;
//here refer to whatever TextView, similiary to whatever.setText(inc[0])
leftFragment.setWhateverText(incLength);
}
答案 1 :(得分:0)
答案是:
private void print(String msg) {
String s = msg;
inc = s.split(",");
incLength = inc.length;
LeftSectionFragment.set(inc[0]);
for(int i = 0; i < incLength; i++)
Log.i("taguś",inc[i]);
}
在这里,我们只是引用我们有方法的LeftSectionFragment
:
public static void set(String text) {
valuefixInd.setText(text);
}
这一切都很好。重要的是将我们的Fragment
类,TextView
字段和set
方法定义为静态:private static TextView whatever
&amp; public static class LeftSectionFragment extends Fragment
&amp; public static void set
。