如何从列表视图活动导航到选项卡式片段活动

时间:2018-09-01 20:28:37

标签: android listview android-fragments android-tabbed-activity

我是Android编程的新手,需要您的帮助。请。我想做什么。

我创建了列表视图,从创建OnItemClickListener的列表视图到TabbedActivity。

现在,我希望每个listView项在TabbedActivity上显示不同的片段。

这是listView活动

public class BookListActivity extends AppCompatActivity {

    android.support.v7.widget.Toolbar toolbar;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_list);

        toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Best Selling Books by Author");
        listView = (ListView) findViewById(R.id.listView);

        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(SongListActivity.this,
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.bookList));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Intent intent = new Intent(ListViewActivity.this, TabbedActivity.class);
intent.putExtra("BookList", listView.getItemAtPosition(i).toString());
startActivity(intent);
            }
        });


        listView.setAdapter(mAdapter);
    }
}

这是我的选项卡式活动

public class TabbedActivity extends AppCompatActivity {

    private static final String TAG = "HymnActivity";

    private SectionsPageAdapter mSectionsPageAdapter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hymn);
        Log.d(TAG, "onCreate: Starting");

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

//        Bundle bundle = getIntent().getExtras();
//        if(bundle != null) {
////           HOW SHOULD I IMPLEMENT THIS
//        }

    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Book1(), "Book1");
        adapter.addFragment(new Book2(), "Book2");
        adapter.addFragment(new Book3(), "Book3");
        adapter.addFragment(new Book4(), "Book4");
        adapter.addFragment(new Book5(), "Book5");
        adapter.addFragment(new Book6(), "Book6");
        adapter.addFragment(new Book7(), "Book7");
        adapter.addFragment(new Book8(), "Book8");
        adapter.addFragment(new Book9(), "Book9");
        adapter.addFragment(new Book10(), "Book10");

        viewPager.setAdapter(adapter);
    }


}

这是我的SectionsPagerAdapter

public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();


    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }


    public SectionsPageAdapter(FragmentManager fm) {

        super(fm);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

这是我的片段之一

public class Book274 extends Fragment {
    private static final String TAG = "Book274";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.book274, container, false);

        return view;
    }
}

到目前为止,选项卡式片段在我滑过它们时效果很好。但是我不能去特定的幻灯片。它总是从头开始,我有200多个标签页

这就是为什么我要实现listView选项卡。这样一来,我就可以从listView中访问我选择的特定选项卡/片段,并且仍然可以从那里向前或向后滑动。

谢谢 帕皮。

1 个答案:

答案 0 :(得分:0)

传递您想要打开的标签号。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        Intent intent = new Intent(ListViewActivity.this, TabbedActivity.class);
        intent.putExtra("tabNum", i); //which tab number you want to open
        startActivity(intent);
        }
    });

现在,在TabbedActivity(托管片段的活动)中提取意图数据,如下所示:

String tabNum = intent.getExtras().getString("tabNum");

并将viewPager设置为该数字:

viewPager.setCurrentItem(tabNum);