setOnItemClickListener 在 Fragment 中不起作用

setOnItemClickListener not working in Fragment

我正在尝试更改已单击列表项的文本和复选框响应,但我无法这样做。因为我无法获得点击的列表项。 该片段是视图寻呼机的一部分。

这是我的代码,

public class ParticipantsFragment extends Fragment {

    private ParticipantAdapter mAdapter;
    SwipeRefreshLayout refreshLayout;
    private String mParticipantId, mEventId, mGender;
    ParticipantsAsyncTask task;


    public ParticipantsFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
        mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
        mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);

        Log.v("fragh", mEventId + " " + mParticipantId);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.participant_list, container, false);

        TextView textView = (TextView) rootView.findViewById(R.id.no_participant);

        task = new ParticipantsAsyncTask();
        task.execute();

        ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);



        participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());

                EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
                String text = notes.getText().toString();

                Log.v("Participant Text",text);

                CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
                String check;
                if(checkBox.isChecked())
                {
                    check = "1";
                }
                else
                {
                    check = "0";
                }

            }
        });

        Button submit = (Button) rootView.findViewById(R.id.submit_participant);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());

        participantListView.setAdapter(mAdapter);
        participantListView.setEmptyView(textView);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                task = new ParticipantsAsyncTask();
                task.execute();
                refreshLayout.setRefreshing(false);
            }
        });


        return rootView;

    }

    private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {

        private ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            progress = new ProgressDialog(getContext());
            progress.setMessage("Gathering Data...");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();
        }

        @Override
        protected void onPostExecute(List<Participant> data) {

            // Clear the adapter of previous participant data
            mAdapter.clear();

            // If there is a valid list of {@link Event}s, then add them to the adapter's
            // data set. This will trigger the ListView to update.
            if (data != null && !data.isEmpty()) {
                mAdapter.addAll(data);
            }
        }

        @Override
        protected List<Participant> doInBackground(Void... params) {

            List<Participant> result;

            if (!mGender.isEmpty()) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(Config.KEY_EVENT_ID, mEventId);
                map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
                map.put(Config.KEY_GENDER, mGender);

                result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);

            } else {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(Config.KEY_EVENT_ID, mEventId);
                map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
                Log.v("law", mEventId + ", " + mParticipantId);
                result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);

            }

            progress.dismiss();
            return result;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        mAdapter.clear();
    }
}

participant_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/no_participant"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:gravity="center_horizontal"
    android:text="@string/no_participant_found"
    android:textColor="@color/primaryText"
    android:textSize="24sp"
    android:visibility="gone" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout_participant_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

        <ListView
            android:id="@+id/participant_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/divider"
            android:dividerHeight="1dp"
            android:descendantFocusability="beforeDescendants"
            android:drawSelectorOnTop="true"
            android:headerDividersEnabled="true" />

</android.support.v4.widget.SwipeRefreshLayout>

<Button
    android:id="@+id/submit_participant"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:textSize="20sp"
    android:background="@color/colorAccent"/>

</LinearLayout>

participant_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:clickable="true"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="horizontal"
    android:padding="8dp">

<TextView
    android:id="@+id/participant_list_item_id"
    android:layout_width="0dp"
    android:textStyle="bold"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="2"
    android:textSize="20sp"
    tools:text="M78" />

<TextView
    android:id="@+id/participant_list_item_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="5"
    android:textSize="20sp"
    android:fontFamily="sans-serif"
    tools:text="Billu Barber" />

<EditText
    android:id="@+id/participant_list_item_notes"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_weight="6"
    android:textSize="12sp"
    android:background="@drawable/participant_list_notes"
    android:hint="@string/participant_notes"
    android:inputType="textMultiLine"
    android:scrollbars="none"
    android:imeOptions="actionDone"
    android:maxLength="50"
    android:padding="4dp" />

<CheckBox
    android:id="@+id/participant_list_item_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp" />

</LinearLayout>
<CheckBox
android:id="@+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:clickable="false"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />

尝试替换您的 CheckBox 并添加这两行

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

尝试这应该在 participant_list_item.xml 中工作并从列表视图中删除

android:descendantFocusability="blocksDescendants"

你必须这样做

private EditText notes;
private CheckBox checkBox;

并将这些放在 onclick 上面

     notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
     String text = notes.getText().toString();

     Log.v("Participant Text",text);

     checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);

edittext和check box是listview中的view 希望这对你有帮助

android:clickable=true 确保已将 clickable 属性 添加为 true。 在 participantslist.xml 文件中:

<ListView
        android:id="@+id/participant_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/divider"
        android:dividerHeight="1dp"
        android:descendantFocusability="beforeDescendants"
        android:drawSelectorOnTop="true"
        android:headerDividersEnabled="true" 
        android:clickable=true/>

让我知道这个建议是否错误?

终于找到答案了

不要对项目点击使用 setOnItemClickListener。通过使用 setOnClickListener() 在适配器方法中使用项视图单击。在 onClick()

中做事
@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View listItemView = convertView;

    if (listItemView == null) 
    {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.participant_list_item, parent, false);
    }

    listItemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            Toast.makeText(mContext, "click item",Toast.LENGTH_LONG).show();
        }
    });

    Participant currentParticipant = getItem(position);

    if(currentParticipant.getParticipantGender().equals("F")) 
    {
        TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
        idView.setTextColor(getColor(mContext, R.color.colorPrimary));
        idView.setText(currentParticipant.getParticipantId());
    }
    else if(currentParticipant.getParticipantGender().equals("M"))
    {
        TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
        idView.setTextColor(getColor(mContext, R.color.colorAccent));
        idView.setText(currentParticipant.getParticipantId());
    }
    TextView nameView = (TextView) listItemView.findViewById(R.id.participant_list_item_name);
    nameView.setText(currentParticipant.getParticipantName());

    final EditText notesView = (EditText) listItemView.findViewById(R.id.participant_list_item_notes);
    notesView.setText(currentParticipant.getParticipantNotes());

    CheckBox checkedView = (CheckBox) listItemView.findViewById(R.id.participant_list_item_checkbox);
    if (currentParticipant.getParticipantCheck().equals("1"))
    {
        checkedView.setChecked(true);
    }
    else
    {
        checkedView.setChecked(false);
    }

    return listItemView;
}

从 participant_list_item.xml

中删除
android:descendantFocusability="blocksDescendants"

谢谢大家的帮助。