通过ImageView单击更改viewpager片段

时间:2014-02-03 06:20:37

标签: android android-fragments

我正在尝试通过单击按钮来更改viewpager片段。我有2个片段(FragmentA,FragementB),每个片段都有自己的xml文件(fragement_a.xml,fragement_b.xml等)。我在FragementA中调用ImageView对象,当我按下该imageView对象时,我想加载FragmentB。请帮我做。

请找到我使用的以下代码。

.. \ swipetabs \ SRC \ COM \示例\ swipetabs \ MainActivity.java

   package com.example.swipetabs;

import com.tabs.*;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity implements TabListener {

    ActionBar action_bar;
    ViewPager viewPager;
    ImageView creditCards;
    Fragment fragement;

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

        viewPager=(ViewPager)findViewById(R.id.pager);

        viewPager.setAdapter(new MyAdoptor(getSupportFragmentManager()));

        action_bar=getActionBar();
        //action_bar.setBackgroundDrawable(d)
        action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1=action_bar.newTab();
        tab1.setText("Login");

        tab1.setTabListener(this);

        ActionBar.Tab tab2=action_bar.newTab();
        tab2.setText("Compare Now");
        tab2.setTabListener(this);

        ActionBar.Tab tab3=action_bar.newTab();
        tab3.setText("Search");
        tab3.setTabListener(this);

        action_bar.addTab(tab1);
        action_bar.addTab(tab2);
        action_bar.addTab(tab3);


    }

    public void switchToFragmentB(){
        viewPager.setCurrentItem(1);
     }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


}


class MyAdoptor extends FragmentPagerAdapter
{

    public MyAdoptor(FragmentManager fm) {
        super(fm);
                // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub

        Fragment fragement=null;
        if(arg0==0)
        {
            fragement=new FragmentA();
        }
        if(arg0==1)
        {
            fragement=new FragmentB();
        }

        System.out.print("<><><><>");
        return fragement;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

.. \ swipetabs \ src \ com \ tabs \ FragmentA.java

 import com.example.swipetabs.R;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import com.example.swipetabs.*;

    /**
     * A simple {@link android.support.v4.app.Fragment} subclass.
     * 
     */
    public class FragmentA extends Fragment implements OnClickListener{

        public FragmentA() {

            // Required empty public constructor

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment

            View v = inflater.inflate(R.layout.fragment_a, container, false);
            ImageView btnT = (ImageView)v.findViewById(R.id.imageView1);
            btnT.setOnClickListener(this);


            return inflater.inflate(R.layout.fragment_a, container, false);


        }

        public void onClick(View v) {
             /*When I click this button in my fragment, I'd like it to go to fragment B for example*/
            ((MainActivity)getActivity()).switchToFragmentB();


        }

.. \ swipetabs \ SRC \ COM \翼片\ FragmentB.java

package com.tabs;

import com.example.swipetabs.R;
import com.example.swipetabs.R.layout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragmentB extends Fragment {

    public FragmentB() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_b, container, false);
    }

}

fragement_a.xml

<FrameLayout 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:background="#FFCC00"
    tools:context=".FragmentA" >

    <!-- TODO: Update blank fragment layout -->

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="205dp"
        android:src="@drawable/ic_launcher" />

</FrameLayout>

fragement_b.xml

<FrameLayout 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"
    tools:context=".FragmentB" >

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

AndroidManifestFile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.swipetabs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.swipetabs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:4)

在MainActivity.java中创建一个方法

public class MainActivity extends FragmentActivity implements TabListener {

       ///Your Code

       // Call This method From your Fragment's Button click.
       public void switchToFragmentB(){
          viewPager.setCurrentItem(1);
       }
       ///Your Code 
 }

如何从Fragment调用方法?

public class FragmentA extends Fragment {

      /// Your Code

    public void onClick(View v) {

      ((MainActivity)getActivity()).switchToFragmentB();

    }


}

<强>更新

onCreateView()方法中将return语句更改为

return v;(真)

return inflater.inflate(R.layout.fragment_a, container, false);(错误)

答案 1 :(得分:0)

您可以尝试这样的事情:

public class TabPageAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment();
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

//片段类

公共类TopRatedFragment扩展了Fragment {

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

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

    return rootView;
}

}

答案 2 :(得分:0)

尝试此代码,首先制作一个Inteface -

public interface SwitchToFragmentB {
public void switchToFragmentB();

}

现在将此接口实现到您的主Activity -

public class MyActivity extends FragmentActivity implements SwitchToFragmentB {

@Override
public void switchToFragmentB() {
    viewPager.setCurrentItem(1);
}

}

现在在你的片段中 -

public class FragmentA extends Fragment implements View.OnClickListener {

@Override
public void onClick(View view) {
    SwitchToFragmentB switchToB = (SwitchToFragmentB) getActivity();
    switchToB.switchToFragmentB();
}

}

希望这会有所帮助 -