ListFragment上的ClickListener和LongClickListener

时间:2012-04-01 19:22:24

标签: android

我有一个ListFragment,我想添加一个普通的点击监听器和一个长的监听器。

我覆盖ListFragment上的onListItemClick()方法。这里没有问题。

然后我创建了一个Adapter,我在getView()方法中通过setOnLongClickListener()添加了一个OnLongClickListener:

public class QuestionsAdapter extends BaseAdapter {

    private QuestionsBean questions = null;
    private LayoutInflater inflater = null;

    public QuestionsAdapter(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return (questions != null && questions.getQuestionList() != null) ? questions.getQuestionList().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return (questions != null && questions.getQuestionList() != null) ? questions.getQuestionList().get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        QuestionBean question = ((QuestionsBean) questions).getQuestionList().get(position);
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_fragment_question, null);

            holder.questionItem = (TextView) convertView.findViewById(R.id.itemFragmentQuestions);
            holder.questionItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    System.out.println("test => it works");
                    ;
                    return false;
                }
            });

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.questionItem.setText(question.getQuestion());

        return convertView;
    }

    private class ViewHolder {
        TextView questionItem;
    }

    public void setQuestions(QuestionsBean questions) {
        List<QuestionBean> temp = new ArrayList<QuestionBean>();
        if (questions != null && questions.getQuestionList() != null) {
            for (QuestionBean question : questions.getQuestionList()) {
                if (question.isActivated()) {
                    temp.add(question);
                }
            }
            questions.setQuestionList(temp);
        }

        this.questions = questions;
        notifyDataSetChanged();
    }
}

现在,longClickListener可以正常工作,但不能正常点击(实际上它可以像10次中的一次,或15或20次(它取决于...)。

我不知道为什么当我设置两个监听器时,onListItemClick()不起作用。

有什么想法吗?

谢谢你的帮助。

编辑:我的行xml          

    <TextView
        android:id="@+id/itemFragmentQuestions"
        style="@style/ItemFragmentQuestion"
        android:layout_marginBottom="@dimen/itemFragmentQuestionsMarging"
        android:background="@drawable/background_validated" >
    </TextView>

</LinearLayout>

编辑:

我的ListFragment类:

public class QuestionsFragment extends ListFragment {

    public static final int VALIDATED = 1;
    public static final int IN_PROGRESS = 1;
    public static final int NON_OBSERVED = 1;

    private QuestionsAdapter adapter;
    private QuestionsBean questions;

    public QuestionsFragment() {

    }

    public QuestionsFragment(QuestionsBean questions) {
        this.questions = questions;
    }

    public int getShownIndex() {
        return getArguments() != null ? getArguments().getInt("index", 0) : 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.adapter = new QuestionsAdapter(inflater);
        registerForContextMenu(getListView());

        View questionsView = (LinearLayout) inflater.inflate(R.layout.fragment_questions, null);

        setListAdapter(adapter);

        return questionsView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (l == this.getListView()) {
            QuestionBean question = (QuestionBean) l.getItemAtPosition(position);
            TextView textView = (TextView) v.findViewById(R.id.itemFragmentQuestions);
            switch (question.getValue()) {
            case QuestionBean.OK:
                question.setValue(QuestionBean.NOK);
                textView.setBackgroundResource(R.drawable.background_in_progress);
                break;
            case QuestionBean.NOK:
                question.setValue(QuestionBean.NA);
                textView.setBackgroundResource(R.drawable.background_non_observed);
                break;
            case QuestionBean.NA:
                question.setValue(QuestionBean.OK);
                textView.setBackgroundResource(R.drawable.background_validated);
                break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh(this.questions);
    }

    public void refresh(QuestionsBean questions) {
        this.questions = questions;
        adapter.setQuestions(questions);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Menu Title");
        menu.add(0, v.getId(), 0, "Option 1");
        menu.add(0, v.getId(), 0, "Option 2");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        if (item.getTitle() == "Option 1") {
            // Option 1 code here 
       } else if (item.getTitle() == "Option 2") {
            // Option 2 code here
       }
        return super.onContextItemSelected(item);
    }
}

@Barak正如你所看到的,我在onCreateView()中设置了适配器,当我立即放入registerForContextMenu(getListView())时,我收到一个错误。似乎还没有设置getListView()。

2 个答案:

答案 0 :(得分:2)

为什么不使用上下文菜单进行长按,而不是在适配器中使用?

registerForContextMenu(getListView());

然后:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Menu Title");
    menu.add(0, v.getId(), 0, "Option 1");
    menu.add(0, v.getId(), 0, "Option 2");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    if (item.getTitle().equals("Option 1")) {
        // Option 1 code here 
   } else if (item.getTitle().equals("Option 2")) {
        // Option 2 code here
   }
    return super.onContextItemSelected(item);
}

答案 1 :(得分:1)

我认为您应该使用listView.setOnItemLongClickListener(...);代替listView.setOnLongClickListener(...);

抱歉撕毁愈合伤口: - )