Hi Guys i want to display a ListView (Code Below) inside a Fragment!
How to do that. I have already Searched but i dont found anything that solved that.
Thank you Guys for help :-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);
}
}
}
});
}
}
I am assuming you want to display a list in the viewpager under the tabs. For this you can get the viewpager and set a FragmentPagerAdaptor to it, which returns an instance of a ListFragment from the getItem method.
mViewPager = (ViewPager) findViewById(R.id.container);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
In pager adaptor:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
return MyListFragment.newInstance("");
}
.....
.....
}
You can write your implementation of ListFragment to show your list data.
You should change your activity_experiments_list
to this:
<?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>
Add 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;
}
}
let the inflater to inflate 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>
Then, you can see in your activity layout,It also has a ListView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.