2014-04-02 43 views
0

我的需求是需要調整或移動從左到右或從右到左的碎片。我的主要佈局包含兩個片段。我需要拖動&調整片段寬度,如android設置屏幕/消息(在平板電腦上)divider從右向左移動或從左向右移動。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> 

我需要調整片段讓向右或從右到左。像主/細節或設置屏幕。但我沒有創建爲主細節應用程序。如何做這部分?

+0

您使用過ViewPager嗎? –

+0

號請告訴我如何在這裏添加ViewPager? – user3391455

+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>