我有一个项目,我想要做的。我遇到了一个小问题,我不知道如何解决它。下面是应用程序的图像为止。
我想它做的是,当用户onclicks一个列表项,部分它说你好!这是Fragment2的转变,而我在应用程序中声明一个新的XML。所以,如果我点击ATO的listItem,然后在右侧的片段应更改为类似你好!这是ATO片段
下面是我的code到目前为止:
AndroidListFragmentActivity:
包com.exercise.AndroidListFragment;
进口android.app.Activity;
进口android.os.Bundle;
公共类AndroidListFragmentActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
}
Fragment2:
包com.exercise.AndroidListFragment;
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类Fragment2扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
// TODO自动生成方法存根
返回inflater.inflate(R.layout.fragment2,集装箱,假);
}
}
MyListFragment1:
包com.exercise.AndroidListFragment;
进口android.app.ListFragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.Toast;
公共类MyListFragment1扩展ListFragment {
的String []选项= {
ATO
BETA,
DELT
PHI DELT
SAE
SIG NU,
斐济,
SIG CHI,
PHI PSI
};
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
ListAdapter myListAdapter =新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,期权);
setListAdapter(myListAdapter);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.listfragment1,集装箱,假);
}
@覆盖
公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
// TODO自动生成方法存根
Toast.makeText(getActivity(),getListView()getItemAtPosition(位置)的ToString(),Toast.LENGTH_LONG。).show();
}
}
Fragment2.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / fragment2text
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文本=您好!这是Fragment2/>
< / LinearLayout中>
listfragment1.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:以下属性来=8DP
机器人:paddingRight =8DP>
< ListView的机器人:ID =@ ID / Android的:清单
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:drawSelectorOnTop =FALSE/>
< TextView的机器人:ID =@ ID /安卓:空
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:文本=无数据/>
< / LinearLayout中>
main.xml中
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
<片段
机器人:名称=com.exercise.AndroidListFragment.MyListFragment1
机器人:ID =@ + ID /片段1
机器人:layout_weight =1
机器人:layout_width =0px
机器人:layout_height =match_parent/>
<片段
机器人:名称=com.exercise.AndroidListFragment.Fragment2
机器人:ID =@ + ID / fragment2
机器人:layout_weight =2
机器人:layout_width =0px
机器人:layout_height =match_parent/>
< / LinearLayout中>
解决方案
不知道什么是最小的修复,让您的code的工作,但你看使用的的