1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-31 20:11:58 +02:00

Replace ListView with RecyclerView

This commit is contained in:
Erik Johansson
2018-03-10 23:14:58 +01:00
parent 39a5925e7c
commit b0df4fd3aa
3 changed files with 68 additions and 78 deletions

View File

@@ -42,6 +42,7 @@ dependencies {
implementation "com.android.support:design:${supportLibVersion}" implementation "com.android.support:design:${supportLibVersion}"
implementation "com.android.support:support-v4:${supportLibVersion}" implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:appcompat-v7:${supportLibVersion}" implementation "com.android.support:appcompat-v7:${supportLibVersion}"
implementation "com.android.support:recyclerview-v7:${supportLibVersion}"
// HelloCharts // HelloCharts
implementation 'com.github.lecho:hellocharts-library:1.5.8@aar' implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'

View File

@@ -19,17 +19,16 @@ import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout; import android.widget.TableLayout;
import android.widget.TableRow; import android.widget.TableRow;
import android.widget.TextView; import android.widget.TextView;
@@ -48,9 +47,12 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class TableFragment extends Fragment implements FragmentUpdateListener { public class TableFragment extends Fragment implements FragmentUpdateListener {
private View tableView; private View tableView;
private ListView tableDataView;
private LinearLayout tableHeaderView; private LinearLayout tableHeaderView;
private RecyclerView recyclerView;
private MeasurementsAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private List<MeasurementView> measurementViews; private List<MeasurementView> measurementViews;
public TableFragment() { public TableFragment() {
@@ -58,15 +60,19 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
{
tableView = inflater.inflate(R.layout.fragment_table, container, false); tableView = inflater.inflate(R.layout.fragment_table, container, false);
tableDataView = tableView.findViewById(R.id.tableDataView);
tableHeaderView = tableView.findViewById(R.id.tableHeaderView); tableHeaderView = tableView.findViewById(R.id.tableHeaderView);
recyclerView = tableView.findViewById(R.id.tableDataView);
tableDataView.setAdapter(new ListViewAdapter()); recyclerView.setHasFixedSize(true);
tableDataView.setOnItemClickListener(new onClickListenerRow());
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new MeasurementsAdapter();
recyclerView.setAdapter(adapter);
measurementViews = MeasurementView.getMeasurementList( measurementViews = MeasurementView.getMeasurementList(
getContext(), MeasurementView.DateTimeOrder.FIRST); getContext(), MeasurementView.DateTimeOrder.FIRST);
@@ -108,7 +114,6 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
} }
} }
ListViewAdapter adapter = (ListViewAdapter) tableDataView.getAdapter();
adapter.setMeasurements(visibleMeasurements, scaleMeasurementList); adapter.setMeasurements(visibleMeasurements, scaleMeasurementList);
} }
@@ -116,16 +121,15 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
return (int)(dp * getResources().getDisplayMetrics().density + 0.5f); return (int)(dp * getResources().getDisplayMetrics().density + 0.5f);
} }
private class onClickListenerRow implements AdapterView.OnItemClickListener { private class MeasurementsAdapter extends RecyclerView.Adapter<MeasurementsAdapter.ViewHolder> {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(tableView.getContext(), DataEntryActivity.class);
intent.putExtra(DataEntryActivity.EXTRA_ID, (int)id);
startActivity(intent);
}
}
private class ListViewAdapter extends BaseAdapter { public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout measurementView;
public ViewHolder(LinearLayout view) {
super(view);
measurementView = view;
}
}
private List<MeasurementView> visibleMeasurements; private List<MeasurementView> visibleMeasurements;
private List<ScaleMeasurement> scaleMeasurements; private List<ScaleMeasurement> scaleMeasurements;
@@ -143,25 +147,42 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
} }
@Override @Override
public int getCount() { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return scaleMeasurements == null ? 0 : scaleMeasurements.size(); LinearLayout row = new LinearLayout(getContext());
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
final boolean isSmallScreen =
screenSize != Configuration.SCREENLAYOUT_SIZE_XLARGE
&& screenSize != Configuration.SCREENLAYOUT_SIZE_LARGE;
for (int i = 0; i < visibleMeasurements.size(); ++i) {
TextView column = new TextView(getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1);
layoutParams.width = 0;
column.setLayoutParams(layoutParams);
column.setMinLines(2);
column.setGravity(Gravity.CENTER_HORIZONTAL);
if (isSmallScreen) {
column.setTextSize(COMPLEX_UNIT_DIP, 9);
}
row.addView(column);
}
return new ViewHolder(row);
} }
@Override @Override
public Object getItem(int position) { public void onBindViewHolder(ViewHolder holder, int position) {
return scaleMeasurements.get(position); final ScaleMeasurement measurement = scaleMeasurements.get(position);
}
@Override
public long getItemId(int position) {
return scaleMeasurements.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Create entries in stringCache if needed // Create entries in stringCache if needed
if (stringCache[position][0] == null) { if (stringCache[position][0] == null) {
ScaleMeasurement measurement = scaleMeasurements.get(position);
ScaleMeasurement prevMeasurement = null; ScaleMeasurement prevMeasurement = null;
if (position + 1 < scaleMeasurements.size()) { if (position + 1 < scaleMeasurements.size()) {
prevMeasurement = scaleMeasurements.get(position + 1); prevMeasurement = scaleMeasurements.get(position + 1);
@@ -178,49 +199,26 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
} }
} }
// Create view if needed
LinearLayout row;
if (convertView == null) {
row = new LinearLayout(getContext());
final int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
final boolean isSmallScreen =
screenSize != Configuration.SCREENLAYOUT_SIZE_XLARGE
&& screenSize != Configuration.SCREENLAYOUT_SIZE_LARGE;
for (int i = 0; i < visibleMeasurements.size(); ++i) {
TextView column = new TextView(getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1);
layoutParams.width = 0;
column.setLayoutParams(layoutParams);
column.setMinLines(2);
column.setGravity(Gravity.CENTER_HORIZONTAL);
if (isSmallScreen) {
column.setTextSize(COMPLEX_UNIT_DIP, 9);
}
row.addView(column);
}
}
else {
row = (LinearLayout) convertView;
}
// Fill view with data // Fill view with data
LinearLayout row = holder.measurementView;
for (int i = 0; i < visibleMeasurements.size(); ++i) { for (int i = 0; i < visibleMeasurements.size(); ++i) {
TextView column = (TextView) row.getChildAt(i); TextView column = (TextView) row.getChildAt(i);
column.setText(stringCache[position][i]); column.setText(stringCache[position][i]);
} }
return row; row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), DataEntryActivity.class);
intent.putExtra(DataEntryActivity.EXTRA_ID, measurement.getId());
startActivity(intent);
}
});
} }
@Override @Override
public boolean hasStableIds() { public int getItemCount() {
return true; return scaleMeasurements == null ? 0 : scaleMeasurements.size();
} }
} }
} }

View File

@@ -13,18 +13,9 @@
android:paddingTop="5dp"> android:paddingTop="5dp">
</LinearLayout> </LinearLayout>
<LinearLayout <android.support.v7.widget.RecyclerView
android:id="@+id/tableDataView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="0.9" android:scrollbars="vertical" />
android:gravity="bottom"
android:orientation="horizontal">
<ListView
android:id="@+id/tableDataView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="5dp">
</ListView>
</LinearLayout>
</LinearLayout> </LinearLayout>