如何在扩展的ListFragment类中实现长按事件?

时间:2017-11-04 19:22:38

标签: java android android-fragments

我已经通过使用Common Listener成功实现了ListFragment的事件处理,Common Listener将被一个活动中的几个Frags使用。我被设法在ListFragment中实现onListItemClick。但是我很难通过使用公共监听器来完成同一个ListFragment上的长按/按下事件。

在不偏离常见听众方法的情况下,最合适的方法是什么?

我的ListFragment代码如下所示

public class UnSettled_itemsFragment extends ListFragment {

    private SimpleCursorAdapter dataAdapter;
    private DB db;
    private Checque currentchqobj;
    private String currentchqnumber;

    private OnFragmentInteractionListener selectListener;   // Reference to Common FragmentListener

    public UnSettled_itemsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        Bundle bundle = this.getArguments();
        currentchqnumber = bundle.getString("message");
        currentchqobj=Checques.getChecqueByChecquekNo(currentchqnumber);

        Invoice[] invoice=OSInvoices.getInvoices();   // Get the OSInvoice

        // Inflate the ListView
        View view = inflater.inflate(R.layout.fragment_unsettled_invoice_list, container, false);

        final OSInvoiceAdapter adapter = new OSInvoiceAdapter(getActivity(), R.layout.invoice_list_single, invoice);

        setListAdapter(adapter);

        return view;
    }



    @Override
    // SingleClick Listner
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        // Create the Invoice reference at the selected Item
        final Invoice invoice = (Invoice) l.getItemAtPosition(position);

        // Pass the Cursor and the TAG to universal listener
        selectListener.onListFragmentInteraction("TAG_UN_INV_SINGLE", invoice);
    }


    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        if (activity instanceof OnFragmentInteractionListener) {
            selectListener = (OnFragmentInteractionListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement SelectionFragmentListener");
        }
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
        selectListener = null;
    }

Common Listener代码如下所示

package utill;

public interface OnFragmentInteractionListener<T> {
    void onListFragmentInteraction(String tag, T data);
}

动态创建碎片的活动代码如下所示

public class AddChecqueV2 extends Activity implements OnFragmentInteractionListener<Object> {

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    /*

    Some Code

    */
    // Display UnSettled Invoice List Fragment
                Bundle bundle_Un = new Bundle();
                String myMessage_Un = current_cheque_number;
                bundle.putString("message", myMessage_Un );
                UnSettled_itemsFragment fragobj_Un = new UnSettled_itemsFragment();
                fragobj_Un.setArguments(bundle_Un);

                FragmentTransaction transaction_Un = getFragmentManager().beginTransaction();

                transaction_Un.replace(R.id.settlement_Unsettled_fragment_container, fragobj_Un);
                transaction_Un.commit();

    }

    // Implement the Universal Fragment Listener
        // This will handle all the On xxx events in the Fragment
        @Override
        public void onListFragmentInteraction(String tag, Object data) {

            DB heading_db=new DB(AddChecqueV2.this);
            Cursor selectedInvCursor;
            Invoice selectedInvoiceListItem;
            double billBalance = 0;
            final String chequeNo = "CH/"+ String.format("%06d", heading_db.getChequeNo())+heading_db.getRefName();

            //Implement the Listener action for the Unsettled Invoices Single Click Actions
            if(tag.equals("TAG_UN_INV_SINGLE")){
                //################ UNSETTLED INVOICE SELECTION CODE <<<< START >>>> ################## 
                selectedInvoiceListItem = (Invoice) data;
                double selectedInvBalance;
                String selectedInvoice = selectedInvoiceListItem.getInvoiceNo();  
                // Check if the Invoice is already selected
                if(Invoices.isInvoiceExist(selectedInvoice)){
                    selectedInvBalance = selectedInvoiceListItem.getBalance();
                }
                else{
                    selectedInvBalance = selectedInvoiceListItem.getBalance();
                }
      }

//Implement the Listener action for the Unsettled Invoices Long Click Actions
        if(tag.equals("TAG_UN_INV_LONG")){

        // *****
        // This is the place my Long Click Code Goes


}

    }

1 个答案:

答案 0 :(得分:0)

ListFragment没有为处理项目的长按提供覆盖便利方法。您可以在基础ListView上自己设置一个:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return false;
            }
        });
    }