먼저 RecyclerView를 만들기 위해서 



앱 build.gradle에 appcompatV7와 recycleview lib를 추가해야합니다.


compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'



MainActivity


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.damoa.testfragment.dummy.DummyContent;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.test_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(this,DummyContent.ITEMS));
}

}



MyItemRecyclerViewAdapter


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.damoa.testfragment.dummy.DummyContent.DummyItem;

import java.util.List;


public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

private final List<DummyItem> mValues;
private Context mContext = null;

public MyItemRecyclerViewAdapter(Context context, List<DummyItem> items) {
mContext = context;
mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mTitle.setText(mValues.get(position).title);

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"test"+position,Toast.LENGTH_SHORT).show();
}
});
}

@Override
public int getItemCount() {
return mValues.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mTitle;

public ViewHolder(View view) {
super(view);
mView = view;
mTitle = (TextView) view.findViewById(R.id.item_title);
}

}
}



DummyContent


import java.util.ArrayList;
import java.util.List;

/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {

/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();


private static final int COUNT = 25;

static {
// Add some sample items.
for (int i = 1; i <= COUNT; i++) {
addItem(createDummyItem(i));
}
}

private static void addItem(DummyItem item) {
ITEMS.add(item);
}

private static DummyItem createDummyItem(int position) {
return new DummyItem(String.valueOf(position)+"Item "+ position);
}

private static String makeDetails(int position) {
StringBuilder builder = new StringBuilder();
builder.append("Details about Item: ").append(position);
for (int i = 0; i < position; i++) {
builder.append("\nMore details information here.");
}
return builder.toString();
}

/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String title;

public DummyItem(String id) {
this.title = id;
}
}
}



activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.damoa.testfragment.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/test_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>



recycler_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

</LinearLayout>



728x90
반응형

+ Recent posts