11

這裏是我的代碼:刷卡用一個按鈕下一個片段 - 單擊

FirstView.java

import android.content.Intent; 
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.Button; 
import android.widget.TextView; 

public class FirstView extends Fragment 
{ 
    private TextView firstText; 
    private Button btn; 

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


     View view = inflater.inflate(R.layout.fragment_view1,container,false); 


     firstText = (TextView)view.findViewById(R.id.viewOneText); 
     btn = (Button)view.findViewById(R.id.viewOneBtn); 

     btn.setOnClickListener(new ButtonEvent()); 
     return view; 

    } 

    private class ButtonEvent implements OnClickListener 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      new SecondView(); 

     } 

    } 
} 

SecondView.java

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.Button; 
import android.widget.TextView; 

public class SecondView extends Fragment 
{ 
    private TextView secondText; 
    private Button secondViewBtn; 

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


     View view = inflater.inflate(R.layout.fragment_view2,container,false); 


     secondText = (TextView)view.findViewById(R.id.secondViewText); 
     secondViewBtn = (Button)view.findViewById(R.id.secondViewBtn); 

     secondViewBtn.setOnClickListener(new ButtonEvent()); 
     return view; 

    } 

    private class ButtonEvent implements OnClickListener 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      secondText.setText("Second View Text changed"); 

     } 

    } 
} 

MainActivity.java

import android.os.Bundle; 
import android.app.Activity; 
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.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.Menu; 

public class MainActivity extends FragmentActivity { 

    private ViewPager viewPager; 
    private MyAdapter pageAdapter; 
    private static final int ITEMS = 2; 

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

     viewPager = (ViewPager)findViewById(R.id.pager); 
     pageAdapter = new MyAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(pageAdapter); 

    } 

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

    public static class MyAdapter extends FragmentPagerAdapter { 


     public MyAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

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

     @Override 
     public Fragment getItem(int position) { 
      if(position==0) 
      { 
       return new FirstView(); 
      } 
      else 
      { 
       return new SecondView(); 
      } 
     } 
    } 

} 

在這段代碼中,當我點擊FirstView中的Button時,我需要移動到SecondView。我嘗試了Intents,但猜測它是錯誤的。目前,由於ViewPager的原因,此功能具有「滑動」功能,因此當點擊該按鈕時,需要使用相同的滑動功能移動到SecondView。

回答

28

只想補充一點,移動片段到您的活動的新方法:

public void setCurrentItem (int item, boolean smoothScroll) { 
    viewPager.setCurrentItem(item, smoothScroll); 
} 

,並從一個按鈕監聽器調用它:

((MainActivity)getActivity()).setCurrentItem (1, true); 
+0

感謝您的答覆。這種方法來到'活動'或'片段'?我的意思是'MainActivity.java'或'FirstView.java'? –

+0

'MainActivity.java' –

+0

是的,太好了。謝謝 –

2

您可以在活動定義了以下功能:

public void next_fragment(View view) { 
    viewPager.setCurrentItem(viewPager.getCurrentItem()+1); 
} 

public void previous_fragment(View view) { 
    viewPager.setCurrentItem(viewPager.getCurrentItem()-1); 
} 

然後從您的佈局調用它們爲:

android:onClick="next_fragment" for the "NEXT" button 

android:onClick="previous_fragment" for the "PREVIOUS" button 
相關問題