在选项卡式 android 程序中显示列表
Posted
技术标签:
【中文标题】在选项卡式 android 程序中显示列表【英文标题】:Displaying a list in a tabbed android program 【发布时间】:2015-10-12 01:43:06 【问题描述】:我有一个包含 3 个选项卡的程序,但我无法让它在第二个插槽中正确显示。我已经能够在其他选项卡下显示一些很好的东西,但我无法看到这个列表。我已经让它在没有标签的情况下自行工作,所以如果有人能告诉我为什么它不会显示为基于可滑动标签的程序的一部分,这会有所帮助。
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<TextView
android:id="@+id/shiftid"
android:layout_
android:layout_
android:layout_weight="1" />
<TextView
android:id="@+id/datetime"
android:layout_
android:layout_
android:layout_weight="0.5" />
<TextView
android:id="@+id/starttime"
android:layout_
android:layout_
android:layout_weight="1" />
<TextView
android:id="@+id/endtime"
android:layout_
android:layout_
android:layout_weight="1" />
</LinearLayout>
listmain_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#FAA000">
<ListView
android:id="@+id/listView1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
android:layout_ android:layout_
tools:context=".MainActivity" />
ListActivity.java // 这样它就会把它识别为一个标签页
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ListActivity extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.listmain_layout, container, false);
return view;
ListViewAdapter.java //我建立hashmap的地方
import static onyx.shiftcreator.Constants.FIRST_COLUMN;
import static onyx.shiftcreator.Constants.SECOND_COLUMN;
import static onyx.shiftcreator.Constants.THIRD_COLUMN;
import static onyx.shiftcreator.Constants.FOURTH_COLUMN;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list)
super();
this.activity=activity;
this.list=list;
@Override
public int getCount()
// TODO Auto-generated method stub
return list.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return list.get(position);
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null)
convertView=inflater.inflate(R.layout.list_layout, null);
txtFirst=(TextView) convertView.findViewById(R.id.shiftid);
txtSecond=(TextView) convertView.findViewById(R.id.datetime);
txtThird=(TextView) convertView.findViewById(R.id.starttime);
txtFourth=(TextView) convertView.findViewById(R.id.endtime);
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
ListDisplay.java // 我加载和显示 Hashmap 内容的地方
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class ListDisplay extends Activity
private ArrayList<HashMap<String, String>> list;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.listmain_layout);
ListView listView=(ListView)findViewById(R.id.listView1);
list=new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, "Ankit Karia");
temp.put(SECOND_COLUMN, "Male");
temp.put(THIRD_COLUMN, "22");
temp.put(FOURTH_COLUMN, "Unmarried");
list.add(temp);
HashMap<String,String> temp2=new HashMap<String, String>();
temp2.put(FIRST_COLUMN, "Rajat Ghai");
temp2.put(SECOND_COLUMN, "Male");
temp2.put(THIRD_COLUMN, "25");
temp2.put(FOURTH_COLUMN, "Unmarried");
list.add(temp2);
HashMap<String,String> temp3=new HashMap<String, String>();
temp3.put(FIRST_COLUMN, "Karina Kaif");
temp3.put(SECOND_COLUMN, "Female");
temp3.put(THIRD_COLUMN, "31");
temp3.put(FOURTH_COLUMN, "Unmarried");
list.add(temp3);
ListViewAdapter adapter=new ListViewAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
int pos=position+1;
Toast.makeText(ListDisplay.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
);
MainActivity.java //这里我在一个tab下建立页面位置 Main管理actionbar
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener
/**
* The @link android.support.v4.view.PagerAdapter that will provide
* fragments for each of the sections. We use a
* @link FragmentPagerAdapter derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* @link android.support.v4.app.FragmentStatePagerAdapter.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The @link ViewPager that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
@Override
public void onPageSelected(int position)
actionBar.setSelectedNavigationItem(position);
);
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
/**
* A @link FragmentPagerAdapter that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter
public SectionsPagerAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int position)
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//return PlaceholderFragment.newInstance(position + 1);
if(position == 0)
return new SyncActivity();
if(position == 1)
return new ListActivity();
if(position == 2)
return new EntryActivity();
return null;
@Override
public int getCount()
// Show 3 total pages.
return 3;
@Override
public CharSequence getPageTitle(int position)
Locale l = Locale.getDefault();
switch (position)
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
return null;
任何帮助将不胜感激
【问题讨论】:
【参考方案1】:在您的片段中,您必须设置列表及其适配器,如下所示:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.listview, container, false);
lv = (ListView)rootView.findViewById(R.id.listview);
adapter = new ListViewAdapter(getActivity(), R.layout.listviewlayout, new ArrayList<YourObject>());
lv.setAdapter(adapter);
return rootView;
您仍然需要填写列表。这可以通过 AsyncTask 正常完成。
【讨论】:
【参考方案2】:我还建议从 Activity 切换到 ListActivity。这可以简化您的布局。确切地说,您不需要列表本身的任何布局。同样适用于片段。如果它应该只显示一个列表,请使用 ListFragment。
【讨论】:
以上是关于在选项卡式 android 程序中显示列表的主要内容,如果未能解决你的问题,请参考以下文章