gpt4 book ai didi

java - ViewPager fragment 中的 ListView 为空白

转载 作者:行者123 更新时间:2023-12-01 12:58:02 25 4
gpt4 key购买 nike

我在 ViewPager fragment 中有一个 ListView,当前应该由字符串数组填充,但它完全是空白的。这是该 fragment 的代码:

public class DirectoryFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_directory, container, false);

ListView listView = (ListView) rootView.findViewById(R.id.list);

String[] values = new String[] {"Viewer 1",
"Viewer 2",
"Viewer 3"
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values);

listView.setAdapter(adapter);

return rootView;
}

}

fragment 的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff8400" >

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp">
</ListView>


</RelativeLayout>

和MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

//Tab Titles
private String[] tabs = { "sfn Viewer", "Directory", "Help" };

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

//Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//On changing page, make sure right tab is selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}

@Override
public void onPageScrollStateChanged(int arg0) {}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//When the tab is selected show the correct fragment
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

}
}

我在 DirectoryFragment 选项卡中期望的是一个带有字符串 Viewer 1、Viewer 2 和 Viewer 3 的 ListView

提前致谢!

最佳答案

您的 ListViewlayout_height 在 XML 中为 0dp。将其更改为 match_parent 并删除 layout_weight,因为这在 RelativeLayout 中没有效果。

关于java - ViewPager fragment 中的 ListView 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com