在Fragments中设置动态内容时遇到问题

时间:2015-09-10 21:22:44

标签: android android-fragments textview

在我的MainActivity中,我有一个列表,当点击一个项目时,我会进入我的ScreenSlideActivity。这个活动会从另一个叫做ScreenSlidePageFragment的类中产生一个Fragment,当我滑动时会为我生成一些Fragment。我需要这些片段中的内容是唯一的,具体取决于我选择的列表中的任何项目。不幸的是,我没有成功。

我尝试在ScreenSlidePageFragment中创建一个set_all_data(string_data){}函数,并在创建Fragment之前调用它/更新textView,但我想我要么在新文本中遇到竞争条件,要么我只是做错了,不太了解片段。

ScreenSlideActivity

var size = typeface.GetGlyphOutline(charIndex, emSize, 0d).Bounds.Size;

ScreenSlidePageFragment

package com.example.dgzl.corvegas;

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) {
            String test_data = "Soem data";
            ScreenSlidePageFragment newFrag = new ScreenSlidePageFragment();
            newFrag.set_all_data(test_data);
            Fragment newFragment = newFrag.create(position);
            return newFragment;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }

    public void set_fragment_data(String biz_data[]){
        // title as business name
        setTitle(biz_data[1]);
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在getItem()方法中。您正在创建new ScreenSlidePageFragment,之后,您再次调用create()方法创建new ScreenSlidePageFragment。您需要以静态方式呼叫ScreenSlidePageFragment.create(position)

@Override
public Fragment getItem(int position) {
     String test_data = "Soem data";
     ScreenSlidePageFragment newFrag = ScreenSlidePageFragment.create(position);
     newFrag.set_all_data(test_data);
     return newFragment;
}