我在listfragment中有自定义适配器的列表视图,并为listview设置了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>
答案 0 :(得分:12)
终于解决了这个问题。
答案 1 :(得分:7)
检查MyCustomAdapter,自定义布局中的某些小部件(例如:Button,ImageButton)将使用click事件,然后onItemClick将永远不会被调用。
在adaper的getView方法中使用以下代码来获取onClick事件。
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
答案 2 :(得分:6)
根据Android API文档:
ListFragment具有默认布局,该布局由单个列表视图组成。 但是,如果需要,可以通过自定义片段布局 从onCreateView返回您自己的视图层次结构(LayoutInflater, ViewGroup,Bundle)。 要执行此操作,您的视图层次结构必须包含 具有id“@android:id / list”的ListView对象(如果它在,则列出 代码)强>
由于你没有发布片段的布局文件,我不知道这里出了什么问题。
以下代码是使用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;
}
}
答案 3 :(得分:4)
当您夸大视图时,您的视图中似乎有2个按钮:
这row.findViewById(R.id.tips);
和row.findViewById(R.id.fav);
您应该删除这些按钮(如果您不想点击它们,请用textviews替换它们)
或者,如果您想要点击这些按钮,可以为每个按钮使用OnClickListener,为整个视图使用另一个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;
}
答案 4 :(得分:3)
将以下代码放在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()中尚未构建视图,因此不建议在那里设置侦听器。
答案 5 :(得分:2)
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//your code
}
});
我认为这应该有用
答案 6 :(得分:2)
适配器有方法areAllItemsEnabled(),它可能很有用。
public abstract boolean areAllItemsEnabled()
指示是否已启用此适配器中的所有项目。如果此方法返回的值随时间而变化,则无法保证它将生效。如果为true,则表示所有项目都是可选择且可点击的(没有分隔符。)
答案 7 :(得分:1)
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
答案 8 :(得分:0)
只需将android:focusable="false" android:clickable="false"
放入布局中即可。适用于所有文本视图,按钮等。
解决了问题。
答案 9 :(得分:0)
我有一个类似的问题,我花了很长时间才弄清楚出了什么问题。 以前我的片段的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点),我想我应该为其他在类似地点的人发布我的解决方案:)