forked from github/wulkanowy-mirror
Add RecyclerView in marks
This commit is contained in:
@ -1,62 +1,56 @@
|
||||
package io.github.wulkanowy.activity.dashboard.marks;
|
||||
|
||||
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.BaseAdapter;
|
||||
import android.widget.GridView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import io.github.wulkanowy.R;
|
||||
|
||||
public class ImageAdapter extends BaseAdapter {
|
||||
private Context mContext;
|
||||
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
|
||||
|
||||
public ImageAdapter(Context c) {
|
||||
this.mContext = c;
|
||||
private Context context;
|
||||
private ArrayList<String> lista;
|
||||
|
||||
public ImageAdapter(Context context, ArrayList<String> lista) {
|
||||
this.lista = lista;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return mThumbIds.length;
|
||||
@Override
|
||||
public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
|
||||
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
public Object getItem(int position) {
|
||||
return null;
|
||||
@Override
|
||||
public void onBindViewHolder(ImageAdapter.ViewHolder viewHolder, int i) {
|
||||
|
||||
viewHolder.tv_android.setText(lista.get(i));
|
||||
Picasso.with(context).load(R.drawable.sample_0).into(viewHolder.img_android);
|
||||
}
|
||||
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return lista.size();
|
||||
}
|
||||
|
||||
// create a new ImageView for each item referenced by the Adapter
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ImageView imageView;
|
||||
if (convertView == null) {
|
||||
// if it's not recycled, initialize some attributes
|
||||
imageView = new ImageView(mContext);
|
||||
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
|
||||
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
imageView.setPadding(8, 8, 8, 8);
|
||||
} else {
|
||||
imageView = (ImageView) convertView;
|
||||
public class ViewHolder extends RecyclerView.ViewHolder{
|
||||
private TextView tv_android;
|
||||
private ImageView img_android;
|
||||
public ViewHolder(View view) {
|
||||
super(view);
|
||||
|
||||
tv_android = (TextView)view.findViewById(R.id.tv_android);
|
||||
img_android = (ImageView) view.findViewById(R.id.img_android);
|
||||
}
|
||||
|
||||
imageView.setImageResource(mThumbIds[position]);
|
||||
return imageView;
|
||||
}
|
||||
|
||||
// references to our images
|
||||
private Integer[] mThumbIds = {
|
||||
R.drawable.sample_2, R.drawable.sample_3,
|
||||
R.drawable.sample_4, R.drawable.sample_5,
|
||||
R.drawable.sample_6, R.drawable.sample_7,
|
||||
R.drawable.sample_0, R.drawable.sample_1,
|
||||
R.drawable.sample_2, R.drawable.sample_3,
|
||||
R.drawable.sample_4, R.drawable.sample_5,
|
||||
R.drawable.sample_6, R.drawable.sample_7,
|
||||
R.drawable.sample_0, R.drawable.sample_1,
|
||||
R.drawable.sample_2, R.drawable.sample_3,
|
||||
R.drawable.sample_4, R.drawable.sample_5,
|
||||
R.drawable.sample_6, R.drawable.sample_7
|
||||
};
|
||||
}
|
||||
}
|
@ -5,20 +5,33 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
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.GridView;
|
||||
import android.widget.Toast;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import io.github.wulkanowy.R;
|
||||
|
||||
public class MarksFragment extends Fragment {
|
||||
|
||||
Activity mActivity;
|
||||
|
||||
final String lista[] = {
|
||||
"Donut",
|
||||
"Eclair",
|
||||
"Froyo",
|
||||
"Gingerbread",
|
||||
"Honeycomb",
|
||||
"Ice Cream Sandwich",
|
||||
"Jelly Bean",
|
||||
"KitKat",
|
||||
"Lollipop",
|
||||
"Marshmallow"
|
||||
};
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
@ -36,15 +49,15 @@ public class MarksFragment extends Fragment {
|
||||
|
||||
View view = inflater.inflate(R.layout.fragment_marks, container, false);
|
||||
|
||||
GridView gridview = (GridView) view.findViewById(R.id.gridview);
|
||||
gridview.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
|
||||
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.card_recycler_view);
|
||||
recyclerView.setHasFixedSize(true);
|
||||
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(view.getContext(),2);
|
||||
recyclerView.setLayoutManager(layoutManager);
|
||||
|
||||
ArrayList<String> array = new ArrayList<>(Arrays.asList(lista));
|
||||
ImageAdapter adapter = new ImageAdapter(view.getContext(),array);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
gridview.setOnItemClickListener(new OnItemClickListener() {
|
||||
public void onItemClick(AdapterView<?> parent, View v,
|
||||
int position, long id) {
|
||||
Toast.makeText(mActivity,"HALLO", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user