1
我正在使用ViewPager和PageAdapter處理片段,並且想要根據單擊哪個父節點動態地向片段添加內容。在MainActivity發送數據,這個活動收到這樣的:無法將文本添加到TextView
intent.putExtra("biz_data", biz_data);
,
intent.getStringArrayExtra("biz_data");
但是我無法編輯的片段佈局的內容。你可以在我試圖做這個活動的最終看到的,但它給我一個錯誤:
TextView tv = (TextView) findViewById(R.id.today_frag_tv);
// tv.setEnabled(true); // gives error
// tv.append("new stuff"); // gives error
// tv.setText("MY Custom Stuff"); // gives error`
全部活動代碼
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
/**
* Demonstrates a "screen-slide" animation using a {@link ViewPager}. Because {@link ViewPager}
* automatically plays such an animation when calling {@link ViewPager#setCurrentItem(int)}, there
* isn't any animation-specific code in this sample.
*
* <p>This sample shows a "next" button that advances the user to the next step in a wizard,
* animating the current screen out (to the left) and the next screen in (from the right). The
* reverse animation is played when the user presses the "previous" button.</p>
*
* @see ScreenSlidePageFragment
*/
public class ScreenSlideActivity extends android.support.v4.app.FragmentActivity {
/* The number of pages (wizard steps) to show in this demo.*/
private static final int NUM_PAGES = 3;
/* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.*/
private ViewPager mPager;
/* The pager adapter, which provides the pages to the view pager widget.*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
final String biz_data[] = in.getStringArrayExtra("biz_data");
set_fragment_data(biz_data);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
public void onFragClick (View view){
Toast.makeText(ScreenSlideActivity.this, "button", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_screen_slide, menu);
// enable 'prev' and 'next' when not on first node
menu.findItem(R.id.action_prev).setEnabled(mPager.getCurrentItem() > 0);
menu.findItem(R.id.action_next).setEnabled(mPager.getCurrentItem() < (NUM_PAGES-1));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 16908332:
// Navigate "up" the demo structure to the launchpad activity.
// See http://developer.android.com/design/patterns/navigation.html for more.
finish();
return true;
case R.id.action_prev:
// Go to the previous step in the wizard. If there is no previous step,
// setCurrentItem will do nothing.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
return true;
case R.id.action_next:
// Advance to the next step in the wizard. If there is no next step, setCurrentItem
// will do nothing.
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
public void set_fragment_data(String biz_data[]){
// title as business name
setTitle(biz_data[1]);
TextView tv = (TextView) findViewById(R.id.today_frag_tv);
// tv.setEnabled(true);
// tv.append("new stuff");
// tv.setText("MY Custom Stuff");
}
}
更改片段內容的代碼應放在片段。 –
我必須做錯了,因爲我仍然遇到錯誤。當將佈局設置爲片段時,我這樣做:'rootView =(ViewGroup)inflater.inflate(R.layout.fragment_business,container,false);'。不過,我無法向其添加內容。另外,我似乎不能使用函數findviewById(),說「無法解析方法」。 – wndg
你試過在片段的視圖上使用'findViewById()'嗎?你的情況,像'rootView.findViewById(R.id.today_frag_tv)'? –