1

ActionBar、 、Sliding TabViewおよびを使用するプロジェクトに取り組んでいますSectionListView

Android SDK にデフォルトの TabView を実装しました。3 つのタブが生成され、それぞれに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);
        }

    }
}

助けてくれる人はいますか?2 つのアダプターまたはアクティビティを一緒に統合する方法を理解したいだけです:S

4

0 に答える 0