androidandroid-fragmentsandroid-recyclerviewandroid-toolbarandroid-tablayout

toolbar change onLongClick on item in recyclerview


I have an Activity in which there are three fragments. I have used TabLayout with pager so that I can slide between the fragments. On one of those fragments there will be a recyclerview having few items. What I wanted to achieve is When I long press a item in recyclerview, toolbar should change on the top.

I have tried like this:

home.java

    toolbarchange=0;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    }

@Override
protected void onResume()
{
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    tabLayout = (TabLayout) findViewById(R.id.tablayout);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.AddFragments(new tab1(), "Files");
    adapter.AddFragments(new tab2(),"Recent");
    adapter.AddFragments(new tab3(),"Contacts");

    pager.setOffscreenPageLimit(3);

    pager.setAdapter(adapter);
    tabLayout.setupWithViewPager(pager);


    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    if(toolbarchenge==0){

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        this.menu = menu;
    }
    else {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.onlongclick, menu);

    }
    return super.onCreateOptionsMenu(menu);

}

public void toolbarChange(){

    toolbarchenge=1;
    invalidateOptionsMenu();
}

tab1.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab1, container, false);

    fRecyclerView = (RecyclerView) v.findViewById(R.id.file_tab);
    fRecyclerView.setHasFixedSize(true);
    fLayoutManager = new LinearLayoutManager(this.getContext());

    fRecyclerView.setLayoutManager(fLayoutManager);
    fAdapter = new MyAdapter(this.context,fDataset,inflate,menu);
    fRecyclerView.setAdapter(fAdapter);

    return v;
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

private ArrayList<String> fDataset;

private save s;
private Context ctx;
private Drawable myDrawable;
private MenuInflater inflater;
private Menu menu;


public class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView filename;

    private ProgressBar file_progressbar;

    private ImageView fileicon;
    public MyViewHolder(View v) {

        super(v);

        filename = (TextView) v.findViewById(R.id.filename);

        file_progressbar = (ProgressBar) v.findViewById(R.id.file_progressbar);

        fileicon = (ImageView)v.findViewById(R.id.fileicon);


    }

}

public MyAdapter(Context ctx,ArrayList<String> myDataset,MenuInflater inflater,Menu menu) {

    fDataset = myDataset;

    this.inflater=inflater;
    this.menu=menu;
    this.ctx = ctx;

}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(this.ctx).inflate(R.layout.file_row, parent, false);

    MyViewHolder vh = new MyViewHolder(v);

    return vh;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

    final String fname = fDataset.get((fDataset.size() - 1) - position);
    holder.filename.setText(fDataset.get((fDataset.size() - 1) - position));

    holder.itemView.setOnLongClickListener((View.OnLongClickListener) new OnClickListenr());


}


@Override
public int getItemCount() {
    return fDataset.size();
}


private class OnClickListenr implements View.OnLongClickListener {


    @Override
    public boolean onLongClick(View v) {

                  home h = new home();
                  h.toolbarChenge();


        return false;
    }
}

ERROR:

       java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                 at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:67)
                                                 at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:144)
                                                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:29)
                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:135)
                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117)
                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:456)
                                                 at android.support.v7.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:198)
                                                 at com.example.sandesh.filer.home.toolbarChange(home.java:148)
                                                 at com.example.sandesh.filer.MyAdapter$OnClickListenr.onLongClick(MyAdapter.java:169)
                                                 at android.view.View.performLongClick(View.java:4819)
                                                 at android.view.View$CheckForLongPress.run(View.java:19840)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Solution

  • You do one thing the way of calling a activity method  is like this
    
    private class OnClickListenr implements View.OnLongClickListener {
    
    
        @Override
        public boolean onLongClick(View v) {
    
                      ((home)ctx).toolbarChenge();
    
    
            return false;
        }
    }