如何使用ListView中的OnItemClickListener移动到PagerStrip Slidding选项卡中的下一个片段

时间:2015-04-03 15:52:03

标签: android listview android-fragments

我正在使用android PagerStrip Slidding标签。我有两个片段。在第一个片段中,我有一个listview。我想导航到第二个片段,我点击第一个片段中的listview项。我该怎么做呢?谢谢。 这是Fragment1中的OnClick侦听器

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
     // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragmentcontainer,Fragment2.newInstance());
            transaction.addToBackStack(null);
        }


    });

//这是我的片段2,我想导航到

 public class Fragment2 extends Fragment {

  private static final String ARG_POSITION = "position";

   public static final String TAG = Fragment2.class
        .getSimpleName();

@InjectView(R.id.textView)
TextView textView;

private int position;

    public static Fragment2 newInstance(int position) {
    Fragment2 f = new Fragment2();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}
    public static Fragment2 newInstance(){
    Fragment2 f = new Fragment2();
    return f;
}

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    position = getArguments().getInt(ARG_POSITION);
   //setContentView(R.layout.activity_maps);
}

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
       View rootView =   inflater.inflate(R.layout.fragment_card,container,false);
    ButterKnife.inject(this, rootView);
    ViewCompat.setElevation(rootView, 50);
    textView.setText("CARD "+position);
    return rootView;
}

}

1 个答案:

答案 0 :(得分:0)

我已经想出了如何做到这一点。 1.在您可能已经拥有的MainActivity中              ViewPager寻呼机;            在你的Oncreate方法中你也有 ......

           pager.setAdapter(adapter);

现在在下面的MainActivity中创建以下方法。我(控制所有片段的Activity等)

     public void setCurrentItem (int position, boolean smoothScroll) {
      pager.setCurrentItem(item,true);

      }
  1. 现在让我们假设你的fragment1有一个列表视图或按钮。你想通过onClick导航到fragment2。 这就是你需要做的事情

            lv.setOnItemClickListener(new OnItemClickListener() {
            @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1,int   position, long arg3) { 
        ((MainActivity)getActivity()).setCurrentItem (1, true);
    

    } });

      //Remeber int position is the position of the fragment you want to navigate to in your Adapter