Button in Fragment's ListView item Interface Definition?

后端 未结 1 730
借酒劲吻你
借酒劲吻你 2020-12-20 04:01

In my app, I have an activity that has two fragments in actionbar tabs navigation mode, just like the android developer site example.

in my first fragment I have a l

相关标签:
1条回答
  • 2020-12-20 04:04

    If you want it on List Item Click

    Fragment A:

    public class FragmentA extends ListFragment {
    
    OnItemSelectedListener mListener;
    
    ...
    // Container Activity must implement this interface
    public interface OnItemSelectedListener {
        public void onItemSelected(int position);
    }
    ...
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnItemSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
        }
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    
        mCallback.onItemSelected(position);
    
        }   
    }
    

    ContainerActivity:

    public class ContainerActivity extends FragmentActivity 
        implements FragmentA.OnItemSelectedListener
    {
    
    //...
    
    
    
    public void onItemSelected(int Position/*pass anything which u want*/) 
        {
    
            SecondFragment second_fragment = (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentB);
    
            if(second_fragment !=null)
            {
                second_fragment.UpdateUI(Position); 
            }
    
        }
    
    
     }
    

    Second Fragment:

    public class SecondFragment extends Fragment {
    
        ...
        public void UpdateUI(Position)
        {
    
        }
    
    }
    

    Hope this helps. On click of a Button inside each listitem might be bit difficult, but try the same approach. May be you have to write the interface declaration and call in your custom adapter.

    0 讨论(0)
提交回复
热议问题