【问题标题】:setOnItemClickListener not working in FragmentsetOnItemClickListener 在片段中不起作用
【发布时间】:2017-02-15 07:13:26
【问题描述】:

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

这是我的代码,

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>

【问题讨论】:

    标签: android listview android-fragments onitemclicklistener


    【解决方案1】:

    终于找到答案了。

    不要使用 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"
    

    感谢大家的帮助。

    【讨论】:

      【解决方案2】:
      <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"
      

      【讨论】:

        【解决方案3】:

        试试这个应该在参与者列表项.xml 中工作并从列表视图中删除

        android:descendantFocusability="blocksDescendants"
        

        【讨论】:

        • 谢谢先生。它有效,但现在我也无法更改复选框响应和编辑文本。
        • 尝试为 edittext 添加 TextWatcher 并为您的 pozo 中的复选框设置一个布尔变量,这可能会帮助您更改值
        • 我正在阅读日志。在那里我看到,当我单击 TextViews 时,Touch up 调度的日志用于 ListView,但是当我单击 edittext 或复选框时,Touch up 调度用于 edittext 或 textview。任何想法!
        • 我不明白你到底想达到什么目的?? setOnItemClickListener?或者你想实现edittext和checkbox click???
        • 我想点击一些特定的listitem并根据用户更改listitem的edittext和checkbox的值并在arraylist中更新它。
        【解决方案4】:

        你必须这样做

        private EditText notes;
        private CheckBox checkBox;
        

        然后将这些放在点击上方

             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 和复选框是列表视图中的视图 希望对你有帮助

        【讨论】:

        • 在此之前我无法获取点击的列表项。
        【解决方案5】:

        android:clickable=true 确保您已将 clickable 属性添加为 true。 在参与者列表.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/>
        

        如果这个建议有误,请告诉我?

        【讨论】:

        • 这件事已经解决了,但现在我无法编辑edittext和更新复选框。
        • 你的意思是,checkbox的点击动作没看到对吧?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-07
        • 1970-01-01
        相关资源
        最近更新 更多