繁体   English   中英

如何在片段内显示Listview

[英]How to display a Listview inside a Fragment

嗨,大家好,我想在一个片段中显示一个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);

                }





            }



        }
    });
}

}

我假设您想在选项卡下的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的实现以显示列表数据。

您应该将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;
    }

}

让充气机充气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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM