【问题标题】:Custom List Fragment disabling clicks to element and enabling button clicks(Android Studio)自定义列表片段禁用点击元素并启用按钮点击(Android Studio)
【发布时间】:2018-01-04 15:04:09
【问题描述】:

我知道它们是类似的问题,但我无法使它们起作用。我有一个列表片段,其中包含我的数组列表中的项目以及它旁边的一个按钮,用于我的数组列表中的每个元素。我的最终目标是让程序仅在用户点击按钮时做出响应,但我什至无法检测到屏幕上的点击。我还尝试将按钮的可聚焦设置为 false(从其他问题建议)但这也不起作用。这是我的代码。

public class ResultListFragment extends ListFragment {
    private List<String> listValues, keyValues;
    private String email, username;
    private ArrayAdapter<String> myAdapter;
    private ListView myListView;

    private TextView title;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_resultlist, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View v = getView();
        myListView = getListView();

        listValues = new ArrayList<String>();

        myAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
                R.layout.fragment_rowlayout, R.id.myListText, CameraActivity.resultList);
        setListAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();


    }
    @Override
    public void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.d("blabla", "onListItemClick: clicked to : "+position);
        final String delete=CameraActivity.resultList.get(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setCancelable(true);
            builder.setTitle("DELETION");
            builder.setMessage(delete + " delete it.");
            builder.setPositiveButton("Onayla",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getActivity().getApplicationContext(), delete+ "has been deleted", Toast.LENGTH_SHORT).show();
                            CameraActivity.resultList.remove(position);
                            myAdapter.notifyDataSetChanged();
                            for(String st:CameraActivity.resultList){
                                Log.d("TAG", "onClick: eleman: "  +st);
                            }
                        }
                    });
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();

    }
}

这是我的 xml 文件

分片结果列表

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp">

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:background="@drawable/cembutton"
        android:text="Yükle"
        android:id="@+id/load"
        android:layout_alignRight="@+id/results"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffff"
        android:textStyle="bold"/>

    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No data"/>
</RelativeLayout>

和fragment_rowlayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    >
        <TextView
            android:id="@+id/myListText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:textStyle="bold"
            android:textColor="#3700ff" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawable="@drawable/cembutton"
            android:layout_alignParentRight="true"
            android:text="Çıkart"
            android:layout_marginRight="50dp"/>

    </RelativeLayout>

【问题讨论】:

    标签: android android-fragments android-listfragment


    【解决方案1】:

    您应该为您的listview 提供custom adapter
    然后,在getView() 方法中,您可以通过 id 找到您的按钮并将onClickListener 设置为它。

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2012-12-03
      相关资源
      最近更新 更多