This commit is contained in:
Philipp Wo 2019-03-15 18:08:47 +01:00
parent d8bcefe10e
commit 2105cb8303
8 changed files with 188 additions and 36 deletions

View File

@ -4,51 +4,77 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import at.fhj.airkoality.R;
import at.fhj.airkoality.model.Location;
import at.fhj.airkoality.ui.adapter.LocationListAdapter;
import at.fhj.airkoality.ui.fragment.LocationListFragment;
import at.fhj.airkoality.ui.fragment.MapFragment;
public class MainActivity extends AppCompatActivity {
private Fragment locationListFragment;
private Fragment mapFragment;
private static final String LOCATION_LIST = "location_list";
private static final String MAP = "map";
private BottomNavigationView bottomNavigationView;
public class MainActivity extends AppCompatActivity implements LocationListAdapter.ItemClickListener {
private RecyclerView locationList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationListFragment = new LocationListFragment();
mapFragment = new MapFragment();
locationList = findViewById(R.id.rvLocationList);
List<Location> locations = new ArrayList<>();
bottomNavigationView = findViewById(R.id.bnvMain);
locations.add(new Location("Daheim", "Graz" , "Österreich"));
locations.add(new Location("Daheim2", "Graz" , "Österreich"));
locations.add(new Location("Daheim3", "Graz" , "Österreich"));
locations.add(new Location("Daheim4", "Graz" , "Österreich"));
locations.add(new Location("Daheim5", "Graz" , "Österreich"));
locations.add(new Location("Daheim6", "Graz" , "Österreich"));
locations.add(new Location("Daheim7", "Graz" , "Österreich"));
locations.add(new Location("Daheim8", "Graz" , "Österreich"));
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.flFragmentContainer, locationListFragment);
transaction.add(R.id.flFragmentContainer, mapFragment);
LocationListAdapter adapter = new LocationListAdapter(locations, this);
transaction.commit();
locationList.setAdapter(adapter);
locationList.setLayoutManager(new LinearLayoutManager(this));
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.action_location:
setSelectedFragment(LOCATION_LIST);
break;
case R.id.action_map:
setSelectedFragment(MAP);
break;
}
return false;
}
});
setSelectedFragment(LOCATION_LIST);
}
@Override
public void onItemClicked(Location location, int position) {
Toast.makeText(this, "Clicked " + location, Toast.LENGTH_SHORT).show();
private void setSelectedFragment(String fragmentName){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (fragmentName) {
case LOCATION_LIST:
transaction.hide(mapFragment);
transaction.show(locationListFragment);
break;
case MAP:
transaction.hide(locationListFragment);
transaction.show(mapFragment);
break;
}
transaction.commit();
}
}

View File

@ -0,0 +1,56 @@
package at.fhj.airkoality.ui.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import at.fhj.airkoality.R;
import at.fhj.airkoality.model.Location;
import at.fhj.airkoality.ui.adapter.LocationListAdapter;
public class LocationListFragment extends Fragment implements LocationListAdapter.ItemClickListener {
private RecyclerView locationList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_location_list, container, false);
locationList = view.findViewById(R.id.rvLocationList);
List<Location> locations = new ArrayList<>();
locations.add(new Location("Daheim", "Graz" , "Österreich"));
locations.add(new Location("Daheim2", "Graz" , "Österreich"));
locations.add(new Location("Daheim3", "Graz" , "Österreich"));
locations.add(new Location("Daheim4", "Graz" , "Österreich"));
locations.add(new Location("Daheim5", "Graz" , "Österreich"));
locations.add(new Location("Daheim6", "Graz" , "Österreich"));
locations.add(new Location("Daheim7", "Graz" , "Österreich"));
locations.add(new Location("Daheim8", "Graz" , "Österreich"));
LocationListAdapter adapter = new LocationListAdapter(locations, this);
locationList.setAdapter(adapter);
locationList.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
@Override
public void onItemClicked(Location location, int position) {
Toast.makeText(getContext(), "Clicked " + location, Toast.LENGTH_SHORT).show();
}
}

View File

@ -0,0 +1,24 @@
package at.fhj.airkoality.ui.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import at.fhj.airkoality.R;
public class MapFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
}

View File

@ -22,17 +22,19 @@
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
/>
</android.support.design.widget.BottomNavigationView>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvLocationList"
<FrameLayout
android:id="@+id/flFragmentContainer"
android:layout_above="@id/bnvMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bnvMain"
></android.support.v7.widget.RecyclerView>
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvLocationList"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v7.widget.RecyclerView>
</LinearLayout>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Map Fragment"
android:textSize="25dp"/>
</LinearLayout>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/location" android:title="@string/location" android:icon="@drawable/baseline_location_city_white_24"/>
<item android:id="@+id/map" android:title="@string/map" android:icon="@drawable/baseline_location_on_white_24"/>
<item android:id="@+id/action_location" android:title="@string/location" android:icon="@drawable/baseline_location_city_white_24"/>
<item android:id="@+id/action_map" android:title="@string/map" android:icon="@drawable/baseline_location_on_white_24"/>
</menu>

View File

@ -44,3 +44,19 @@ Liste bekommt einen Adapter und stellt dann nur Listenelemente dar, die gerade a
Card View Layout ausprobiert
Toast Messages implementiert -> Pop up Nachrichten im unteren Bereich des Bildschirms
## 15.03.2019 - Lab 4
RecyclerView -> Items der View wiederverwendbar machen
- vorsicht bei ausgewählten Items (Adapter muss sich Position merken)
### Fragments
- ursprünglich für Tablets entwickelt
- Darstellung von z.B. Listen und Inhalt gleichzeitig
### Storage
- unterschiedliche Möglichkeiten der Speicherung
- local storage (im App Verzeichnis)
- external storage
-