app\build.gradle
compile 'com.android.support:recyclerview-v7:26.1.0'
CommomAdapter.java
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by baostorm on 2017/12/17.
*/
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonAdapter.ViewHolder> {
private List<T> mItemsList;
private OnItemClickListener mListener;
public CommonAdapter(List<T> list) {
mItemsList = list;
}
public abstract int getLayoutId(int ViewType);
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
return ViewHolder.get(parent, getLayoutId(viewType));
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
convert(holder, mItemsList.get(position), position);
if (mListener != null) {
holder.mConvertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(holder.getAdapterPosition());
}
});
holder.mConvertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mListener.onLongClick(holder.getAdapterPosition());
return false;
}
});
}
}
@Override
public int getItemCount() {
return mItemsList.size();
}
public abstract void convert(ViewHolder holder, T data, int position);
public static class ViewHolder extends RecyclerView.ViewHolder{
private SparseArray<View> mView;
private View mConvertView;
private ViewHolder(View v) {
super(v);
mConvertView = v;
mView = new SparseArray<>();
}
public static ViewHolder get(ViewGroup parent, int layoutId) {
View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new ViewHolder(convertView);
}
public <T extends View> T getView(int id) {
View v = mView.get(id);
if (v == null) {
v = mConvertView.findViewById(id);
mView.put(id, v);
}
return (T)v;
}
public void setText(int id, String value) {
TextView view = getView(id);
view.setText(value);
}
public void onWidgetClick(int id, View.OnClickListener listener) {
View view = getView(id);
view.setOnClickListener(listener);
}
}
public interface OnItemClickListener {
void onClick(int position);
void onLongClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private CommonAdapter<MyItemClass> mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainAdapter = new CardAdapter<MyItemClass>(mListItem) {
@Override
public int getLayoutId(int ViewType) {
return R.layout.my_item;
}
@Override
public void convert(ViewHolder holder, MyItemClass data, int position) {
holder.setText(R.id.git_name, data.getLogin());
holder.setText(R.id.git_id, data.getId());
holder.setText(R.id.git_blog, data.getBlog());
holder.onWidgetClick(R.id.git_name, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, data.getLogin(), Toast.LENGTH_SHORT).show();
}
});
}
};
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mainAdapter);
}
}