Listview 操作栏、选项卡视图和列表视图
我一直在从事一个项目,我将使用Listview 操作栏、选项卡视图和列表视图,listview,android-actionbar,tabview,sliding,Listview,Android Actionbar,Tabview,Sliding,我一直在从事一个项目,我将使用操作栏、滑动选项卡视图和部分列表视图 我在Android SDK中实现了默认的TabView,其中它生成了三个选项卡,每个选项卡都生成了一个TextView,包括选项卡的数量(即1、2和3) 但我面临的问题是,我无法在我的应用程序中集成部分列表视图 以下是我创建滑动选项卡项目时Android SDK自动生成的代码: package my.project.sectionlisttab; import android.app.ActionBar; import and
操作栏
、滑动选项卡视图和部分列表视图
我在Android SDK中实现了默认的TabView,其中它生成了三个选项卡,每个选项卡都生成了一个TextView
,包括选项卡的数量(即1、2和3)
但我面临的问题是,我无法在我的应用程序中集成部分列表视图
以下是我创建滑动选项卡项目时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;
}
}
}
下面是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);
}
}
}
打包my.project.sectionlisttab;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.BaseAdapter;
公共类SectionListAdapter扩展了BaseAdapter{
公共类索引{
公共情报科;
公共int row;
}
公共接口部分ListAdapterAdapterDelegate{
公共int sectionCount();
公共综合区(综合区);
ROWATINDEXPATH(IndexPath路径)的公共视图;
标题部分(内部部分)的公共视图;
public void itemSelectedAtIndexPath(IndexPath路径);
}
公共部分列表适配器适配器代表;
公共OnItemClickListener项ClickListener=新建OnItemClickListener(){
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
IndexPath路径=SectionListAdapter.this.indexPathForPosition(位置);
如果(path.row>=0)
{
delegate.itemSelectedAtIndexPath(路径);
}
}
};
public int getCount(){
整数计数=0;
int sections=delegate.sectionCount();
对于(int i=0;i=0)
{
位置-=(行+1);
}
其他的
{
path.section=i;
path.row=位置-1;
返回路径;
}
}
返回路径;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
IndexPath path=indexPathForPosition(位置);
如果(path.row==-1)
{
返回delegate.viewForHeaderInSection(path.section);
}
其他的
{
返回delegate.viewForRowAtIndexPath(路径);
}
}
}
有人帮忙吗?
我只是想了解如何将两个适配器或活动集成在一起:S您应该提供更多详细信息,您尝试了什么(代码是最好的),什么不起作用(如果可能,完整的错误消息),以及您希望它做什么。请查看帖子,我已经包含了代码:)
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);
}
}
}