Android ListView没有响应

时间:2015-04-01 11:32:33

标签: android listview android-listview adapter

我有ListView和适配器的问题。 ListView setOnItemClickListener和setOnItemLongClickListener没有响应,但onScroll工作正常。这是代码,你可能有我的解决方案......

提前致谢。

public class AFragment extends Fragment {

View rootView;
ListView l;
public static AArrayAdapter adapter;

public AFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_list, container, false);
    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    l = (ListView) rootView.findViewById(R.id.listView);

    adapter = new AArrayAdapter(getActivity(), R.layout.list_item_adapter, new ArrayList<AString>());
    l.setAdapter(adapter);

    l.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //Toast doesn't work
            return true;
        }
    });

    aList();

    l.setOnScrollListener(new OnScrollListener() {
        private int mLastFirstVisibleItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            //
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (mLastFirstVisibleItem < firstVisibleItem) {
                // working
            }
            if (mLastFirstVisibleItem > firstVisibleItem) {
                // working
            }
            mLastFirstVisibleItem = firstVisibleItem;
        }
    });

   l.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             //Toast doesn't work
         }
     });
}

public void aList(){
    ...
}

/***/

public class AArrayAdapter extends ArrayAdapter<AString> {
    private final Context context;
    private List<AString> items;

    public AArrayAdapter(Context context, int layoutResourceId, List<AString> items) {
        super(context, layoutResourceId, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.list_item_adapter, parent, false);

        AHolder holder = null;
        holder = new AHolder();
        holder.acc = items.get(position);
        holder.id = (TextView) rowView.findViewById(R.id.idLine);
        holder.name = (TextView) rowView.findViewById(R.id.firstLine);
        holder.value = (TextView) rowView.findViewById(R.id.secondLine);

        rowView.setTag(holder);

        setupItem(holder);

        final TextView tvId = (TextView) rowView.findViewById(R.id.idLine);
        final ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.icon);

        return rowView;
    }

    private void setupItem(AccHolder holder) {
        holder.name.setText(holder.acc.getName());
        holder.value.setText(holder.acc.getValue());
        holder.id.setText(String.valueOf(holder.acc.getId()));
        holder.removeButton = (ImageButton) rowView.findViewById(R.id.icon);
        holder.removeButton.setTag(holder.account);

        holder.removeButton.setFocusable(false); // <=========== :)
    }

    public class AHolder {
        AString account;
        TextView id;
        TextView name;
        TextView value;
    }
}
}

2 个答案:

答案 0 :(得分:0)

这可能与 - ListView items are not clickable. why?

重复

要解决此问题,您必须在ListView / GridView中没有任何可聚焦项目。

因此,对于Buttons / ImageButtons,您必须设置:

android:focusable="false" 

如果您在ListView / GridView中有一个ImageButton,您可以将其切换到ImageView,因为无论如何该按钮都不会被点击,这样您就不需要改变焦点。

同样如所提供的链接中所述的fpr0001,您也可以这样做:

  

这个答案对我有用:https://stackoverflow.com/a/16536355/5112161

     

主要是他在LinearLayout或RelativeLayout中添加了以下内容:

android:descendantFocusability="blocksDescendants"
     

您还需要从以下所有xml中删除:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"

答案 1 :(得分:0)

如果您有可点击的视图,行中的按钮或复选框最好使用Recyclerview。这将是更好的选择。您可以找到Recyclerview here的基本实现。

按钮或复选框单击侦听器会干扰listview单击侦听器。在行的根元素中 添加android:descendantFocusability="beforeDescendants"android:descendantFocusability="blocksDescendants"之一 还可以尝试为按钮添加android:focusable="false"

这应该可以解决问题。