SwipeView更改了2页的内容

时间:2015-08-05 10:29:01

标签: java android

我使用滑动视图创建了一个新活动,并添加了一些代码。

我现在有两个可滑动的页面,但是如果内容相同,我该如何更改第二个滑动页面的内容。

我是否必须创建新活动或仅将其重定向到另一个xml?

import java.util.Locale;

import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


public class ListItemClicked extends ActionBarActivity {

static Bundle extras;

SectionsPagerAdapter mSectionsPagerAdapter;
static ImageLoader imageLoader;
static DisplayImageOptions options;




ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_item_clicked);


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    extras = getIntent().getExtras();

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    //Setup the ImageLoader, we'll use this to display our images
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    //Setup options for ImageLoader so it will handle caching for us.
    options = new DisplayImageOptions.Builder()
            .cacheInMemory()
            .cacheOnDisc()
            .build();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    return id == R.id.action_settings || super.onOptionsItemSelected(item);

}



public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section4).toUpperCase(l);
            case 1:
                return getString(R.string.title_section5).toUpperCase(l);
        }
        return null;
    }
}


public static class PlaceholderFragment extends Fragment {


    private static final String ARG_SECTION_NUMBER = "section_number";


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        View rootView = inflater.inflate(R.layout.fragment_list_item_clicked, container, false);


        TextView pDate = (TextView) rootView.findViewById(R.id.textView);
        pDate.setText( extras.getString("pdate") );


        TextView ptitle = (TextView) rootView.findViewById(R.id.section_label);
        ptitle.setText(extras.getString("pname"));


        TextView pnText = (TextView) rootView.findViewById(R.id.textView2);
        pnText.setText( extras.getString("pText"));




        //Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
        ImageLoadingListener listener = new ImageLoadingListener(){



            @Override
            public void onLoadingStarted(String arg0, View arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLoadingCancelled(String arg0, View arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
                // i/ndicator.setVisibility(View.INVISIBLE);
               // iconImg.setVisibility(View.VISIBLE);
            }
            @Override
            public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                // TODO Auto-generated method stub

            }

        };

        //Load the image and use our options so caching is handled.
        final ImageView iconImg = (ImageView) rootView.findViewById(R.id.imageView);
        imageLoader.displayImage( extras.getString("pImage"), iconImg, options, listener);



        return rootView;
    }
}

}

谢谢!

3 个答案:

答案 0 :(得分:1)

titleForRow课程中,您可以从didSelectRow方法获取当前页面位置。之后,您可以根据页面部分编号更改您的片段内容

答案 1 :(得分:1)

有很多方法可以做到这一点。您不必创建另一个Activity

下面是一种简单的方法,它会重写您的PlaceholderFragment并使其在onCreateView上的不同位置返回不同的视图。

public static class PlaceholderFragment extends Fragment {
    private int position;

    public static PlaceholderFragment newInstance(int sectionNumber) {
        return new PlaceholderFragment(sectionNumber);
    }

    public PlaceholderFragment(int sectionNumber) {
        this.position = sectionNumber;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {
        View rootView;
        switch(position){
            case 1:
            rootView = inflater.inflate(..);
            //init view widget.
            break;

            case 2:
            rootView = inflater.inflate(..);
            //init view widget.
            break;
        }
        return rootView;
    }
}

答案 2 :(得分:1)

根据您从Placeholder Fragment获得的页码,您可以设置新内容