本文介绍了Onclicklistner 在片段列表视图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 listfragment 中有一个带有自定义适配器的列表视图,并且还为列表视图设置了 onclicklistner.但是 Onclicklistner 不起作用.
I have a listview with custom adapter in listfragment and also set onclicklistner for listview. But Onclicklistner does not work.
这是我的代码:
public class BasicFragment extends ListFragment {
ListView lv;
MyCustomAdapter adapter;
@Override
public void onCreate(Bundle si) {
super.onCreate(si);
}
@Override
public void onActivityCreated(Bundle b) {
super.onActivityCreated(b);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_basic, container, false);
lv = (ListView) view.findViewById(android.R.id.list);
FetchedData DT = FetchedData.StaticDataTransfer();
RecepiesProperties[] AryObjaz = DT.getData();
getdata(AryObjaz);
adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout,
Dataset);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast t = Toast.makeText(getActivity(), "Message",
Toast.LENGTH_SHORT);
t.show();
}
});
return view;
}}
MyCustomAdapter.java
MyCustomAdapter.java
public class MyCustomAdapter extends ArrayAdapter<Recipes> {
Context context;
int layoutResourceId;
Recipes data[] = null;
Typeface typeface;
public ImageLoader imageLoader;
public MyCustomAdapter(Context context, int textViewResourceId,
Recipes[] dataset) {
super(context, textViewResourceId, dataset);
this.layoutResourceId = textViewResourceId;
this.context = context;
this.data = dataset;
imageLoader = new ImageLoader(context.getApplicationContext());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
RecipesHolder holder = new RecipesHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView) row.findViewById(R.id.title);
holder.category = (TextView) row.findViewById(R.id.category);
holder.source = (TextView) row.findViewById(R.id.source);
holder.country = (TextView) row.findViewById(R.id.country);
holder.readytime = (TextView) row.findViewById(R.id.readytime);
holder.tips = (Button) row.findViewById(R.id.tips);
holder.fav = (Button) row.findViewById(R.id.fav);
Recipes ap = data[position];
imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon);
holder.txtTitle.setText(ap.getNAME());
holder.category.setText(ap.getCATEGORY());
holder.source.setText(ap.getSOURCE());
holder.country.setText(ap.getCOUNTRY());
holder.readytime.setText(ap.getREADYTIME());
return row;
}
static class RecipesHolder {
ImageView imgIcon;
TextView txtTitle;
TextView category;
TextView source;
TextView country;
TextView readytime;
Button tips;
Button fav;
}}
//listview_layout.xml
//listview_layout.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"><ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:focusable="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/readytime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/country"
android:layout_alignBottom="@+id/country"
android:layout_marginLeft="73dp"
android:layout_toRightOf="@+id/country"
android:focusable="true"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/source"
android:focusable="true"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="@+id/source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/country"
android:layout_alignLeft="@+id/category"
android:focusable="true"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="@+id/category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/source"
android:layout_alignLeft="@+id/title"
android:text="TextView"
android:focusable="true"
android:textColor="#000" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/category"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView"
android:focusable="true"
android:textColor="#000" />
<Button
android:id="@+id/fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tips"
android:layout_below="@+id/source"
android:focusable="true"
android:background="@drawable/favourite" />
<Button
android:id="@+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginRight="14dp"
android:background="@drawable/yellow" /></RelativeLayout>
推荐答案
终于解决了listview中的所有控件(Buttons,textviews)都设置为focusable false的问题.
finally solved the issue when all the controls(Buttons,textviews) in listview set to focusable false.
这篇关于Onclicklistner 在片段列表视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!