我有一个在listfragment自定义适配器,还可以设置为onclicklistner列表视图列表视图。 但Onclicklistner不起作用。
这里是我的代码:
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
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
<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>
Answer 1:
终于解决了这个问题,当列表视图中的所有控件(按钮,textviews)设置为可聚焦假。
Answer 2:
检查MyCustomAdapter,有些部件(如:按钮,ImageButton的)在自定义布局会消耗click事件,然后onItemClick将永远不会被调用。
请使用您adaper的getView方法,下面的代码,以获得onclick事件。
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
Answer 3:
按照Android的API文档:
ListFragment具有由单个列表视图的默认布局。 但是,如果你愿意,你可以从onCreateView(LayoutInflater,ViewGroup中,捆绑)返回自己的视图层次定制片段布局。 要做到这一点,您的视图层次结构中必须包含id为一个ListView对象“@android:ID /列表”(或列表,如果它在代码)
既然你没有张贴碎片的布局文件我不知道出了什么错在这里。 下面的代码是当您使用ListFragment的默认列表视图应该如何。 如果您使用的是ListFragment你应该充分利用可用像其他方法setListAdapter
和onListItemClick
。 你还可以做同样的事情,而不使用ListFragment(只使用片段)。
所述片段代码(修改您的代码部件)
public class BasicFragment extends ListFragment {
MyCustomAdapter adapter;
@Override
public void onCreate(Bundle si) {
super.onCreate(si);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// not sure what you are doing here but data fetch should be asynchronous if it interacts with DB or makes network call
FetchedData DT = FetchedData.StaticDataTransfer();
RecepiesProperties[] AryObjaz = DT.getData();
getdata(AryObjaz);
adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout, Dataset);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast t = Toast.makeText(getActivity(), "Message",
Toast.LENGTH_SHORT);
t.show();
}
}
此外,我修改了适配器代码,以帮助回收的观点,当前的代码是不使用视图回收和总是夸大的看法。
适配器代码:
public class MyCustomAdapter extends ArrayAdapter<Recipes> {
Context context;
int layoutResourceId;
Recipes data[] = null;
Typeface typeface;
public ImageLoader imageLoader;
private LayoutInflater inflater;
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());
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecipesHolder holder = null;
//recycling views
if(null == row){
row = inflater.inflate(layoutResourceId, parent, false);
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);
row.setTag(holder);
}else{
holder = (RecipesHolder)row.getTag();
}
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;
}
}
Answer 4:
当你夸大你的意见,似乎你的观点有2个按钮它:
此row.findViewById(R.id.tips);
这row.findViewById(R.id.fav);
您应该删除这些按钮(带textviews替换他们,如果你不想让自己的点击)
或者,如果你希望这些按钮的点击,您可以使用OnClickListeners每个按钮和另一OnClickListener整个视图。
例
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//we only inflate if the row is null!!
if(row == null) {
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);
//we set the tag of the view to this holder so we can get it everytime
row.setTag(holder);
}
//change this to final so that you can use it inside your click listeners
final Recipes ap = data[position];
//here we get the holder of the view from its tag
RecipesHolder holder = (RecipesHolder) row.getTag();
//no changes to ur setup
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());
//now the click listeners
//tips button
holder.tips.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//tips has been clicked
//do whatever you want with `ap`
}
});
//fav button
holder.fav.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//fav has been clicked
//do whatever you want with `ap`
}
});
//whole item click
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//the row has been clicked
//do whatever you want with `ap`
}
});
return row;
}
Answer 5:
把下面的代码在onViewCreated()
方法:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
ListView list = (ListView) view.findViewById(android.R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast t = Toast.makeText(getActivity(), "Message",
Toast.LENGTH_SHORT);
t.show();
}
});
}
问题可能在于,在onViewCreate()的观点并没有构成可事实,所以它不是建议设置听众那里。
Answer 6:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//your code
}
});
我想,这应该工作
Answer 7:
适配器已方法areAllItemsEnabled(),它可能是有用的。
公共抽象布尔areAllItemsEnabled()
表示该适配器的所有物品是否被启用。 如果此方法的返回值随时间而改变,也不能保证它会生效。 如果属实,这意味着所有项目都选择和点击(没有分隔符。)
Answer 8:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
Answer 9:
只要把android:focusable="false" android:clickable="false"
的布局。 对于所有textviews,按钮等问题就迎刃而解了。
Answer 10:
我有一个类似的问题,我花了很长的时间来弄清楚什么是错的。 以前我的片段的onListItemClick()开辟了新的活动,但久违的片段后onListItemClick()侦听器没工作了。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// additional code (e.g. open new activity)
}
这个问题是通过创建我的自定义内的监听器,而不是ArrayAdapter解决。 我放置getView()的内部这样的代码:
rowView.setOnClickListener(new View.OnClickListener() {
final int p = position;
@Override
public void onClick(View v) {
Log.d("FeedListAdapter", "onClick()");
openPost(p); // for example
}
});
我彻夜未眠解决这个(它的)上午8时,我想我应该张贴我为别人谁是相似的点解决方案:)
文章来源: Onclicklistner not working in fragment listview