Skip to content

Instantly share code, notes, and snippets.

@simplydikka
Last active November 10, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simplydikka/1a721e322d6f3125dc131955c4f061e9 to your computer and use it in GitHub Desktop.
Save simplydikka/1a721e322d6f3125dc131955c4f061e9 to your computer and use it in GitHub Desktop.
Details from list
<?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.example.android.signs.SingleCardGroupA">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.android.signs.SingleCardGroupA">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:id="@+id/image_activity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/text1_activity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="left"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text2_activity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="16dp"
android:textSize="14sp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="previous"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp"
android:id="@+id/parent_layout">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</RelativeLayout>
package com.example.android.signs;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import java.util.ArrayList;
public class AllGroupASignsList extends AppCompatActivity {
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_group_a_signs_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Знаци от Група А");
createExampleList();
buildRecyclerView();
}
public void createExampleList() {
mExampleList = new ArrayList<>();
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A1_title), getString(R.string.A1_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A2_title), getString(R.string.A2_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A3_title), getString(R.string.A3_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A4_title), getString(R.string.A4_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A5_title), getString(R.string.A5_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A6_title), getString(R.string.A6_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A7_title), getString(R.string.A7_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A8_title), getString(R.string.A8_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A9_title), getString(R.string.A9_description)));
mExampleList.add(new ExampleItem(R.drawable.imageplaceholder, getString(R.string.A10_title), getString(R.string.A10_description)));
}
public void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(AllGroupASignsList.this, SingleCardGroupA.class);
intent.putExtra("Example Item", mExampleList.get(position));
startActivity(intent);
}
});
}
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_group_a_signs, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:contentPadding="10dp"
android:layout_marginBottom="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="100"
>
<ImageView
android:id="@+id/imageSign"
android:layout_weight="40"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
/>
<LinearLayout
android:paddingLeft="16dp"
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/signName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Placeholder"
android:textStyle="bold"
android:maxLines="2"
android:minLines="1"
android:ellipsize="end"/>
<TextView
android:id="@+id/signDescription"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Placeholder"
android:maxLines="3"
android:minLines="1"
android:ellipsize="end" />
<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right|bottom"
>
<Button
android:id="@+id/openButton"
android:minHeight="0dp"
android:minWidth="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:text="Отвори"
android:textColor="@color/colorAccent"
android:layout_marginRight="16dp"
/>
<ImageButton
android:id="@+id/heart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
package com.example.android.signs;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> implements Filterable {
private ArrayList<ExampleItem> mExampleList;
private List<ExampleItem> mExampleListFull;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextTitle;
public TextView mTextDescription;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageSign);
mTextTitle = itemView.findViewById(R.id.signName);
mTextDescription = itemView.findViewById(R.id.signDescription);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList){
mExampleList = exampleList;
mExampleListFull = new ArrayList<>(exampleList);
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextTitle.setText(currentItem.getTextTitle());
holder.mTextDescription.setText(currentItem.getmTextDescription());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public Filter getFilter(){
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<ExampleItem> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(mExampleListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for(ExampleItem item : mExampleListFull){
if (item.getTextTitle().toLowerCase().contains(filterPattern) || item.getmTextDescription().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mExampleList.clear();
mExampleList.addAll((List)results.values);
notifyDataSetChanged();
}
};
}
package com.example.android.signs;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import android.widget.Toast;
public class ExampleItem implements Parcelable {
private int mImageResource;
private String mTextTitle;
private String mTextDescription;
public ExampleItem(int imageResource, String textTitle, String textDescription) {
mImageResource = imageResource;
mTextTitle = textTitle;
mTextDescription = textDescription;
}
protected ExampleItem(Parcel in) {
mImageResource = in.readInt();
mTextTitle = in.readString();
mTextDescription = in.readString();
}
public static final Creator<ExampleItem> CREATOR = new Creator<ExampleItem>() {
@Override
public ExampleItem createFromParcel(Parcel in) {
return new ExampleItem(in);
}
@Override
public ExampleItem[] newArray(int size) {
return new ExampleItem[size];
}
};
public int getImageResource() {
return mImageResource;
}
public String getTextTitle() {
return mTextTitle;
}
public String getmTextDescription() {
return mTextDescription;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mImageResource);
dest.writeString(mTextTitle);
dest.writeString(mTextDescription);
}
}
package com.example.android.signs;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class SingleCardGroupA extends AppCompatActivity {
Button next;
Button previous;
private String position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_card_groupa);
Intent intent = getIntent();
ExampleItem exampleItem = intent.getParcelableExtra("Example Item");
int imageRes = exampleItem.getImageResource();
String line1 = exampleItem.getTextTitle();
String line2 = exampleItem.getmTextDescription();
ImageView imageView = findViewById(R.id.image_activity2);
imageView.setImageResource(imageRes);
TextView textView1 = findViewById(R.id.text1_activity2);
textView1.setText(line1);
TextView textView2 = findViewById(R.id.text2_activity2);
textView2.setText(line2);
Button next = (Button) findViewById(R.id.next);
Button previous = (Button) findViewById(R.id.previous);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment