Android中的列表段



当选择每个列表项目时,如何触发新片段或类打开?基本上,我希望新片段成为YouTube片段。当选择" Android"时,特定的YouTube链接将传递给该片段。同样的是其他列表项目。

import android.R.string;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AddingList extends ListFragment implements OnItemClickListener{
  String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux" };
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

 @Override
 public void onItemClick(AdapterView adapter, View view, int position, long id) {
  Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + values[position], Toast.LENGTH_LONG).show();
   }
}

有一些错误:

import android.R.string;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AddingList extends ListFragment implements OnItemClickListener{
  String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux" };
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

 @Override
 public void onItemClick(AdapterView adapter, View view, int position, long id) {
     FragmentTransaction ft = FragmentManager.beginTransaction();
     ft.replace(R.id.yourcontainer, new Home());
  //Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + values[position], Toast.LENGTH_LONG).show();
   }
}

setonItemClickListner(),然后致电 fragment

喜欢

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.yourcontainer, new YourClass()).commit();

问候

相关内容

  • 没有找到相关文章

最新更新