Here are the required code. Create these files and put respective code in them in Android Studio. Data is mock. Put your own data accordingly. Feel free to reach out in case of any issues.
MainActivity.java:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentFragment contentFragment = new ContentFragment(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.top_frame, new CityFragment(contentFragment)); transaction.commit(); transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.bottom_frame, contentFragment); transaction.commit(); } }
CityFragment.java:
/** * A fragment representing a list of Items. */ public class CityFragment extends Fragment { private OnListFragmentInteractionListener mListener; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). */ public CityFragment() { } CityFragment(ContentFragment contentFragment) { mListener = contentFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_city_list, container, false); // Set the adapter if (view instanceof RecyclerView) { Context context = view.getContext(); RecyclerView recyclerView = (RecyclerView) view; recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setAdapter(new MyCityRecyclerViewAdapter( Arrays.asList(context.getResources().getStringArray(R.array.cities)), mListener )); } return view; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. */ public interface OnListFragmentInteractionListener { void onListFragmentInteraction(int position); } }
ContentFragment.java:
public class ContentFragment extends Fragment implements CityFragment.OnListFragmentInteractionListener { private View view = null; private List<String> descriptions; public ContentFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment view = inflater.inflate(R.layout.fragment_content, container, false); descriptions = Arrays.asList(view.getContext().getResources().getStringArray(R.array.descriptions)); return view; } @Override public void onListFragmentInteraction(int position) { if(null != view) ((TextView)view.findViewById(R.id.description)).setText(descriptions.get(position)); } }
MyCityRecyclerViewAdapter:
public class MyCityRecyclerViewAdapter extends RecyclerView.Adapter<MyCityRecyclerViewAdapter.ViewHolder> { private final List<String> mValues; private final OnListFragmentInteractionListener mListener; MyCityRecyclerViewAdapter(List<String> items, OnListFragmentInteractionListener listener) { mValues = items; mListener = listener; } @NonNull @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.fragment_city, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.mIdView.setText(String.valueOf(position)); holder.mContentView.setText(mValues.get(position)); holder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != mListener) { // Notify the active callbacks interface (the activity, if the // fragment is attached to one) that an item has been selected. mListener.onListFragmentInteraction(position); } } }); } @Override public int getItemCount() { return mValues.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { final View mView; final TextView mIdView; final TextView mContentView; ViewHolder(View view) { super(view); mView = view; mIdView = view.findViewById(R.id.item_number); mContentView = view.findViewById(R.id.content); } @NonNull @Override public String toString() { return super.toString() + " '" + mContentView.getText() + "'"; } } }
AndroidManifest.xml (only change the activity tag to be like this and not the whole file):
<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
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:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <FrameLayout android:id="@+id/top_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="12dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:background="#004" /> <FrameLayout android:id="@+id/bottom_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
fragment_city.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/item_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin" android:textAppearance="?attr/textAppearanceListItem" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin" android:textAppearance="?attr/textAppearanceListItem" /> </LinearLayout>
fragment_city_list.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.recyclerview.widget.RecyclerView 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/list" android:name="com.example.myapplication.CityFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutManager="LinearLayoutManager" tools:context=".CityFragment" tools:listitem="@layout/fragment_city" />
fragment_content.xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:textSize="20sp" /> </ScrollView>
strings.xml:
<resources> <string name="app_name">Tour Guide</string> <array name="cities"> <item>Calgary</item> <item>Victoria</item> <item>Vancouvar</item> <item>Another city</item> </array> <array name="descriptions"> <item>Description of the city Calgary</item> <item>Description of the city Victoria</item> <item>Again here would be the description</item> <item>Keep in mind to have same number of items in both of these arrays else app will crash</item> </array> </resources>
5554:Nexus 4 APL23 25:31 App Description In this lab, you will develop a Tour guide App....