我一直在做一个项目,我将在其中使用 a ActionBar
、 aSliding TabView
和SectionListView
.
我在 Android SDK 中实现了默认的 TabView,它生成三个选项卡,每个选项卡都有一个TextView
生成的选项卡,包括选项卡的数量(即 1、2 和 3)。
但是我面临一个问题,我无法将 a 集成SectionListView
到我的应用程序中。
以下是我创建滑动选项卡项目时 Android SDK 自动生成的代码:
package my.project.sectionlisttab;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* sections. We use a {@link android.support.v4.app.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;
//Section list identifiers
SectionListAdapter adapter;
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 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
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
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());
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
现在如您所见,代码不允许我扩展 ListActivity。
所以,我想集成以下分段列表代码:
package my.project.sectionlisttab;
import my.project.sectionlisttab.SectionListAdapter.IndexPath;
import my.project.sectionlisttab.SectionListAdapter.SectionListAdapterAdapterDelegate;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class SectionListActivity extends ListActivity implements SectionListAdapterAdapterDelegate {
SectionListAdapter adapter;
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
inflater = LayoutInflater.from(this);
adapter = new SectionListAdapter();
adapter.delegate = this;
getListView().setAdapter(adapter);
getListView().setOnItemClickListener(adapter.itemClickListener);
}
public int sectionCount() {
return 3;
}
public int rowsInSection(int section) {
return 4;
}
public View viewForRowAtIndexPath(IndexPath path) {
View view = inflater.inflate(R.layout.list_row, null);
TextView text = (TextView)view.findViewById(R.id.text);
text.setText("Row " + path.row);
return view;
}
public View viewForHeaderInSection(int section) {
View view = inflater.inflate(R.layout.list_header, null);
TextView text = (TextView)view.findViewById(R.id.text);
text.setText("Section " + section);
return view;
}
public void itemSelectedAtIndexPath(IndexPath path) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, "Row " + path.row + " in section " + path.section + " selected", duration);
toast.show();
}
}
这是 SectionListAdapter:
package my.project.sectionlisttab;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
public class SectionListAdapter extends BaseAdapter {
public class IndexPath {
public int section;
public int row;
}
public interface SectionListAdapterAdapterDelegate {
public int sectionCount();
public int rowsInSection(int section);
public View viewForRowAtIndexPath(IndexPath path);
public View viewForHeaderInSection(int section);
public void itemSelectedAtIndexPath(IndexPath path);
}
public SectionListAdapterAdapterDelegate delegate;
public OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
IndexPath path = SectionListAdapter.this.indexPathForPosition(position);
if(path.row >= 0)
{
delegate.itemSelectedAtIndexPath(path);
}
}
};
public int getCount() {
int count = 0;
int sections = delegate.sectionCount();
for(int i = 0; i < sections; i++)
{
count += delegate.rowsInSection(i);
}
return count + sections;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public IndexPath indexPathForPosition(int position)
{
IndexPath path = new IndexPath();
for(int i = 0; i < delegate.sectionCount(); i++)
{
int rows = delegate.rowsInSection(i);
if(position - rows - 1 >= 0)
{
position -= (rows + 1);
}
else
{
path.section = i;
path.row = position - 1;
return path;
}
}
return path;
}
public View getView(int position, View convertView, ViewGroup parent) {
IndexPath path = indexPathForPosition(position);
if(path.row == -1)
{
return delegate.viewForHeaderInSection(path.section);
}
else
{
return delegate.viewForRowAtIndexPath(path);
}
}
}
有帮助吗?我只想了解如何将两个适配器或活动集成在一起:S