如何使用 ListView 中的 OnItemClickListener 移动到 PagerStrip 滑动选项卡中的下一个片段
How to move to next fragment in PagerStrip Slidding tab with OnItemClickListener in ListView
我正在使用 android PagerStrip 滑动标签。我有两个片段。在第一个片段中,我有一个 listview.I 想要导航到第二个片段,当我单击第一个片段中的列表视图项时。我该怎么做呢?谢谢。
这是 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. 在 MainActivity 你可能已经有了
ViewPager 寻呼机;
在你的 Oncreate 方法中你也有
......
pager.setAdapter(adapter);
现在在 MainActivity below.ie(控制所有片段等的 Activity 中创建下面的方法)
public void setCurrentItem (int position, boolean smoothScroll) {
pager.setCurrentItem(item,true);
}
现在让我们假设您的 fragment1 有一个列表视图或 button.And 您想要通过 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
我正在使用 android PagerStrip 滑动标签。我有两个片段。在第一个片段中,我有一个 listview.I 想要导航到第二个片段,当我单击第一个片段中的列表视图项时。我该怎么做呢?谢谢。 这是 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. 在 MainActivity 你可能已经有了 ViewPager 寻呼机; 在你的 Oncreate 方法中你也有 ......
pager.setAdapter(adapter);
现在在 MainActivity below.ie(控制所有片段等的 Activity 中创建下面的方法)
public void setCurrentItem (int position, boolean smoothScroll) {
pager.setCurrentItem(item,true);
}
现在让我们假设您的 fragment1 有一个列表视图或 button.And 您想要通过 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