You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fragment中ListView的setOnItemClickListener点击事件失效求助

Fixing ListView Item Click Not Responding in Tab Fragment

Let's break down the issues and fix your ListView click problem step by step:

1. The Immediate Culprit: Disabled Clickability in Layout

Looking at your tabFragment1.xml layout, your ListView has these attributes set that explicitly block all click and focus interactions:

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

This is the primary reason your setOnItemClickListener isn't triggering at all.

Fix: Remove those three attributes from your ListView tag. Your updated ListView should look like this:

<ListView 
    android:id="@+id/listReport" 
    android:layout_width="match_parent" 
    android:layout_height="300dp"/>

2. Optimize ListView Initialization in Fragment

Right now you're using getActivity().findViewById(R.id.listReport) to get the ListView reference, which can lead to conflicts if other views in the parent activity share the same ID. Instead, grab the reference directly from the inflated view in onCreateView:

Update your Fragment code:

public class TabFragment1_Staff extends Fragment {
    ListView lsReport;
    ArrayList<HashMap<String, String>> reportlist;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.activity_tab_fragment1__staff, container, false);
        // Initialize ListView directly from the inflated fragment view
        lsReport = view.findViewById(R.id.listReport);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        reportlist = new ArrayList<>();
        loadReport();
        lsReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Your existing intent logic remains the same
                Intent intent = new Intent(getActivity(),AddStatusDialogWindow.class);
                Bundle bundle = new Bundle();
                Log.e("HANIS",reportlist.get(position).get("reportID"));
                bundle.putString("reportID",reportlist.get(position).get("reportID"));
                bundle.putString("fullName",reportlist.get(position).get("fullName"));
                bundle.putString("icNum",reportlist.get(position).get("icNum"));
                bundle.putString("phoneNum",reportlist.get(position).get("phoneNum"));
                bundle.putString("Rdate",reportlist.get(position).get("Rdate"));
                bundle.putString("Rtime",reportlist.get(position).get("Rtime"));
                bundle.putString("timeName",reportlist.get(position).get("timeName"));
                bundle.putString("typeVehicle",reportlist.get(position).get("typeVehicle"));
                bundle.putString("registrationnum",reportlist.get(position).get("registrationnum"));
                bundle.putString("location",reportlist.get(position).get("location"));
                bundle.putString("brief",reportlist.get(position).get("brief"));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}

3. Check for Focus-Stealing Views in Custom Item Layout

If after fixing the above your click still doesn't work, inspect your activity_custom_list__status_report.xml (the item layout used by CustomList_StatusReport). Views like Button, CheckBox, or EditText are focusable by default, which can steal click events from the parent ListView item.

Fix for item layout:

  • Add these attributes to any focusable child views:
    android:focusable="false"
    android:focusableInTouchMode="false"
    
  • Or, add this to the root layout of your item to prioritize parent clicks:
    android:descendantFocusability="blocksDescendants"
    

4. Verify AsyncTask Data Population

Double-check that your LoadReport AsyncTask properly parses the API response and populates reportlist before setting the adapter. If the list is empty, there won't be any items to trigger clicks on.

After applying these fixes, your ListView item clicks should start working as expected!

内容的提问来源于stack exchange,提问作者Yvevon Harm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:27:37