簡體   English   中英

使用兩個片段的平板電腦布局

[英]Layout for tablets using two fragments

我正在做一個樣本項目作為練習,並且想要在處理數位板(7'和10')時在同一活動中顯示兩個片段。

所以我到目前為止是這個。

在此處輸入圖片說明

如您所見,我可以在左側(靜態)片段中顯示recyclerview的數據。 但是正確的片段是空的。

所以我有兩個問題。

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

2)如何實現點擊監聽器並更新正確的片段?

這是我的代碼:

主要活動

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;
        }
     }
  }

}

layout / 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" />

layout-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);
 }

}

首先讓我回答第二個問題。 要在屏幕右側顯示片段,請添加以下內容(請注意我如何使用框架布局的ID替換片段):

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

現在,對於第一個問題,您需要實現點擊監聽器,您可以在此處找到一個示例。

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);
            }
        });
    }
}

請注意,我正在使用視圖的標記來標識位置(因此,在onBindViewHolder代碼中的某些位置,您必須設置此標記)。

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

最后,當您在OnCreateView中或設置代碼中的某個位置時,調用showDetail(0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM