工具栏更改长按回收器视图中的项目

问题描述 投票:0回答:2

我有一个 Activity,其中包含三个片段。我将 TabLayout 与寻呼机一起使用,以便我可以在片段之间滑动。在这些片段之一上将有一个包含很少项目的回收器视图。我想要实现的是,当我长按 recyclerview 中的某个项目时,工具栏应该在顶部发生变化。

我尝试过这样的:

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;
    }
}

错误:

       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)
android android-fragments android-recyclerview android-toolbar android-tablayout
2个回答
1
投票
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;
    }
}

0
投票

我希望此参考可以帮助您解决问题,请点击以下链接: 覆盖工具栏

© www.soinside.com 2019 - 2024. All rights reserved.