【发布时间】:2016-07-09 23:35:00
【问题描述】:
我相信答案是肯定的..不确定 FragmentManager 是否适用于此..因为一个片段包含 ListView 而另一个片段包含不同的小部件(ImageView、Text)
在我的主要活动中,我使用以下方法调用我的第一个片段:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LFragment lFragment= (LFragment)getSupportFragmentManager().findFragmentByTag("lFragment");
if(lFragment==null){
lFragment= new LFragment();
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,lFragment,"lFragment");
transaction.commit();
}
好的,一旦我进入我的 LFragment(1st Fragment) 类,我就会使用此方法转到下一个 Fragment:
//handling item click
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewGroup=(ViewGroup)view;
TextView txt=(TextView)viewGroup.findViewById(R.id.txtitem);
int itemPosition = position;
//stores the text of the clicked item in the list
String value = (String) l.getItemAtPosition(position);
//using intent to get into infoFragment
Intent intent = new Intent(view.getContext(),infoFragment.class);
//store string value into intent
intent.putExtra(value,itemPosition);
view.getContext().startActivity(intent);
//kills the fragment
getActivity().finish();
这是我尝试使用的(第二个片段)..但是 getIntent 不起作用
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.info_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
ImageView image = (ImageView) view.findViewById(R.id.imageView);
RatingBar rating=(RatingBar) view.findViewById(R.id.ratingBar);
TextView degrees=(TextView)view.findViewById(R.id.textView);
//ERROR "can not resolve method getIntent"
Intent intent = getIntent();
}
【问题讨论】:
-
我建议使用接口并通过那里传递您的数据,以便活动处理片段替换。
-
你能不能有一个参考资料让我看看?这是否也意味着我必须更改在 Main Activity 中调用第一个片段的方式或仅更改 onClickList 方法?
-
@Raykud 谢谢我明白你的意思了
-
您解决了问题吗?如果解决了,请关闭问题。如果没有,请告诉我们以获得进一步的帮助。
-
@Kalininskaya Raykud 回答帮助..使用接口而不是下面提供的答案
标签: java android-studio android-fragments