ListView项不可点击,内部有单个TextView

时间:2018-07-24 12:10:26

标签: listview android-fragments textview click

我的ListView项目单击不起作用。我不知道为什么。它是简单的列表视图,每行只有一个TextView。请帮忙。 我尝试了其他选项,但没有一个起作用。我的片段是抽屉活动的一部分,其中我根据用户的点击替换片段。

fragment_first.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/detailNoticeListView"/>

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:text="@string/app_name"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content"
        android:id="@+id/detailNoticeText" />
</LinearLayout>

适配器:

公共类MyAdapter扩展了android.widget.BaseAdapter {

private List<DataModel> dataModels;
private Activity activity;

public MyAdapter(List<DataModel> dataModels, Activity activity){
    this.dataModels = dataModels;
    this.activity = activity;
}

@Override
public int getCount() {
    return dataModels.size();
}

@Override
public Object getItem(int i) {
    return dataModels.get(i);
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    if (view == null) {
        view = LayoutInflater.from(activity).
                inflate(R.layout.item, viewGroup, false);
    }

    DataModel currentItem = (DataModel) getItem(i);

    // get current item to be displayed
   TextView textViewItemName = (TextView)
            view.findViewById(R.id.detailNoticeText);
    textViewItemName.setText(currentItem.getText());
    return view;
}

@android.support.annotation.Nullable
@Override
public CharSequence[] getAutofillOptions() {
    return new CharSequence[0];
}

}

片段:

公共类MyFragment扩展了片段{

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
java.util.ArrayList<DataModel> dataModels;
ListView listView;
private static MyAdapter adapter;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public MyFragment() {
    // Required empty public constructor
}

public static MyFragment newInstance(String param1, String param2) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    dataModels= new java.util.ArrayList<>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_first, container, false);
    listView=(ListView)view.findViewById(R.id.detailNoticeListView);


    dataModels.add(new DataModel(
            "1"));
    dataModels.add(new DataModel(
            "2"));
    dataModels.add(new DataModel(
            "3"));
    dataModels.add(new DataModel(
            "4"));

    adapter= new MyAdapter(dataModels, getActivity());
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(android.widget.AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            android.widget.Toast.makeText(getActivity(), "User logged out successfully", android.widget.Toast.LENGTH_SHORT).show();
        }

    });

    return view;
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

1 个答案:

答案 0 :(得分:0)

将android:clickable =“ true”添加到您的商品布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true">
    <TextView
        android:layout_width="match_parent"
        android:text="@string/app_name"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content"
        android:id="@+id/detailNoticeText" />
</LinearLayout>