Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
236 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="materia.aswifter.com.materialexample" > | ||
package="materia.aswifter.com.materialexample"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme" > | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:label="@string/app_name" > | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
|
||
<activity android:name=".RecycleViewExampleActivity"></activity> | ||
</application> | ||
|
||
</manifest> |
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
141 changes: 141 additions & 0 deletions
141
app/src/main/java/materia/aswifter/com/materialexample/RecycleViewExampleActivity.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package materia.aswifter.com.materialexample; | ||
|
||
import android.os.Bundle; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.DefaultItemAnimator; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.support.v7.widget.Toolbar; | ||
import android.view.LayoutInflater; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
public class RecycleViewExampleActivity extends AppCompatActivity { | ||
|
||
private RecyclerView mRecyclerView; | ||
private LinearLayoutManager mLayoutManager; | ||
private String[] myDataset; | ||
private MyAdapter mAdapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_recycler_view); | ||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
toolbar.setTitle("RecycleViewExample"); | ||
setSupportActionBar(toolbar); | ||
|
||
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); | ||
|
||
// use this setting to improve performance if you know that changes | ||
// in content do not change the layout size of the RecyclerView | ||
mRecyclerView.setHasFixedSize(true); | ||
|
||
// use a linear layout manager | ||
mLayoutManager = new LinearLayoutManager(this); | ||
mRecyclerView.setLayoutManager(mLayoutManager); | ||
|
||
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); | ||
|
||
mRecyclerView.setItemAnimator(new DefaultItemAnimator()); | ||
|
||
// specify an adapter (see also next example) | ||
myDataset = new String[]{"JAVA", "Objective-C", "C", "C++", "Swift", | ||
"GO", "JavaScript", "Python", "Ruby", "HTML", "SQL"}; | ||
mAdapter = new MyAdapter(myDataset); | ||
mRecyclerView.setAdapter(mAdapter); | ||
} | ||
|
||
|
||
private void snackbarTest() { | ||
// Button button = (Button) findViewById(R.id.button); | ||
// button.setOnClickListener(new View.OnClickListener() { | ||
// @Override | ||
// public void onClick(View view) { | ||
// Snackbar.make(view, "hello world", Snackbar.LENGTH_SHORT).show(); | ||
// } | ||
// }); | ||
} | ||
|
||
|
||
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { | ||
private String[] mDataset; | ||
|
||
// Provide a reference to the views for each data item | ||
// Complex data items may need more than one view per item, and | ||
// you provide access to all the views for a data item in a view holder | ||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | ||
// each data item is just a string in this case | ||
public TextView mTextView; | ||
|
||
public ViewHolder(View v) { | ||
super(v); | ||
mTextView = (TextView) v.findViewById(R.id.textView); | ||
v.setOnClickListener(this); | ||
} | ||
|
||
@Override | ||
public void onClick(View view) { | ||
Snackbar.make(view, mTextView.getText(), Snackbar.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
// Provide a suitable constructor (depends on the kind of dataset) | ||
public MyAdapter(String[] myDataset) { | ||
mDataset = myDataset; | ||
} | ||
|
||
// Create new views (invoked by the layout manager) | ||
@Override | ||
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, | ||
int viewType) { | ||
// create a new view | ||
View v = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.item_view, parent, false); | ||
// set the view's size, margins, paddings and layout parameters | ||
ViewHolder vh = new ViewHolder(v); | ||
return vh; | ||
} | ||
|
||
// Replace the contents of a view (invoked by the layout manager) | ||
@Override | ||
public void onBindViewHolder(ViewHolder holder, int position) { | ||
// - get element from your dataset at this position | ||
// - replace the contents of the view with that element | ||
holder.mTextView.setText(mDataset[position]); | ||
|
||
} | ||
|
||
// Return the size of your dataset (invoked by the layout manager) | ||
@Override | ||
public int getItemCount() { | ||
return mDataset.length; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
// Inflate the menu; this adds items to the action bar if it is present. | ||
getMenuInflater().inflate(R.menu.menu_main, menu); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
// Handle action bar item clicks here. The action bar will | ||
// automatically handle clicks on the Home/Up button, so long | ||
// as you specify a parent activity in AndroidManifest.xml. | ||
int id = item.getItemId(); | ||
|
||
//noinspection SimplifiableIfStatement | ||
if (id == R.id.action_settings) { | ||
return true; | ||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/materia/aswifter/com/materialexample/RecyclerItemClickListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package materia.aswifter.com.materialexample; | ||
|
||
import android.content.Context; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.GestureDetector; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
|
||
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { | ||
private OnItemClickListener mListener; | ||
|
||
public interface OnItemClickListener { | ||
public void onItemClick(View view, int position); | ||
} | ||
|
||
GestureDetector mGestureDetector; | ||
|
||
public RecyclerItemClickListener(Context context, OnItemClickListener listener) { | ||
mListener = listener; | ||
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | ||
@Override public boolean onSingleTapUp(MotionEvent e) { | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { | ||
View childView = view.findChildViewUnder(e.getX(), e.getY()); | ||
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { | ||
mListener.onItemClick(childView, view.getChildPosition(childView)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<RelativeLayout 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="match_parent"> | ||
|
||
|
||
<android.support.v7.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentTop="true" | ||
android:background="?attr/colorPrimaryDark" /> | ||
|
||
|
||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/recyclerView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_below="@+id/toolbar" | ||
android:scrollbars="none" /> | ||
|
||
<android.support.design.widget.FloatingActionButton | ||
android:id="@+id/fab_normal" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentBottom="true" | ||
android:layout_alignParentRight="true" | ||
android:layout_marginBottom="10dp" | ||
android:layout_marginRight="10dp" | ||
android:src="@drawable/ic_favorite" | ||
app:fabSize="normal" /> | ||
</RelativeLayout> |
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