我有这个OnLongClickListener
没有被解雇。我想我已经正确设置了所有内容,并且它之前使用过上下文菜单,但现在它没有触发,日志甚至没有给我任何错误。
我真的希望你能给我任何想法。这可能是某个地方的愚蠢错误。
package fragments;
import interfaces.AsyncTaskCompleteListener;
import java.util.List;
import com.example.slidingmenu.R;
import dialog.EditDialog;
import services.GetRequest;
import model.Post;
import adapter.PostAdapter;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class HomeFragment extends Fragment implements AsyncTaskCompleteListener, View.OnLongClickListener {
private ListView mainListView;
private GetRequest service;
private ProgressDialog dialog;
private ActionMode mActionMode;
public HomeFragment() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedIntanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mainListView = (ListView) rootView.findViewById(R.id.mainListView);
mainListView.setOnLongClickListener(this);
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Please wait...");
service = new GetRequest(this);
service.execute();
dialog.show();
registerForContextMenu(mainListView);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()) {
case R.id.edit:
Log.d("edit", "pressed");
FragmentManager fragmentManager = getFragmentManager();
EditDialog editDialog = new EditDialog();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, editDialog).addToBackStack(null).commit();
mode.finish();
return true;
default:
return false;
}
}
};
@Override
public boolean onLongClick(View v) {
Toast.makeText(getActivity(), "PRESSED", Toast.LENGTH_SHORT).show();
Log.d("hey", "pressed");
if(mActionMode != null) {
return false;
}
mActionMode = getActivity().startActionMode(mActionModeCallback);
mainListView.setSelected(true);
return true;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(getActivity(), "Refresh!", Toast.LENGTH_SHORT).show();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Please wait while refreshing...");
dialog.show();
service = new GetRequest(this);
service.execute();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTaskComplete(List<Post> posts) {
PostAdapter adapter = new PostAdapter(getActivity(), R.layout.listview_item_row, posts);
LayoutInflater inflater = (LayoutInflater)getActivity().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = (View)inflater.inflate(R.layout.listview_header_row, null);
if(mainListView.getHeaderViewsCount() == 0) {
mainListView.addHeaderView(header);
}
mainListView.setAdapter(adapter);
if(dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
public void onTaskComplete(String result) {
// TODO Auto-generated method stub
}
}
这是我的 xml 文件ListView
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainListView"
android:longClickable="true"
android:clickable="true"
></ListView>
</LinearLayout>