2016-05-17 85 views
0

我一直試圖用列表視圖中的項目被按下時用另一個替換當前片段。但是,當按鈕被按下時,onClick方法被觸發,但它不會替換片段。無法在viewpager中替換片段

JournalFragment.java

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

     rootView = inflater.inflate(R.layout.fragment_journal_view, container, false); 
     context = getContext(); 
     ListView myView = (ListView) rootView.findViewById(R.id.listView); 

     TableControllerUser TCU = new TableControllerUser(context); 
     final TableControllerJournal TCJ = new TableControllerJournal(context); 


     int accID = TCU.getLoggedInId(); 
     Cursor cursor = TCJ.getAllJournals(accID); 
     Cursor allFood = TCJ.getAllFoodJournals(accID); 
     Cursor allActivity = TCJ.getAllActivityJournals(accID); 
     Cursor[] cursors = {cursor, allFood, allActivity}; 

     MergeCursor m = new MergeCursor(cursors); 

     final JournalAdapter adapter = new JournalAdapter(context, m, 0); 

     myView.setAdapter(adapter); 

     myView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Cursor c = (Cursor) adapter.getItem(position); 
       Toast.makeText(getActivity(), "onClick Pressed!", Toast.LENGTH_SHORT).show(); 

       String title = c.getString(c.getColumnIndexOrThrow("title")); 

       if (title.contains("Glucose")) { 
        String glucose = c.getString(c.getColumnIndexOrThrow("glucose")); 
        String dateTime = c.getString(c.getColumnIndexOrThrow("glucose_time")); 
        String journalId = c.getString(c.getColumnIndexOrThrow("_id")); 
        String split[] = dateTime.split(" "); 
        String date = split[0]; 
        String time = split[1]; 

        Fragment gluFrag = new GlucoseFragment(); 

        Bundle bundle = new Bundle(); 
        bundle.putString("time", time); 
        bundle.putString("date", date); 
        bundle.putString("glucose", glucose); 
        bundle.putString("journalId", journalId); 

        gluFrag.setArguments(bundle); 
        ((GraphActivity) getActivity()).replaceFragments(gluFrag, bundle); 
       } 
      } 
     }); 
     return rootView; 
     // Inflate the layout for this fragment 
    } 

} 

JournalFragment.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="edu.tp.sghealthapp.JournalViewFragment" 
    android:id="@+id/jv"> 


     <ListView 
      android:id="@+id/listView" 
      android:layout_below="@+id/viewPager" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:divider="@color/iron" 
      android:dividerHeight="1dp" /> 

</LinearLayout> 

JournalListViewLayout.XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <TextView 
     android:id="@+id/txt_listTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/arrowIcon" 
     android:layout_toStartOf="@+id/arrowIcon" 
     android:textColor="#000000" 
     android:focusable="false" 
     android:textSize="16sp" 
     android:typeface="sans" /> 

    <ImageView 
     android:id="@+id/arrowIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:focusable="false" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_arrow" /> 
</RelativeLayout> 

GraphActivity.java

public class GraphActivity extends AppCompatActivity { 

    Context context; 
    TabLayout mTabLayout; 
    ViewPager mViewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     context = getApplicationContext(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.activity_graph); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setElevation(0); 


     mTabLayout = (TabLayout) findViewById(R.id.tabLayout); 
     mViewPager = (ViewPager) findViewById(R.id.viewPager); 
     ViewPagerAdapter pgAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     pgAdapter.addFragments(new GraphFragment(), "Graph"); 
     pgAdapter.addFragments(new JournalViewFragment(), "Journal"); 
     mViewPager.setAdapter(pgAdapter); 
     mTabLayout.setupWithViewPager(mViewPager); 


    } 

    public void replaceFragments(Fragment newFragment, Bundle bundle) { 
     Fragment fragment = newFragment; 
     FragmentManager fm = getSupportFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     fragment.setArguments(bundle); 
     ft.replace(R.id.jv, fragment); 
     ft.commit(); 

    } 
} 

編輯:

ActivityGraph.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    tools:context=".GraphActivity" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/graph"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/primary" 
     app:tabGravity="fill" 
     app:tabMode="scrollable" 
     app:tabSelectedTextColor="@color/white" 
     app:tabTextColor="@color/black"> 
     <!--To be fixed in future to occupy whole bar when mode = fixed--> 


    </android.support.design.widget.TabLayout> 

    <edu.tp.sghealthapp.library.graphViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager> 





</RelativeLayout> 

EDIT 2

試圖哈倫的一個d Vivek_Neel的方法,他們都工作,但與格式

Messed Up Formatting

把安卓導致此問題:layout_below = 「@ + ID/viewPager」 中的LinearLayout會導致片段不再顯示

EDIT 3

與此https://stackoverflow.com/a/34573034/5509513

+1

是您的if條件執行? –

+0

此ID是exidts R.id.jv嗎? – Haroon

+0

@PradeepGupta if條件被執行,但片段事務不起作用 – Isaac

回答

2

activity_graph.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    tools:context=".GraphActivity" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/graph"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/primary" 
     app:tabGravity="fill" 
     app:tabMode="scrollable" 
     app:tabSelectedTextColor="@color/white" 
     app:tabTextColor="@color/black"> 
     <!--To be fixed in future to occupy whole bar when mode = fixed--> 


    </android.support.design.widget.TabLayout> 

    <edu.tp.sghealthapp.library.graphViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/tabLayout"></edu.tp.sghealthapp.library.graphViewPager> 

<LinearLayout 
    android:id="@+id/jv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 



</RelativeLayout> 
+0

該片段可以被替換,但現在我有一些格式問題: [鏈接](http://i.imgur.com/iEn2LvZ.png) – Isaac

+0

@Isaac我無法檢查您的鏈接,因爲它在這裏被阻止在我的公司。對不起:) –

+0

檢查編輯,我嵌入屏幕截圖 – Isaac

2
<FrameLayout 
    android:id="@+id/jv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
解決格式化問題

將這個代碼剪斷,進入活動,並檢查

+0

你的方法和Vivek_Neel都能產生所需的效果,但格式化存在一些問題,請參考編輯。謝謝! – Isaac

2

嘗試wrap_contentView Pager的高度,並與@+id/jd添加Relative Layout,並給它layout below view pager

請參閱此。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/graph" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/ColorPrimary" 
     app:tabGravity="fill" 
     app:tabMode="scrollable" 
     app:tabSelectedTextColor="#FFF" 
     app:tabTextColor="#000"> 
     <!--To be fixed in future to occupy whole bar when mode = fixed--> 


    </android.support.design.widget.TabLayout> 

    <edu.tp.sghealthapp.library.graphViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tabLayout" /> 

    <RelativeLayout 
     android:id="@+id/jv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/viewPager" /> 


</RelativeLayout> 

編輯1

其次辦法做到這一點創建新的佈局只Frame Layout。並在更換Fragment時使用它的佔位符。

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

    <FrameLayout 
     android:id="@+id/jd" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 

</LinearLayout> 
+0

這不起作用。新的片段不會出現在屏幕上,即使onClick方法被激發:( – Isaac

+0

它會替換並顯示您的片段內容下面查看傳呼檢查它。如果不包裝內容然後給一些修復高度查看尋呼機像100dp。 –

+0

@jayDorider該片段現在出現,但它出現在當前片段的一側。是否有辦法完全替換片段? 我已經在使用fragmentTransaction.replace – Isaac