Last active
November 11, 2017 17:04
-
-
Save mik-laj/42d4027f72b66ec42e149e62506dd3d8 to your computer and use it in GitHub Desktop.
Przykład kodu programu dla platformy Android, który wyświetla listę elementów. Jest tutaj zaimplentowane dwie ciekawe rzeczy: a) BetterArrayAdapter, który wymuszę stosowanie wzorca ViewHolder na klasach potomnych, co zwiększa wydajność oraz rozdziela dwie funkcje metody (create i bind) na dwie metody, co usprawnia kod b) wykorzystanie CheckableR…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="com.laboratories.bregula.myapplication.StudentListActivity"> | |
<ListView | |
android:id="@android:id/list" | |
android:layout_width="fill_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1.0" /> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:id="@+id/button_bar_normal" | |
style="?android:buttonBarStyle"> | |
<Button | |
android:id="@+id/button_save" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Add" | |
android:layout_weight="1.0" | |
style="?android:attr/borderlessButtonStyle"/> | |
<Button | |
android:id="@+id/button_remove" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1.0" | |
android:text="Remove" | |
style="?android:attr/borderlessButtonStyle"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:visibility="gone" | |
android:id="@+id/button_bar_remove" | |
style="?android:buttonBarStyle"> | |
<Button | |
android:id="@+id/button_confirm_remove" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Remove" | |
android:layout_weight="1.0" | |
style="?android:attr/borderlessButtonStyle"/> | |
<Button | |
android:id="@+id/button_cancel" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1.0" | |
android:text="Cancel" | |
style="?android:attr/borderlessButtonStyle"/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.laboratories.bregula.myapplication; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import java.util.List; | |
public abstract class BetterArrayAdapter<T, VH extends BetterArrayAdapter.ViewHolder> extends ArrayAdapter<T> { | |
abstract static class ViewHolder { | |
View view; | |
ViewHolder(View v) { | |
this.view = v; | |
} | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource) { | |
super(context, resource); | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource, int textViewResourceId) { | |
super(context, resource, textViewResourceId); | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource, @NonNull T[] objects) { | |
super(context, resource, objects); | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull T[] objects) { | |
super(context, resource, textViewResourceId, objects); | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource, @NonNull List<T> objects) { | |
super(context, resource, objects); | |
} | |
public BetterArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<T> objects) { | |
super(context, resource, textViewResourceId, objects); | |
} | |
@NonNull | |
@Override | |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
VH vh; | |
View v; | |
if (convertView == null) { | |
int viewType = getItemViewType(position); | |
v = createView(parent, viewType); | |
vh = createViewHolder(v); | |
v.setTag(vh); | |
} else { | |
v = convertView; | |
vh = (VH) convertView.getTag(); | |
} | |
T item = getItem(position); | |
bindViewHolder(vh, position, item); | |
return v; | |
} | |
protected abstract View createView(ViewGroup parent, int viewType); | |
protected abstract void bindViewHolder(VH vh, int position, T item); | |
protected abstract VH createViewHolder(View v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.laboratories.bregula.myapplication; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.CheckBox; | |
import android.widget.Checkable; | |
import android.widget.RelativeLayout; | |
/** | |
* Created by andrzej on 06.11.17. | |
*/ | |
public class CheckableRelativeLayout extends RelativeLayout implements Checkable { | |
private CheckBox mCheckbox; | |
public CheckableRelativeLayout(Context context) { | |
super(context); | |
} | |
public CheckableRelativeLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public CheckableRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
mCheckbox = findViewById(android.R.id.checkbox); | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
mCheckbox.setChecked(checked); | |
} | |
@Override | |
public boolean isChecked() { | |
return mCheckbox.isChecked(); | |
} | |
@Override | |
public void toggle() { | |
mCheckbox.toggle(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<com.laboratories.bregula.myapplication.CheckableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="?android:attr/listChoiceBackgroundIndicator" | |
android:minHeight="?attr/listPreferredItemHeight" | |
android:mode="twoLine" | |
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" | |
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft" | |
android:paddingRight="?android:attr/listPreferredItemPaddingRight" | |
android:paddingStart="?android:attr/listPreferredItemPaddingStart"> | |
<TextView | |
android:id="@android:id/text1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentTop="true" | |
android:layout_marginTop="8dp" | |
android:layout_toLeftOf="@android:id/checkbox" | |
android:layout_toStartOf="@android:id/checkbox" | |
android:textAppearance="?attr/textAppearanceListItem" /> | |
<TextView | |
android:id="@android:id/text2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@android:id/text1" | |
android:layout_alignStart="@android:id/text1" | |
android:layout_below="@android:id/text1" | |
android:layout_toLeftOf="@android:id/checkbox" | |
android:layout_toStartOf="@android:id/checkbox" | |
android:textAppearance="?attr/textAppearanceListItemSecondary" /> | |
<CheckBox | |
android:id="@android:id/checkbox" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentEnd="true" | |
android:layout_alignParentRight="true" | |
android:layout_marginTop="12dp" | |
android:focusable="false" | |
android:text="" /> | |
</com.laboratories.bregula.myapplication.CheckableRelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.laboratories.bregula.myapplication; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.SparseBooleanArray; | |
import android.view.View; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
import java.util.Random; | |
public class StudentListActivity extends AppCompatActivity implements View.OnClickListener { | |
public static final int VIEW_MODE_NORMAL = 1; | |
public static final int VIEW_MODE_REMOVE = 2; | |
private static final int REQUEST_CODE_ADD = 1; | |
private ListView mListView; | |
private Random random = new Random(); | |
private StudentListAdapter mAdapter; | |
private View mBarNormal; | |
private View mBarRemove; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_student_list); | |
mListView = findViewById(android.R.id.list); | |
ArrayList<Student> students = new ArrayList<>(); | |
for (int i = 0; i < 10; i++) { | |
students.add(new Student("Kotek " + i, random.nextInt(50000) + "")); | |
} | |
mAdapter = new StudentListAdapter(this, students); | |
mListView.setAdapter(mAdapter); | |
findViewById(R.id.button_save).setOnClickListener(this); | |
findViewById(R.id.button_remove).setOnClickListener(this); | |
findViewById(R.id.button_confirm_remove).setOnClickListener(this); | |
findViewById(R.id.button_cancel).setOnClickListener(this); | |
mBarNormal = findViewById(R.id.button_bar_normal); | |
mBarRemove = findViewById(R.id.button_bar_remove); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.button_save: | |
startActivityForResult(new Intent(this, AddStudentActivity.class), REQUEST_CODE_ADD); | |
break; | |
case R.id.button_remove: | |
setMode(VIEW_MODE_REMOVE); | |
break; | |
case R.id.button_confirm_remove: | |
removeSelectedItems(); | |
setMode(VIEW_MODE_NORMAL); | |
break; | |
case R.id.button_cancel: | |
setMode(VIEW_MODE_NORMAL); | |
break; | |
} | |
} | |
private void removeSelectedItems() { | |
SparseBooleanArray ids = mListView.getCheckedItemPositions(); | |
if (ids != null && ids.size() != 0) { | |
mAdapter.setNotifyOnChange(false); | |
for (int i = ids.size(); i >= 0; i--) { | |
if (ids.valueAt(i)) { | |
int position = ids.keyAt(i); | |
mAdapter.remove(mAdapter.getItem(position)); | |
} | |
} | |
mAdapter.setNotifyOnChange(true); | |
mAdapter.notifyDataSetChanged(); | |
} | |
mListView.clearChoices(); | |
} | |
public void setMode(int viewMode) { | |
if (viewMode == VIEW_MODE_NORMAL) { | |
mBarNormal.setVisibility(View.VISIBLE); | |
mBarRemove.setVisibility(View.GONE); | |
mAdapter.setCheckboxVisible(false); | |
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); | |
} else if (viewMode == VIEW_MODE_REMOVE) { | |
mBarNormal.setVisibility(View.GONE); | |
mBarRemove.setVisibility(View.VISIBLE); | |
mAdapter.setCheckboxVisible(true); | |
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE_ADD) { | |
if (resultCode == Activity.RESULT_OK) { | |
String name = data.getStringExtra(AddStudentActivity.EXTRA_NAME); | |
String phone = data.getStringExtra(AddStudentActivity.EXTRA_PHONE); | |
mAdapter.add(new Student(name, phone)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment