自定义列表片段禁用元素点击并启用按钮点击(Android Studio)

时间:2017-07-28 13:52:09

标签: android android-fragments android-listfragment

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

body

以下是我的xml文件

片段结果列表

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();

    }
}

和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: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>

1 个答案:

答案 0 :(得分:0)

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