我在片段视图中管理onItemClickListener
时遇到了一些困难。当我按下列表中的项目时,没有任何反应。请检查我的代码,如果我做得对,或者我的代码有什么问题,请告诉我。
public class People extends BaseFragment implements OnClickListener {
static String name;
ListView listview;
TextView et;
static public List<SuccessStoreyItem> success_list3 = new ArrayList<SuccessStoreyItem>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.home,null);
ParserResult pr = new ParserResult();
Button b=(Button) view.findViewById(R.id.home1);
listview = (ListView)view.findViewById(R.id.lv_home);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "ullu ka patha", Toast.LENGTH_LONG).show();
/*Profile frg3 = new Profile();
Bundle b3 = new Bundle();
String username = success_list3.get(position).getStorey_id();
b3.putString("username", username);
frg3.setArguments(b3);
((MainActivity)getActivity()).launchNewFragment(frg3, R.id.tab2);
Toast.makeText(getActivity(), "Name :"+name ,1).show();*/
}
});
et=(TextView) view.findViewById(R.id.editText1);
b.setOnClickListener(this);
if(ParserResult.success_list2.size() != 0){
success_list3 = ParserResult.success_list2;
}
listview.setAdapter(new Adapters(success_list3 ,MainActivity.mainactivity_context));
ParserResult.success_list2 = new ArrayList<SuccessStoreyItem>();
return view;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
setTitle(R.string.Home);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
name=et.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Name", name);
Toast.makeText(getActivity(), "Name :"+name ,1).show();
//((MainActivity)getActivity()).launchNewFragment(new Home2(),R.id.tab1);
}
}
更新:添加了列表视图结构
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/view_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/view_tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view_imageview"
android:layout_alignLeft="@+id/view_tv_age"
android:layout_marginBottom="5sp"
android:text="TextView" />
<TextView
android:id="@+id/view_tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view_tv_username"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/view_imageview"
android:text="TextView" />
<ImageButton
android:id="@+id/ib_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="29dp"
android:layout_marginTop="18dp"
android:background="#fff"
android:src="@drawable/chevron" />
</RelativeLayout>
FRAGMENT LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="10sp" >
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="sp"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="New Machtes"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/iconnew"
android:textColor="#ffffff"
android:text="16" />
</RelativeLayout>
<ListView
android:id="@+id/lv_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout1" >
</ListView>
</RelativeLayout>
答案 0 :(得分:3)
将android:descendantFocusability="blocksDescendants"
添加到ListView项目布局中的<RelativeLayout..
。
查看listview项目布局,我看到你有一个ImageButton
。碰巧ImageButton
从其所在的视图组(列表行)窃取焦点。这意味着当您单击列表行时,它始终是ImageButton
捕获事件。
通过添加android:descendantFocusability="blocksDescendants"
,我们阻止列表行后代窃取可聚焦性。
您可以详细了解here。
答案 1 :(得分:0)
我认为你的listview.setadapter()行和与之关联的数据应该在你将onclickitemlistener()设置为listview之前出现..