从活动导航到视图寻呼机选项卡

时间:2015-03-04 11:37:44

标签: android

我正在创建一个使用viewpager和PageSlidingTabLibrary的android应用程序。它有4个选项卡,每个选项卡里面都有一个列表视图。当单击列表视图项时,将打开一个新活动并显示该详细信息。单击项目。新活动在操作栏中有一个后退按钮,用于上一个活动(即MainActivity),其中存在选项卡视图。 问题是如果导航到其他活动是在返回MainActivity后从第二个选项卡显示第二个选项卡应该显示但它没有发生它将转到第一个选项卡。我怎么能完成它帮助我。< / p>

这是我的代码

xml layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
    <com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    app:pstsDividerColor="#ff1abc9c"
    app:pstsIndicatorColor="#ff1abc9c"
    app:pstsIndicatorHeight="5dp"
    app:pstsDividerPadding="5dp"
    android:keepScreenOn="true"
    app:pstsShouldExpand="true"
    app:pstsUnderlineColor="#ff1abc9c"
    />
    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" >
    <FrameLayout
        android:id="@+id/content"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        <!--you can put your existing views of your current xml here, so yes your entire xml is now inside this FrameLayout -->
    </FrameLayout>
  </android.support.v4.view.ViewPager>
  </LinearLayout>

和活动文件

public class MainActivity extends FragmentActivity{

ViewPager pager;
PagerSlidingTabStrip tabStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pager= (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    tabStrip= (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabStrip.setViewPager(pager);
    Log.d("msg","Oncreate in  main activity called");
    if(savedInstanceState!=null)
    {
        onRestoreInstanceState(savedInstanceState);
    }


}


public class MyPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.IconTitleProvider
{

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position)
        {
            case 0:
                return "Events";
            case 1:
                return "Users";
            case 2:
                return "Groups";
            case 3:
                return "Settlement";
        }


        return null;
    }
    @Override
    public int getCount() {
        return 4;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new EventFragment();
            case 1:
                return new UserFragment();
            case 2:
                return new GroupFragment();
            case 3:
                return new SettlementFragment();
        }
        return null;
    }


    @Override
    public int getPageIconResId(int position) {
        switch(position)
        {
            case 0:
                return R.drawable.event;
            case 1:
                return R.drawable.user;
            case 2:
                return R.drawable.group;
            case 3:
                return R.drawable.settlement;
        }


        return 0;
    }
}
}

片段类

public class EventFragment extends ListFragment {

String[] events={"Trip to Goa","Trip to Ooty"};

public EventFragment() {
    // Required empty public constructor
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ListView eventlist=new ListView(getActivity());
    eventlist.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    eventlist.setId(android.R.id.list);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,events);
    setListAdapter(adapter);

    return eventlist;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("Tabno",1);

}

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id)     {
    super.onListItemClick(l, v, position, id);


        Intent expense = new Intent();
        expense.setClass(getActivity(), ExpenseActivity.class);
        startActivity(expense);
        Bundle eventbundle=new Bundle();
        eventbundle.putInt("Tabno",1);
        onSaveInstanceState(eventbundle);


}
}

3 个答案:

答案 0 :(得分:2)

在您的下一个活动中添加以下代码:

 @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            finish();
            super.onBackPressed();
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
            switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;

            }
            return super.onOptionsItemSelected(item);
        }

答案 1 :(得分:1)

覆盖设备的后退按钮:

@Override
 public void onBackPressed() {
   // TODO Auto-generated method stub
   finish();
   super.onBackPressed();
 }

答案 2 :(得分:0)

只需覆盖活动详情中的后退按钮:

@Override
 public void onBackPressed() {
   // TODO Auto-generated method stub
   finish();
   super.onBackPressed();
 }

它将在viewpager中显示正确的片段