[英]Change viewpager fragment by a ImageView Click
I'm trying to change the viewpager fragment by clicking on a button. 我试图通过单击一个按钮来更改viewpager片段。 I have 2 fragments(FragmentA,FragementB), each fragment has it's own xml file (fragement_a.xml, fragement_b.xml, and so on). 我有2个片段(FragmentA,FragementB),每个片段都有它自己的xml文件(fragement_a.xml,fragement_b.xml等)。 i call ImageView object in FragementA and when i press that imageView object i want to load FragmentB. 我在FragementA中调用ImageView对象,当我按下该imageView对象时,我想加载FragmentB。 please help me to do it. 请帮我做。
please find below code i used. 请在下面找到我使用的代码。
..\\swipetabs\\src\\com\\example\\swipetabs\\MainActivity.java .. \\ 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 .. \\ swipetabs的\\ src \\ COM \\标签\\ 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\\tabs\\FragmentB.java .. \\ 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 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 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 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>
Create one method in your MainActivity.java 在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
}
How to call the method from Fragment? 如何从Fragment调用方法?
public class FragmentA extends Fragment {
/// Your Code
public void onClick(View v) {
((MainActivity)getActivity()).switchToFragmentB();
}
}
UPDATE: 更新:
In onCreateView()
method Change your return statement to 在onCreateView()
方法中,将return语句更改为
return v;
(True) (真正)
return inflater.inflate(R.layout.fragment_a, container, false);
(Wrong) (错误)
You can try something like this: 您可以尝试如下操作:
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;
}
} }
//fragment class //片段类
public class TopRatedFragment extends Fragment { 公共类TopRatedFragment扩展Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag1,
container, false);
return rootView;
}
} }
Try this code, first make a Inteface - 尝试使用此代码,首先制作一个接口-
public interface SwitchToFragmentB {
public void switchToFragmentB();
} }
Now implement this interface to your main Activity- 现在,将这个接口实现到您的主要Activity-
public class MyActivity extends FragmentActivity implements SwitchToFragmentB {
@Override
public void switchToFragmentB() {
viewPager.setCurrentItem(1);
}
} }
Now in your fragment - 现在在您的片段中-
public class FragmentA extends Fragment implements View.OnClickListener {
@Override
public void onClick(View view) {
SwitchToFragmentB switchToB = (SwitchToFragmentB) getActivity();
switchToB.switchToFragmentB();
}
} }
Hope this will Help - 希望这会有所帮助-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.