ContextMenu不适用于选项卡

时间:2016-08-28 03:24:04

标签: android tabs contextmenu

我有两个带有上下文菜单的选项卡但是当一个工作时,另一个不会在同一页面上工作,但在不同的选项卡上。这是我的代码,谢谢我无法让它工作。

带有标签

的页面
public class OwnerTabs extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
SharedPreferences pref;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_owner_tabs);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(2);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(OwnerTabs.this, InsertActivity.class);
            startActivity(in);
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.action_logout){
        editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent in = new Intent (getApplicationContext(), MainActivity.class);
        startActivity(in);
        finish();
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
            View rootView = inflater.inflate(R.layout.content_ownerhome, container, false);
            return rootView;
        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
            View rootView = inflater.inflate(R.layout.content_rented, container, false);
            return rootView;
        }
        else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3){
            View rootView = inflater.inflate(R.layout.fragment_owner_tabs, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText("This content is under construction. Thank you!!");
            return rootView;
        }
        else {

            View rootView = inflater.inflate(R.layout.fragment_owner_tabs, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Your Cars";
            case 1:
                return "Pending Cars";
            case 2:
                return "GPS";
        }
        return null;
    }
}
}

FirstTab

public class ownerFragment extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener {
final String TAG = this.getClass().getName();
private ArrayList<Cars> carList;
private ListView lvCars;
private FunDapter<Cars> adapter;
SharedPreferences pref;
private String LOG = "ownerhome";
TextView tvUser;


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.list_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    final Cars selectedCar = adapter.getItem(info.position);

    if (item.getItemId() == R.id.menuUpdate) {
        Intent in = new Intent(getActivity(), UpdateActivty.class);
        in.putExtra("cars", selectedCar);
        startActivity(in);

    } else if (item.getItemId() == R.id.menuDelete) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setMessage("Are you sure to remove " + selectedCar.Car_Model + "?");
        alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                carList.remove(selectedCar);
                adapter.notifyDataSetChanged();

                HashMap postData = new HashMap();
                postData.put("Car_No", "" + selectedCar.Car_No);
                postData.put("mobile", "android");


                PostResponseAsyncTask taskPost = new PostResponseAsyncTask(getActivity(), postData, new AsyncResponse() {
                    @Override
                    public void processFinish(String s) {
                        Log.d(TAG, s);
                        if (s.contains("success")) {
                            Toast.makeText(getActivity().getApplicationContext(), "Car deleted!!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                taskPost.execute("http://carkila.esy.es/delete.php");
            }
        });
        alert.setNegativeButton("Cancel", null);
        alert.show();
    }

    return super.onContextItemSelected(item);
}

文本菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.kun.carkila.ListActivity">
<item
android:id="@+id/menuUpdate"
android:orderInCategory="1"
android:title="Update"
app:showAsAction="never" />
<item
    android:id="@+id/menuDelete"
    android:orderInCategory="2"
    android:title="Delete"
    app:showAsAction="never" />
</menu>

第二个标签

public class rentedActivityFragment extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener {
final String TAG = this.getClass().getName();
private ArrayList<RentedCars> carList;
private ListView lvRented;
private FunDapter<RentedCars> adapter;
SharedPreferences pref;


public rentedActivityFragment() {
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.reject_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    final RentedCars selectedCar = adapter.getItem(info.position);


    if(item.getItemId() == R.id.menuAccept){
        Intent in = new Intent(getActivity(), ownerAccept.class);
        in.putExtra("cars", selectedCar);
        startActivity(in);
    }else if (item.getItemId()==R.id.menuReject){
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setMessage("Are you sure to reject "+selectedCar.carModel +"?");
        alert.setPositiveButton("Reject", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                carList.remove(selectedCar);
                adapter.notifyDataSetChanged();

                HashMap postData = new HashMap();
                postData.put("resID", "" + selectedCar.resID);
                postData.put("mobile", "android");
                PostResponseAsyncTask taskPost = new PostResponseAsyncTask(getActivity(), postData, new AsyncResponse() {
                    @Override
                    public void processFinish(String s) {
                        Log.d(TAG, s);
                        if (s.contains("success")) {
                            Toast.makeText(getActivity().getApplicationContext(), "Reservation rejected", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                taskPost.execute("http://carkila.esy.es/rejectrent.php");
            }
        });
        alert.setNegativeButton("Cancel",null);
        alert.show();
    }

return super.onContextItemSelected(item);
}

上下文菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.kun.carkila.ListActivity">
<item
android:id="@+id/menuAccept"
android:orderInCategory="1"
android:title="Accept"
app:showAsAction="never" />
<item
    android:id="@+id/menuReject"
    android:orderInCategory="2"
    android:title="Reject"
    app:showAsAction="never" />
</menu>

提前谢谢你们。 :d

0 个答案:

没有答案