(╯°□°)╯︵ ┻━┻
parent
78825de38c
commit
110d6197d1
@ -1,26 +1,10 @@
|
||||
package com.calebfontenot.crimeactivity;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
public class CrimeActivity extends AppCompatActivity {
|
||||
|
||||
public class CrimeActivity extends SingleFragmentActivity{
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
|
||||
|
||||
if (fragment == null) {
|
||||
fragment = new CrimeFragment();
|
||||
fm.beginTransaction()
|
||||
.add(R.id.fragment_container,
|
||||
fragment).commit();
|
||||
}
|
||||
protected Fragment createFragment() {
|
||||
return new CrimeFragment();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
package com.calebfontenot.crimeactivity;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CrimeLab {
|
||||
private static CrimeLab sCrimeLab;
|
||||
private List<Crime> mCrimes;
|
||||
public static CrimeLab get(Context context) {
|
||||
if (sCrimeLab == null) {
|
||||
sCrimeLab = new CrimeLab(context);
|
||||
}
|
||||
return sCrimeLab;
|
||||
}
|
||||
private CrimeLab(Context context) {
|
||||
mCrimes = new ArrayList<>();
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
Crime crime = new Crime();
|
||||
crime.setmTitle("Crime #" + i);
|
||||
crime.setmSolved(i % 2 == 0);
|
||||
mCrimes.add(crime);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Crime> getCrimes() {
|
||||
return mCrimes;
|
||||
}
|
||||
|
||||
public Crime getCrime(UUID id) {
|
||||
for (Crime crime: mCrimes) {
|
||||
if (crime.getmId().equals(id)) {
|
||||
return crime;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.calebfontenot.crimeactivity;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
public class CrimeListActivity extends SingleFragmentActivity {
|
||||
public CrimeListActivity() {
|
||||
createFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Fragment createFragment() {
|
||||
return new CrimeListFragment();
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.calebfontenot.crimeactivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CrimeListFragment extends Fragment {
|
||||
private RecyclerView mCrimeRecyclerView;
|
||||
private CrimeAdapter mAdapter;
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
|
||||
mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
|
||||
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
updateUI();
|
||||
return view;
|
||||
}
|
||||
private void updateUI() {
|
||||
CrimeLab crimeLab = CrimeLab.get(getActivity());
|
||||
List<Crime> crimes = crimeLab.getCrimes();
|
||||
mAdapter = new CrimeAdapter(crimes);
|
||||
mCrimeRecyclerView.setAdapter(mAdapter);
|
||||
}
|
||||
private static class CrimeHolder extends RecyclerView.ViewHolder {
|
||||
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
|
||||
super(inflater.inflate(R.layout.list_item_crime, parent, false));
|
||||
}
|
||||
}
|
||||
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
|
||||
private List<Crime> mCrimes;
|
||||
public CrimeAdapter(List<Crime> crimes) {
|
||||
mCrimes = crimes;
|
||||
}
|
||||
@NonNull
|
||||
@Override
|
||||
public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
|
||||
return new CrimeHolder(layoutInflater, parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull CrimeHolder holder, int position) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mCrimes.size();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.calebfontenot.crimeactivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
public abstract class SingleFragmentActivity extends AppCompatActivity {
|
||||
protected abstract Fragment createFragment();
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_fragment);
|
||||
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
|
||||
if (fragment == null) {
|
||||
fragment = createFragment();
|
||||
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.v7.widget.RecyclerView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/crime_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="8dp">
|
||||
<TextView
|
||||
android:id="@+id/crime_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/crime_title"/>
|
||||
<TextView
|
||||
android:id="@+id/crime_date"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
Loading…
Reference in New Issue