使用视图寻呼机的Android滑动视图

时间:2014-05-25 19:37:59

标签: android android-viewpager swipeview

基于Android创建带选项卡的滑动视图

http://developer.android.com/training/implementing-navigation/lateral.html

如何使用自定义适配器获取listview并在此示例中使用?我的意思是显示简单的textview,显示为。

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

  </RelativeLayout>

结果应该是Play商店应用程序。

感谢。

1 个答案:

答案 0 :(得分:2)

您可以尝试查看我的其他帖子:

Android List view layout Similar to Google play

Unable to use AndroidDrawer (sidebar like facebook)

也许这就是你要找的东西:

How to use swipe gesture in a view pager's page with android?

试试这些链接..可能这就是你要找的东西。

以下是更新

1)制作适配器以处理您的碎片:

public class MyAdapter extends FragmentStatePagerAdapter {

    public MyAdapter(FragmentManager fm) {

        super(fm);

    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            return new FirstFragment();

        case 1:

            return new SecondFragment();
        case 2:

            return new ThirdFragment();

        }
        return null;

    }

    @Override
    public int getCount() {
        return 3;
    }

}

2)添加片段:我只是添加一个片段的代码。你可以这样做。

public class FirstFragment extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.somefragment, container, false);//You will have your custom layout and add more items. 

    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    TextView tv = (TextView)getActivity().findViewById(R.id.textview1);
    //You can add different items in here according to your needs. 
    //You will have your own code to do stuff. Like to use a listview or a gridview etc. You will make a separate adapter in a separate class and use that adapter in this onActivityCreated() method.
}


}

3)修改MainActivity:

public class AllActivities extends FragmentActivity implements ActionBar.TabListener {

    public ViewPager viewPager;
    private MyAdapter mAdapter;
    private ActionBar actionBar;
    private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment"};

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

        //Initializing all stuff
        viewPager = (ViewPager)findViewById(R.id.pager);

        actionBar = getActionBar();
        mAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



        //Add the tabs here
        for(String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
        public void onPageSelected(int position){

                //on Page change, that particular page should be selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
                public void onPageScrolled(int arg0,float arg1,int arg2){

            }
            @Override
        public void onPageScrollStateChanged(int position){

            }

        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {



    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {

        viewPager.setCurrentItem(tab.getPosition());

    }
}

实施此示例后,您不必担心此MainActivity。您需要添加更多片段的是将片段添加到适配器并根据该构建页面。另外,添加private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment","You other fragments when you will define"};

要使谷歌像导航抽屉一样玩,你可以参考我的前两个链接,然后随意工作。

希望这个有助于你所寻找的东西。