简体   繁体   English

使用两个片段的平板电脑布局

[英]Layout for tablets using two fragments

I am doing a sample project as an excercise and want to display two fragments in the same activity when dealing with tablets(7' and 10'). 我正在做一个样本项目作为练习,并且想要在处理数位板(7'和10')时在同一活动中显示两个片段。

So what I have so far is this. 所以我到目前为止是这个。

在此处输入图片说明

As you can see I can display the data of my recyclerview in the left (static) fragment. 如您所见,我可以在左侧(静态)片段中显示recyclerview的数据。 However the right fragment is empty. 但是正确的片段是空的。

So I have two questions. 所以我有两个问题。

1) How to display by default in the right fragment the data of the first row of recyclerview?(ie image and article) 1)默认情况下如何在右片段中显示recyclerview第一行的数据(即图像和文章)?

2) How to implement the click listener and update the right fragment? 2)如何实现点击监听器并更新正确的片段?

Here is my code: 这是我的代码:

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

} }

layout/activity_main.xml 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 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 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);
 }

}

First let me answer the second question. 首先让我回答第二个问题。 To show a fragment on the right side of the screen add something like this (note how I'm using that id of the frame layout to replace the fragment): 要在屏幕右侧显示片段,请添加以下内容(请注意我如何使用框架布局的ID替换片段):

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

Now, for the first question, you need to implement click listener, you can find an example 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);
            }
        });
    }
}

Notice that I'm using the tag of the view to identify the position (so somwhere in your onBindViewHolder code you must set this tag). 请注意,我正在使用视图的标记来标识位置(因此,在onBindViewHolder代码中的某些位置,您必须设置此标记)。

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

and finally, when your in your OnCreateView or somewhere in your setup code, call showDetail(0) . 最后,当您在OnCreateView中或设置代码中的某个位置时,调用showDetail(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM