ListView项目Onclick片段 [英] ListView Item Onclick Fragment
本文介绍了ListView项目Onclick片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
早上好,我正在分段制作列表视图项目 onClick
.每次单击其他项目时,它都会显示相同的片段活动.我该怎么做才能使其与其他项目有所不同?我应该使用
Good Day, I'm making a listview item onClick
in fragment. Every time I click the other item it shows the same fragment activity. What should I do to to make it different from the other item? Should I use
是(postion == 0)
还是Case?
List1TW
public List1TW() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_list1_tw, container, false);
// Inflate the layout for this fragment
String[] awayStrings = {
"Chocolate Hills",
"Banaue Rice Terraces",
"Hinatuan Enchanted River",
"Mount Mayon",
"Puerto Princesa Underground River",
"Lake Sebu",
"Sumaguing Cave",
"Tinuy-an Falls",
};
ListView lv = (ListView) view.findViewById(R.id.listView2);
ArrayAdapter<String> lva = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, awayStrings);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TW1 fragment = new TW1();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
推荐答案
您可以使用 position
变量
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0){
TW1 fragment = new TW1();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
else if(){
}
//// or even switch
switch(position){
case 1:
// do your work
break;
}
}
});
这篇关于ListView项目Onclick片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文