扩展ListFragment的类中的onListItemClick()方法不会被调用,单击列表项时甚至没有点击动画。
即使我将TextViews设置为focusable =“false”或其根元素设置为android:descendantFocusability =“blocksDescendants”它也不起作用。
问题出现在CustomAdapter上,当我使用默认的ArrayAdapter和默认的android.R.layout.simple_list_item_1时没有问题。此外,我可以设置listView.setOnItemClickListener(...),列表将被点击在正确的位置(但没有点击动画)。
以下是相关代码:
public class MyListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// String[] strings = {"one", "two", "three", "four"};
// ArrayList stringsList = new ArrayList(Arrays.asList(strings));
// ArrayAdapter adapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,stringsList);
ArrayList items = new ArrayList();
for (int i = 1; i < 20; i++) {
items.add(new Item("Sample Item " + i));
}
CustomAdapter adapter = new CustomAdapter(getContext(), items);
setListAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getContext(), "Toast", Toast.LENGTH_SHORT).show();
}
}
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter implements ListAdapter {
Context context;
ArrayList items;
public CustomAdapter(Context context, ArrayList items) {
this.context = context;
this.items = items;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getCount() {
return items.size();
}
@Override
public Item getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater = LayoutInflater.from(context);
if (convertView == null) {
holder = new Holder();
convertView = layoutInflater.inflate(R.layout.item_row, null);
holder.title = (TextView) convertView.findViewById(R.id.title_textView);
holder.description = (TextView) convertView.findViewById(R.id.description_textView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.title.setText(getItem(position).getTitle());
holder.description.setText(getItem(position).getDescription());
return convertView;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
return items.isEmpty();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
class Holder {
TextView title, description;
}
}
public class Item {
private String title = "test";
private String description = "test description";
public Item(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return getTitle();
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我无法发布xml,但它是item_row.xml中非常简单的TextView,一个ListView,其中包含:fragment_list.xml的android:list id,以及activity_main.xml的单个片段。
以下是下载的最小失败示例:https://github.com/ronenlh/ListFragment
谢谢。
编辑:我找到了解决方案。我为适配器实现了ListAdapter,但我不得不扩展android提供的两个类中的任何一个为我实现ListAdapter。 SimpleAdapter用于静态数据(Maps),SimpleCursorAdapter用于Cursor查询结果。