2016-10-21 70 views
0

我正在做一個示例項目作爲練習,並希望在處理平板電腦(7'和10')時在同一活動中顯示兩個片段。使用兩個片段的平板電腦的佈局

所以我到目前爲止是這樣的。

enter image description here

正如你看到的,我可以在左側(靜態)片段顯示我recyclerview的數據。然而,正確的片段是空的。

所以我有兩個問題。

1)如何在默認情況下在右邊的片段顯示recyclerview的第一行?(即圖像和文章)

2)如何實現點擊收聽並更新正確的片段的數據?

這裏是我的代碼:

MainActivity

public class MainActivity extends AppCompatActivity { 
private boolean mTwoPane; 
private static final String DETAIL_FRAGMENT_TAG = "DFTAG"; 

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

    if(findViewById(R.id.detailed_match_reports)!=null) { 
     mTwoPane = true; 

     if (savedInstanceState == null) { 
      getSupportFragmentManager() 
        .beginTransaction() 
        .replace(R.id.detailed_match_reports, new DetailedActivityFragment(),DETAIL_FRAGMENT_TAG) 
        .commit(); 
     }else{ 
      mTwoPane = false; 
     } 
    } 
    } 

}

佈局/ activity_main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/match_reports" 
android:name="theo.testing.androidservices.fragments.MainActivityFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginLeft="16dp" 
android:layout_marginRight="16dp" 
tools:context="theo.testing.androidservices.activities.MainActivity" 
tools:layout="@android:layout/list_content" /> 

佈局sw600dp/activity_main

<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" 
android:baselineAligned="false" 
android:divider="?android:attr/dividerHorizontal" 
android:orientation="horizontal" 
tools:context="theo.testing.androidservices.activities.MainActivity"> 

<fragment 
    android:id="@+id/match_reports" 
    android:name="theo.testing.androidservices.fragments.MainActivityFragment" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    tools:layout="@android:layout/list_content" /> 

<FrameLayout 
    android:id="@+id/detailed_match_reports" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="4" /> 


</LinearLayout> 

MainActivityFragment

public class MainActivityFragment extends Fragment { 
public static final String TAG = "AelApp"; 
public static ArrayList<MyModel> listItemsList; 
RecyclerView myList; 
public static MatchReportsAdapter adapter; 

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

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    updateMatchReport(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    getActivity().setTitle("Match Report"); 

    View rootView = inflater.inflate(R.layout.fragment_main_activity, container, false); 
    listItemsList = new ArrayList<>(); 

    myList = (RecyclerView)rootView.findViewById(R.id.listview_match_reports); 
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); 
    myList.setHasFixedSize(true); 
    myList.setLayoutManager(linearLayoutManager); 
    adapter = new MatchReportsAdapter(getActivity(), listItemsList); 
    myList.setAdapter(adapter); 

    return rootView; 
} 

public void updateMatchReport(){ 
    Intent i = new Intent(getActivity(), MatchReport.class); 
    getActivity().startService(i); 
} 

} 
+2

教程和示例應用程序在https://developer.android.com/training/basics/fragments/index.html – CSmith

回答

0

首先讓我回答第二個問題。要顯示在屏幕的右側的片段添加這樣的事情(注意我如何使用框架佈局的那個ID替換片段):現在

Fragment fragment = new MyRightSideFragment(); 
FragmentManager fm = getFragmentManager(); 
fm.beginTransaction().replace(R.id.detailed_match_reports, fragment).commit(); 

,對於第一個問題,你需要要實現點擊監聽器,可以找到一個示例here

public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 
     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int position = (Integer)v.getTag(); 
       showDetail(position); 
      } 
     }); 
    } 
} 

請注意,我使用的視圖的標籤來識別位置(所以somwhere在onBindViewHolder代碼,您必須設置此標記)。

public function showDetail(int position) { 
    ... show fragment por position 
} 

最後,當你在你的OnCreateView或在某處你的設置代碼,調用showDetail(0)

+0

感謝您的答案。你能告訴我請在showDetail(int position)方法裏面發生什麼? – Theo

+0

在showDetail()中你必須放置代碼來顯示片段,這是我在那裏寫的第一塊代碼。 – Merlevede

+0

我把它放在我的適配器中。然而,getSupportManager是未知的:(。public void showDetail(int position){ Fragment fragment = new DetailedActivityFragment(); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction()。替換(R.id.detailed_match_reports,fragment).commit(); } – Theo