嗨,大家好我要显示一个ListView(下面的代码)
在片段里面!
怎么做。
我已经搜索过但我没有找到解决这个问题的任何东西。
谢谢大家的帮助:-D
ExperimentsList.java:
public class ExperimentsList extends AppCompatActivity {
public ListView lv1;
public String[] listentext = {"Cola&Mentos","2","3","4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experiments_list);
android.support.v7.app.ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setDisplayHomeAsUpEnabled(true);
lv1 = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> listenadapter = new ArrayAdapter<>(ExperimentsList.this, android.R.layout.simple_list_item_1, listentext);
lv1.setAdapter(listenadapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
switch (lv1.getPositionForView(view)) {
case 0:{
Intent myIntent = new Intent(ExperimentsList.this, ColaMentos.class);
final int result = 1;
ExperimentsList.this.startActivity(myIntent);
}
}
}
});
}
}
答案 0 :(得分:0)
我假设您要在选项卡下的viewpager中显示列表。 为此,您可以获取viewpager并为其设置FragmentPagerAdaptor,它从getItem方法返回ListFragment的实例。
mViewPager = (ViewPager) findViewById(R.id.container);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
在寻呼机适配器中:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
return MyListFragment.newInstance("");
}
.....
.....
}
您可以编写ListFragment的实现来显示列表数据。
答案 1 :(得分:0)
您应该将activity_experiments_list
更改为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/one_frag"
android:name="biz.progmar.testapp.ListViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
添加ListViewFragment.class:
public class ListViewFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] listentext = {"Cola&Mentos","2","3","4"};
ArrayAdapter<String> listenadapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listentext);
//load the simple_layout.xml which contains only one ListView.
View view = inflater.inflate(R.layout.simple_layout,container,false);
//use id="lv1" to capture the ListView
ListView listView = (ListView) view.findViewById(R.id.lv1);
//set the adapter
listView.setAdapter(listenadapter);
return view;
}
}
让inflater膨胀simple_layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
然后,您可以在活动布局中看到它还有一个ListView