如何使列表视图中的项目响应对列表视图中特定行的单击

How to make the items in the listview respond to the click on that particular row on the listview

我正在显示一个选项卡,它有两个标签 Category1 和 Category2。 该列表根据类别 1 和类别 2 的表中的数据填充。这工作正常,但要使列表视图中的项目以某种方式响应在列表视图上进行的点击是我所坚持的。例如:

我试图让复选框在用户长按列表视图中的特定行时变得可见并被选中。我正在尝试对其进行编码,但我不知道从哪里开始。没有专门针对此主题提出的问题。下面是我试过的代码。

片段类

public class CategoryFragment extends Fragment{

    private ListView listView;
    private String currentTabTag;
    private CategoryAdapter1 categoryAdapter1;
    private CategoryAdapter2 categoryAdapter2;
    private MergeAdapter adapter = new MergeAdapter();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_category_fragment_list, container, false);

        // get references to widgets
        listView = (ListView) view.findViewById (R.id.ListView);

        // get the current tab
        TabHost tabHost = (TabHost) container.getParent().getParent();
        currentTabTag = tabHost.getCurrentTabTag();

        // refresh the category list view
        refreshCategoryList();

        listView.setOnItemLongClickListener(this);

        // return the view
        return view;
    }

    public void refreshCategoryList() {
        // get category list for current tab from database
        Context context = getActivity().getApplicationContext();
        CategoryDatabase categoryDatabase = new CategoryDatabase(context);

        if(currentTabTag.equals("Category 1")) {
            ArrayList<Category1> category1ArrayList = categoryDatabase.getAllCategory1(currentTabTag);
            // create adapter and set it in the ListView widget
            categoryAdapter1 = new Category1Adapter(context, category1ArrayList);
            adapter.add(Category1Adapter);
        }
        else if(currentTabTag.equals("Category 2"))
        {
            ArrayList<Category2> category2ArrayList = categoryDatabase.getAllCategory2(currentTabTag);
            // create adapter and set it in the ListView widget
            categoryAdapter2 = new categoryAdapter2(context, category2ArrayList);
            adapter.add(categoryAdapter2);
        }
        listView.setAdapter(adapter);

    }
    @Override
    public void onResume() {
        super.onResume();
        refreshTaskList();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        ItemsListLayout itemsListLayout = new ItemsListLayout(context);

                switch (view.getId()) {
                    case R.id.listView: {
                        itemsListLayout.setMyListItems();
                        Toast.makeText(context,"Why cant I be called?",Toast.LENGTH_SHORT).show();
                        //registerForContextMenu(view);
                    }
            /*default:
                Intent intent = new Intent(context, MyCategoryAddEdit.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("categoryId", category.getId());
                intent.putExtra("editMode", true);
                context.startActivity(intent);
                break;*/
               }
        return false;
    }
}

activity_category_fragment_list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView
        android:id="@+id/prescriptorsListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:longClickable="true"
        android:descendantFocusability="blocksDescendants">
    </ListView>


</LinearLayout>

ItemsListLayout

public class ItemsListLayout extends RelativeLayout implements View.OnCreateContextMenuListener {

    private CheckBox completedCheckBox;
    private TextView nameTextView;
    private TextView notesTextView;
    private ListView listView;
    private CategoryDatabase categoryDatabase;
    private Context context;

    public ItemsListLayout(Context context) {   // used by Android tools
        super(context);
    }

    public ItemsListLayout(Context context, Prescription p) {
        super(context);

        // set context and get db object
        this.context = context;
        categoryDatabase = new CategoryDatabase(context);

        // inflate the layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.activity_my_category_items_list_layout, this, true);

        // get references to widgets
        completedCheckBox = (CheckBox) findViewById(R.id.completedCheckBox);
        completedCheckBox.setVisibility(GONE);
        nameTextView = (TextView) findViewById(R.id.nameTextView);
        notesTextView = (TextView) findViewById(R.id.notesTextView);

        //listView = (ListView) findViewById(R.id.listView);
        // set listeners
        //listView.setOnItemLongClickListener(this);
        // set task data on widgets
        setCategory1(p);
    }

    public void setCategory1(setCategory1 p) {
        setCategory1 = p;
        nameTextView.setText(setCategory1.getCategory1Name());

        // Remove the notes if empty
        if (setCategory1.getMedicineName().equalsIgnoreCase("")) {
            notesTextView.setVisibility(GONE);
        }
        else {
            notesTextView.setText(setCategory1.getDescriptionName());
        }
    }

    /*@Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (view.getId()) {
            case R.id.prescriptorsListView:
                completedCheckBox.setVisibility(VISIBLE);
                completedCheckBox.setChecked(true);
                break;
            default:
                Intent intent = new Intent(context, MyCategoryAddEdit.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("categoryId", category.getId());
                intent.putExtra("editMode", true);
                context.startActivity(intent);
                break;
        }
        return false;
    }
*/
    public void setMyListItems()
    {
        completedCheckBox = (CheckBox) findViewById(R.id.completedCheckBox);
        completedCheckBox.setVisibility(VISIBLE);
        completedCheckBox.setChecked(true);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
    {
        super.onCreateContextMenu(contextMenu);
        //MenuInflater inflater = this.context.
    }
}

activity_my_category_items_list_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutTaskItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <CheckBox
        android:id="@+id/completedCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:button="@drawable/btn_check"
        android:textColor="#ffffff"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/completedCheckBox"
        android:layout_alignBottom="@+id/completedCheckBox"
        android:layout_toRightOf="@+id/completedCheckBox"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:textIsSelectable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/notesTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameTextView"
        android:layout_marginTop="-5dp"
        android:layout_toRightOf="@+id/completedCheckBox"
        android:paddingBottom="7dp"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:textIsSelectable="false"
        android:focusableInTouchMode="false"/>

</RelativeLayout>

感谢任何形式的帮助。如果可能的话,尤其是代码片段 :).

我建议你阅读这篇文章blog, it has step by step procedure of what exactly you want to achieve. You can even clone the project from github