Это то, чего я достиг без фрагментов.
Снимок экрана просмотра карты
Теперь я хочу добиться того же результата, используя фрагменты.
Это мой MainActivity.java
public class MainActivity extends AppCompatActivity
implements Recycler_View_Fragment.OnFragmentInteractionListener,
Row1.OnFragmentInteractionListener
{
//Create the Fragment Manager
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//populate the fragment manager
fm = getSupportFragmentManager();
/*
Step 4 Check that the Recycler_View_Fragment has not already been created.
Otherwise create it
*/
if(savedInstanceState == null)
{
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.content, new Recycler_View_Fragment());
transaction.commit();
}
}
@Override
public void onFragmentInteraction(Uri uri)
{
}
}
Это мой activity_main.xml и его предварительный просмотр
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/content"
android:layout_height="match_parent"
tools:context=".MainActivity">
</LinearLayout>
Это Recycler_View_Fragment.java
public class Recycler_View_Fragment extends Fragment
{
FragmentManager fm;
private OnFragmentInteractionListener mListener;
public Recycler_View_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycler__view_, container, false);
fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.row_1_layout,new Row1());
transaction.addToBackStack(null);
transaction.commit();
return view;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Это фрагмент_recycler_view_.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="vertical"
tools:context=".Recycler_View_Fragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</RelativeLayout>
Это Row1.java
public class Row1 extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;
private ArrayList<String> mDataset;
private OnFragmentInteractionListener mListener;
public Row1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
mDataset = new ArrayList<>();
for (int i = 0; i < 10; i++) {
mDataset.add("News " + i);
}
View view = inflater.inflate(R.layout.fragment_recycler__view_, container, false);
mRecyclerView = view.findViewById(R.id.recyclerView);
mAdapter = new Row1Adapter(mDataset);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public class Row1Adapter extends RecyclerView.Adapter<Row1Adapter.ViewHolder>
{
private ArrayList<String> mDataset;
public Row1Adapter(ArrayList<String> dataset)
{
this.mDataset = dataset;
}
@NonNull
@Override
public Row1Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_row1,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Row1Adapter.ViewHolder holder, int position)
{
holder.mTitle.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView mTitle;
public ViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.title);
}
}
}
}
Это фрагмент_row1.xml и егоПредварительный просмотр
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/row_1_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_margin="12dp"
android:id="@+id/cardView"
android:layout_height="200dp"
android:background="#000">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
Это ОШИБКА Я получаю.
Я пробовал много вещей и искал в Google, но ничего не нашел.