Search code examples
javaandroidbutterknife

How to use Butter Knife For Header And Footer for ListView Android


I add a Header and a Footer to my listview like this:

lv = (ListView) findViewById(R.id.lv_process_person)
            View header = getLayoutInflater().inflate(R.layout.activity_input_forward_department_header, null);
            View footer = getLayoutInflater().inflate(R.layout.activity_input_forward_department_footer, null);
            lv.addHeaderView(header);
            lv.addFooterView(footer);

And i use view inside header like this:

btnForward = (TextView) header.findViewById(R.id.button_forward);
    btnForward.setText("SetText");

How to use ButterKnife Liblary intead of these code? i researched but not resuilt. thank you and sorry for my english.


Solution

  • You can use something similar to this example from the ButterKnife docs:

    You can also perform binding on arbitrary objects by supplying your own view root.

    public class FancyFragment extends Fragment {
      @BindView(R.id.button1) Button button1;
      @BindView(R.id.button2) Button button2;
    
      @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fancy_fragment, container, false);
        ButterKnife.bind(this, view);
        // TODO Use fields...
        return view;
      }
    }
    

    The class which you pass as the first argument to ButterKnife.bind() must have fields that are annotated with @BindView. The most straightforward way to do this is to create a custom View class called ListViewHeader or something more specific to your use.