Android Fragments从左到右或从右到左拖动或移动

时间:2014-04-02 06:05:01

标签: android

我的要求是需要调整或移动从左到右或从右到左的片段。我的主要布局包含两个片段。我需要拖延调整片段宽度的大小,如android设置屏幕/消息(平板电脑上)分隔线从右向左或从左向右移动。

我的应用仅考虑平板电脑。 &安培;这两个片段是独立的(不是主要细节)

courses_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:baselineAligned="false" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        class="com.mobile.courses.CousesFragment" ></fragment>
   <View
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:background="@color/list_divider_color" />
    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        class="com.mobile.whathappenfeed.DiscussionFeedFragment" ></fragment>

</LinearLayout>

CoursesActivity

public class CoursesActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.courses_main);
    }

}

这是我的`CourseFragment.java

    public class CousesFragment extends Fragment {
private List<Course> courses;

private RefreshableListViewContainer refresh;
private RefreshableListView listView;
protected CourseArrayAdapter courseAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    View view = inflater.inflate(R.layout.courses_fragment, container,
            false);

    refresh = (RefreshableListViewContainer) view
            .findViewById(R.id.pull_to_refresh_list);
    listView = (RefreshableListView) refresh
            .findViewById(android.R.id.list);

    // Set a listener to be invoked when the list should be refreshed.
    refresh.setOnRefreshListener(new OnRefreshListener() {
        public void onRefresh(boolean loading) {
            refresh.finishLoading();
            reloadCurrentCourses();
        }
    });



    loadAndDisplayCourses();
    return view;
}

这是我的DiscussionFragment.java

public class DiscussionFeedFragment extends ListFragment implements  OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.discussion_fragment, container,false);

        return view;
    }

这是我的course_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <include
        android:id="@+id/list_header_item"
        layout="@layout/list_header_item" />

     <View
        android:padding="4dip" 
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:background="@color/list_divider_color" />


    <include
        android:id="@+id/empty_courses"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/empty_courses"
        android:visibility="gone" />
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" 
        android:orientation="vertical" >

        <com.mobile.widget.RefreshableListViewContainer
            android:id="@+id/pull_to_refresh_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <com.mobile.widget.RefreshableListViewOverflowItem
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <com.mobile.widget.RefreshableListViewItem
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <com.mobile.widget.RefreshableListView
                android:id="@android:id/list"
                style="@style/MotorolaListViewFooterFix"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@android:color/white"
                android:cacheColorHint="#00000000"
                android:paddingLeft="10dip" 
                android:paddingTop="10dip" 
                android:divider="@drawable/list_item_divider"
                android:dividerHeight="3dp"
                android:drawSelectorOnTop="true"
                android:fadingEdge="none"
                android:smoothScrollbar="true"
                android:fastScrollEnabled="false"
                android:footerDividersEnabled="true"
                android:headerDividersEnabled="true"
                android:listSelector="@drawable/item_background_holo_light"
                android:textFilterEnabled="true" />

        </com.mobile.widget.RefreshableListViewContainer>
    </FrameLayout>

</LinearLayout>

我需要调整片段let to right或right to right。像主/细节或设置屏幕。但我没有创建主要的细节应用程序。怎么做这个部分?

1 个答案:

答案 0 :(得分:0)

onCreate()方法内的活动类中添加此代码段

viewPager=(ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

然后在同一活动类中创建MyPagerAdapter私有类

private class MyPagerAdapter extends  FragmentPagerAdapter{

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position){
            case 0: return new AFragment();
            case 1: return new BFragment();
            default: return null;
            }
        }

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

更新您的活动布局,添加以下内容:

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </android.support.v4.view.ViewPager>