我尝试了应用程序启动的所有内容,但是在片段的列表视图中长按没有任何反应, 谢谢你的考虑
contextmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/edit"
android:title="@string/add"
app:showAsAction="never" />
<item android:id="@+id/delete"
android:title="@string/delete"
app:showAsAction="never" />
<item android:id="@+id/share"
android:title="@string/share"
app:showAsAction="never" />
</menu>
MainActivity
package ivano.android.com.ucanote;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.framebig, new FragmentAsList())
.commit();
}
}//end on Create
@Override
public boolean onCreateOptionsMenu (Menu menu){
// Inflate the menu; this adds items to the action bar if it is `
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ContextMenuFloating(在Fragment中我无法添加两个contexmenu方法,因此我附加了另一个扩展的类)
/**
* Created by ivano on 3/26/2015.
*/
package ivano.android.com.ucanote;
import android.app.ListActivity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class ContextMenuFloating extends ListActivity {
/**
* Created by ivano on 3/26/2015.
*/
@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.edit:
return true;
case R.id.delete:
return true;
case R.id.share:
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Fragmentaslist
package ivano.android.com.ucanote;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by ivano on 3/25/2015.
*/
public class FragmentAsList extends Fragment {
private ArrayAdapter<String> mValues;
public FragmentAsList() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// // Add this line in order for this fragment to handle menu events.
// setHasOptionsMenu(true);
}
@Override
public String toString() {
return super.toString();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragmentaslist, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// // Handle action bar item clicks here. The action bar will
// // automatically handle clicks on the Home/Up button, so long
// // as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_refresh) {
// FetchWeatherTask weatherTask = new FetchWeatherTask();
// weatherTask.execute("94043");
// return true;
// }
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
String[] toDo = {
"vai che vincerai - Sunny - Shakespeare",
"dormire - dolce dormire - anonimo",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare",
"vai che vincerai - Sunny - Shakespeare"
};
List<String> tasks = new ArrayList<String>(Arrays.asList(toDo));
mValues =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.textview_pretty_cool, // The name of the layout ID.
R.id.text_row, // The ID of the textview to populate.
tasks);
View hiddenList = inflater.inflate(R.layout.fragment_as_list, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) hiddenList.findViewById(R.id.list_hidden);
registerForContextMenu(listView);
listView.setAdapter(mValues);
return hiddenList;
}
}
答案 0 :(得分:0)
尝试setHasOptionsMenu(true);在片段中