Android 在扫描选项卡中插入ListView
晚上好,伙计们,我正在创建一个聊天应用程序,我正在插入一个片段适配器,用于为其主类放置清理选项卡,但我需要我的选项卡在每个选项卡中都有listview,所以我需要它们扩展listview和Fragment,以便正常执行,因此我需要有经验的家伙的一些帮助,如果你知道我有什么诀窍或方法可以做到这一点,我真的很感激。 顺便说一下,这是我的适配器:Android 在扫描选项卡中插入ListView,android,listview,fragment,Android,Listview,Fragment,晚上好,伙计们,我正在创建一个聊天应用程序,我正在插入一个片段适配器,用于为其主类放置清理选项卡,但我需要我的选项卡在每个选项卡中都有listview,所以我需要它们扩展listview和Fragment,以便正常执行,因此我需要有经验的家伙的一些帮助,如果你知道我有什么诀窍或方法可以做到这一点,我真的很感激。 顺便说一下,这是我的适配器: public class TabsPagerAdapter extends FragmentPagerAdapter { public TabsPager
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
这是我主要活动的一部分
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
break;
case R.id.menu_all:
tours = datasource.findAll();
refreshDisplay();
break;
case R.id.menu_cheap:
tours = datasource.findFiltered("price <= 300", "price ASC");
refreshDisplay();
break;
case R.id.menu_fancy:
tours = datasource.findFiltered("price >= 1000", "price DESC");
refreshDisplay();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@覆盖
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.菜单\u设置:
Intent Intent=新Intent(这是setingsactivity.class);
星触觉(意向);
打破
案例R.id.菜单\u全部:
tours=datasource.findAll();
刷新显示();
打破
案例R.id.菜单\u便宜:
tours=datasource.findFiltered(“price=1000”,“price DESC”);
刷新显示();
打破
违约:
打破
}
返回super.onOptionsItemSelected(项目);
}
谢谢您可以扩展ListFragment而不是Fragment。这将帮助您使用列表构建片段 您可以查看以下文档 所以你需要做一些类似的事情:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListFragment;
public class MyListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.app.ListFragment;
公共类MyListFragment扩展了ListFragment{
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”};
ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u 1,值);
setListAdapter(适配器);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//对数据做点什么
}
}
有一个类叫做ListFragment,为什么不扩展ListFragment?我不知道!Thanksys,非常好,非常感谢:)我想没有办法把一个活动放在选项卡上正确吗?是的,当然,不用使用GamesFragment来扩展片段,你可以创建一个新的类,例如称为List并使它扩展ListFragment。