SuffleList Example
MainActivity:
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private GridLayoutManager mLayoutManager;
private ArrayList<String> text1 = new ArrayList<String>();
private ArrayList<String> text2 = new ArrayList<String>();
private ArrayList<Integer> spos = new ArrayList<Integer>();
private ArrayList<Integer> sposnew = new ArrayList<Integer>();
private ArrayList<Integer> sposnewselected = new ArrayList<Integer>();
private Button b1, b2;
TextView txt1;
int sposp;
int pos;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String count = getIntent().getStringExtra("count");
if (!TextUtils.isEmpty(count)) {
for (int i = 0; i < Integer.parseInt(count); i++) {
text1.add("A " + String.valueOf(i));
text2.add("a " + String.valueOf(i));
spos.add(i);
}
} else {
for (int i = 0; i < 20; i++) {
text1.add("A " + String.valueOf(i));
text2.add("a " + String.valueOf(i));
spos.add(i);
}
}
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
txt1 = (TextView) findViewById(R.id.text1);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet(), MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
b1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Collections.shuffle(spos);
Toast.makeText(MainActivity.this, String.valueOf(spos.get(0)), Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
getFirstPos();
}
});
suffleList();
}
@Override protected void onResume() {
super.onResume();
}
private ArrayList<DataObject> getDataSet() {
ArrayList results = new ArrayList<DataObject>();
for (int index = 0; index < text1.size(); index++) {
DataObject obj = new DataObject(text1.get(index).toString(), text2.get(index).toString(), spos.get(index));
results.add(index, obj);
}
return results;
}
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
private String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
Context context;
public class DataObjectHolder extends RecyclerView.ViewHolder {
TextView label;
TextView dateTime;
LinearLayout layout_my;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
layout_my = (LinearLayout) itemView.findViewById(R.id.layout_my);
}
}
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset, Context context) {
mDataset = myDataset;
this.context = context;
}
@Override public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleriew_item, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override public void onBindViewHolder(final DataObjectHolder holder, final int position) {
final DataObject dataObject = mDataset.get(position);
holder.label.setText(mDataset.get(position).getmText1());
holder.dateTime.setText(mDataset.get(position).getmText2());
if (sposnewselected.size() > 0) {
if (sposnewselected.contains(position)) {
holder.layout_my.setBackgroundResource(R.color.colorPrimaryDark);
}
}
if (sposnew.size() > 0) {
if (position == sposnew.get(0)) {
holder.layout_my.setBackgroundResource(R.color.colorAccent);
}
}
holder.layout_my.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if (sposnew.size() > 0) {
if (position == sposnew.get(0)) {
holder.layout_my.setBackgroundResource(R.color.colorAccent);
getFirstPos();
notifyDataSetChanged();
}
}
}
});
}
@Override public int getItemCount() {
return mDataset.size();
}
public int getItemViewType(int position) {
return position;
}
}
String listString = "";
String listString1 = "";
public void suffleList() {
Collections.shuffle(spos);
for (int str : spos) {
sposnew.add(str);
listString += String.valueOf(str) + "\t";
}
txt1.setText(listString);
}
public void getFirstPos() {
if (sposnew.size() > 0) {
sposnewselected.add(sposnew.get(0));
sposnew.remove(0);
for (int str : sposnew) {
listString1 += String.valueOf(str) + "\t ";
}
Log.e("new str", listString1);
}
}
@Override public void onBackPressed() {
finish();
}
}
ModelClass:
public class DataObject {
private String mText1;
private String mText2;
private int spos;
public DataObject(String mText1, String mText2, int spos) {
this.mText1 = mText1;
this.mText2 = mText2;
this.spos = spos;
}
public String getmText1() {
return mText1;
}
public void setmText1(String mText1) {
this.mText1 = mText1;
}
public String getmText2() {
return mText2;
}
public void setmText2(String mText2) {
this.mText2 = mText2;
}
public int getSpos() {
return spos;
}
public void setSpos(int spos) {
this.spos = spos;
}
}
No comments:
Post a Comment