ActionBar,标签视图和列表视图

时间:2012-10-22 21:05:22

标签: listview android-actionbar tabview sliding

我一直致力于一个项目,我将使用ActionBarSliding TabViewSectionListView

我在Android SDK中实现了默认的TabView,它生成了三个标签,每个标签都生成TextView,包括标签的数量(即1,2和3)。

但是我遇到了一个无法在我的应用中集成SectionListView的问题。

以下是我在创建Sliding Tab项目时由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

0 个答案:

没有答案