Android calendar event use gridview listview - android

http://i.imgur.com/PyO4q.png?1
This image is using the gridview to make a calendar, but now I want to add a new event in "day". So I thought to use the listview inside the gridview-day, but the problem is the listview doesn't scroll. I think maybe the listview scroll collides with gridview. Please give me some idea to solve this problem.
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
TextView present;
TextView meeting;
TextView other;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView)v.findViewById(R.id.date);
present=(TextView)v.findViewById(R.id.ptextView1);
meeting=(TextView)v.findViewById(R.id.mtextView2);
other=(TextView)v.findViewById(R.id.otextView3);
// listitem=(ListView)v.findViewById(R.id.calendarlist);
// listitem.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
// disable empty days from the beginning
if(days[position].equals("")) {
dayView.setClickable(false);
dayView.setFocusable(false);
}
else {
// mark current day as focused
if(month.get(Calendar.YEAR)== selectedDate.get(Calendar.YEAR) && month.get(Calendar.MONTH)== selectedDate.get(Calendar.MONTH) && days[position].equals(""+selectedDate.get(Calendar.DAY_OF_MONTH))) {
v.setBackgroundResource(R.drawable.item_background_focused);
}
else {
v.setBackgroundResource(R.drawable.list_item_background);
}
}
dayView.setText(days[position]);
present.setVisibility(View.GONE);
meeting.setVisibility(View.GONE);
other.setVisibility(View.GONE);
for(int t=0;t<date1.size();t++){
if(returndouble(dayView.getText().toString()).equals(date1.get(t).get(3))){
if(date1.get(t).get(1).equals("Presented")){
meeting.setVisibility( View.VISIBLE);
}else if(date1.get(t).get(1).equals("Present")){
present.setVisibility( View.VISIBLE);
}else{
other.setVisibility( View.VISIBLE);
}
}
}
// create date string for comparison
String date = days[position];
if(date.length()==1) {
date = "0"+date;
}
String monthStr = ""+(month.get(Calendar.MONTH)+1);
if(monthStr.length()==1) {
monthStr = "0"+monthStr;
}
// show icon if date is not empty and it exists in the items array
// ImageView iw = (ImageView)v.findViewById(R.id.date_icon);
/* if(date.length()>0 && items!=null && items.contains(date)) {
iw.setVisibility(View.VISIBLE);
}
else {
iw.setVisibility(View.INVISIBLE);
}*/
return v;
}
public void refreshDays()
{
// clear items
items.clear();
Calendardb date=new Calendardb(mContext);
date1= date.calendarselect(String.valueOf( month.get(Calendar.YEAR))+"/"+returndouble(String.valueOf((month.get(Calendar.MONTH)+1))));
int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDay = (int)month.get(Calendar.DAY_OF_WEEK);
// figure size of the array
if(firstDay==1){
days = new String[lastDay+(FIRST_DAY_OF_WEEK*6)];
}
else {
days = new String[lastDay+firstDay-(FIRST_DAY_OF_WEEK+1)];
}
int j=FIRST_DAY_OF_WEEK;
// populate empty days before first real day
if(firstDay>1) {
for(j=0;j<firstDay-FIRST_DAY_OF_WEEK;j++) {
days[j] = "";
}
}
else {
for(j=0;j<FIRST_DAY_OF_WEEK*6;j++) {
days[j] = "";
}
j=FIRST_DAY_OF_WEEK*6+1; // sunday => 1, monday => 7
}
// populate days
int dayNumber = 1;
for(int i=j-1;i<days.length;i++) {
days[i] = ""+dayNumber;
dayNumber++;
}
}

Related

Android - Regular repetition problem in custom listview in getView

I'm pulling data into Custom Listview from the mysql database, but as follows, I have a custom button. The button is either black or red, according to the records in the database. For example, there are 30 yields in my database and I pull the data with only 2 red and 28 black. Let's say the data in red is my data number 3 and data number 5. When I start to scroll down; my data number 3 and 5 turn red, then 8 data then the yield number 13 and 15, 23 and 25 turns red .. so there is a repetition .. I can't prevent it .. Is there a solution to this? Can you give support with sample code? My Custom Adapter code is as follows:
public class CostumAdaptor extends ArrayAdapter<Ses> {
private ArrayList<Ses> list;
private ViewHolderr vHolder;
boolean flag = false;
public CostumAdaptor(Context context, int resource, ArrayList<Ses> list) {
super(context, resource, list);
list = new ArrayList<Ses>();
list.addAll(list);
}
public int getCount() {
return list.size();
}
public Ses getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return list.get(position).hashCode();
}
public View getView(int position, View conView, ViewGroup parent) {
View vi = conView;
if (vi == null) {
vHolder = new ViewHolderr();
vi = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.tiyatro_item, null);
vHolder.tiyatro_adi = vi.findViewById(R.id.tiyatro_adi);
vHolder.begeni_sayisi2 = (TextView) vi.findViewById(R.id.begeni_sayisi2);
vHolder.daha_sonra_dinleyecegim_2 = (TextView) vi.findViewById(R.id.daha_sonra_dinleyecegim_2);
vHolder.begenmedim_2 = (TextView) vi.findViewById(R.id.begenmedim_2);
vHolder.tiyatro = vi.findViewById(R.id.tiyatro);
vHolder.tur = vi.findViewById(R.id.tur);
vHolder.mensei = vi.findViewById(R.id.mensei);
vHolder.yapimci = vi.findViewById(R.id.yapimci);
vHolder.songCoverImage = vi.findViewById(R.id.coverImage);
vHolder.begendim = vi.findViewById(R.id.begendim);
vHolder.begenmedim = vi.findViewById(R.id.begenmedim);
vHolder.daha_sonra_dinleyecegim = vi.findViewById(R.id.daha_sonra_dinleyecegim);
vi.setTag(vHolder);
}
else {
vHolder = (ViewHolderr) vi.getTag();
}
Ses dmodel = (Ses) list.get(position);
String t_tiyatro_adi = dmodel.getTiyatro_adi();
vHolder.tiyatro_adi.setTag(dmodel);
vHolder.tiyatro.setTag(dmodel);
vHolder.tur.setTag(dmodel);
vHolder.mensei.setTag(dmodel);
vHolder.yapimci.setTag(dmodel);
vHolder.begendim.setTag(dmodel);
vHolder.tiyatro_adi.setText(dmodel.getTiyatro_adi());
vHolder.tiyatro.setText(dmodel.getTiyatro());
vHolder.tur.setText(dmodel.getTur());
vHolder.mensei.setText(dmodel.getMensei());
vHolder.yapimci.setText(dmodel.getYapimci());
String CACHE = t_tiyatro_adi;
new Handler().postDelayed(new Runnable() {
public void run() {
for (int i = 0; i < List1.size(); i++) {
for (int ii = 0; ii < List2.size(); ii++) {
if (List1.get(i).equals(List2.get(ii))) {
if (List1.get(i).equals(CACHE)) {
flag = true;
vHolder.begendim.setImageResource(R.drawable.begendim_dolu);
}
}
}
}
if (!flag) {
vHolder.begendim.setImageResource(R.drawable.begendim_bos);
}
}
}, 250);
return vi;
}
private class ViewHolderr {
TextView tiyatro_adi, tiyatro, tur, mensei, yapimci, begeni_sayisi2, daha_sonra_dinleyecegim_2, begenmedim_2;
ImageView songCoverImage;
ImageView begendim;
}
}
Note: List1 and List2 are ArrayLists pulled from the mysql database and compared with a for loop. The color of the button is red or black as a result of the comparison.

Android ViewBinder values from cursor

Hoping someone can help me here. I have a layout which contains listview, each item in the list view contains individual text fields to represent the days of the week --> SMTWTFS
What I am trying to do is change the colour of the text items if they are set within the database. This nearly works but I have noticed the first row in the list view changes the colour for all items when the are set in other list rows.
My text items are declared like this
textSUN = (TextView) v.findViewById(R.id.textSUN);
textMON = (TextView) v.findViewById(R.id.textMON);
textTUE = (TextView) v.findViewById(R.id.textTUE);
textWED = (TextView) v.findViewById(R.id.textWED);
textTHU = (TextView) v.findViewById(R.id.textTHU);
textFRI = (TextView) v.findViewById(R.id.textFRI);
textSAT = (TextView) v.findViewById(R.id.textSAT);
I then query my db and set the adapter
db = new DatabaseHandler(getActivity());
mCursor=db.getReadableDatabase().rawQuery("SELECT rowid _id,* "+
"FROM table", null);
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.list_row, mCursor,
new String[] {DatabaseHandler.KEY_SUNDAY, DatabaseHandler.KEY_MONDAY,DatabaseHandler.KEY_TUESDAY,DatabaseHandler.KEY_WEDNESDAY, DatabaseHandler.KEY_THURSDAY, DatabaseHandler.KEY_FRIDAY,
DatabaseHandler.KEY_SATURDAY,},
new int[] {R.id.textSUN,R.id.textMON, R.id.textTUE, R.id.textWED, R.id.textTHU, R.id.textFRI, R.id.textSAT});
Finally I try to set the color in the view binder if the value returned is 1 from the db
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int
columnIndex) {
final int sun_column = cursor.getColumnIndex("sun");
final int mon_column = cursor.getColumnIndex("mon");
final int tue_column = cursor.getColumnIndex("tue");
final int wed_column = cursor.getColumnIndex("wed");
final int thu_column = cursor.getColumnIndex("thu");
final int fri_column = cursor.getColumnIndex("fri");
final int sat_column = cursor.getColumnIndex("sat");
}
if (columnIndex == sun_column) {
int sun = cursor.getInt(cursor.getColumnIndexOrThrow("sun"));
if(sun == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == mon_column) {
int mon = cursor.getInt(cursor.getColumnIndexOrThrow("mon"));
if(mon == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == tue_column) {
int tue = cursor.getInt(cursor.getColumnIndexOrThrow("tue"));
if(tue == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == wed_column) {
int wed = cursor.getInt(cursor.getColumnIndexOrThrow("wed"));
if(wed == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == thu_column) {
int thu = cursor.getInt(cursor.getColumnIndexOrThrow("thu"));
if(thu == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == fri_column) {
int fri = cursor.getInt(cursor.getColumnIndexOrThrow("fri"));
if(fri == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
if (columnIndex == sat_column) {
int sat = cursor.getInt(cursor.getColumnIndexOrThrow("sat"));
if(sat == 1){
((TextView) view).setTextColor(Color.parseColor("#FFFFFF"));
}
return true;
}
return false;
}
});
lv = (ListView) v.findViewById(R.id.listView);
lv.setAdapter(adapter);
Perhaps I am going about this all wrong ?? It nearly works though.
Many thanks
You still need to handle the cases where mon, tue, etc. == 0. Adding the following to each if statement should fix it:
else {
((TextView) view).setTextColor(//color that you want if day == 0);
}
Also, you may want to consider changing your whole setViewValue to use a switch statement, it should make the cod more readable.

How to set specific items in spinner to disabled state

I've currently managed to disable my spinner if a specific item is selected, however I also want to set that specific item to a disabled state visually. Is this possible?
EDIT: To clarify, when I click on my spinner, I see a drop down menu with several items I can select. I want the disabled items to have a different visual, like in the link above.
private static final String[]paths = {"None", "Arrow Rain", "Restoration", "Ares' Fervor", "Windstorm", "Guardian Angel", "Meteor", "Disaster", "Ares' Frenzy", "Phoenix Flight", "Snowstorm"};
ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,paths) {
#Override
public boolean isEnabled(int position) {
global1 = position;
if( (global1 == global2 || global1 == global3 || global1 == global4) && global1>0 ){
return false;
}else{
g1t.setText( "Price: " + String.valueOf(g1p) +"\n" );
totalprice.setText( "Total Price: " + String.valueOf(g1p+g2p+g3p+g4p));
return true;
}
}
};
magic1 = (Spinner)findViewById(R.id.spinnermagic1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
magic1.setAdapter(adapter1);
magic1.setOnItemSelectedListener(this);
//global 2, 3, and 4 are also positions.
//Is there a way to set those to disabled states?
// Create spinner month, and disable month < today
List<CharSequence> listMonth = new ArrayList<CharSequence>();
for (int m = 0; m < 12; m++) {
if (m < 9) {
listMonth.add("0" + (m + 1));
} else {
listMonth.add("" + (m + 1));
}
}
// Create spinner item
adapterMonth = new ArrayAdapter<CharSequence>(this,
R.layout.layout_spinner_item, listMonth) {
// Disable click item < month current
#Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
if (year <= max_year && position < max_month - 1) {
return false;
}
return true;
}
// Change color item
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View mView = super.getDropDownView(position, convertView, parent);
TextView mTextView = (TextView) mView;
if (year <= max_year && position < max_month - 1) {
mTextView.setTextColor(Color.GRAY);
} else {
mTextView.setTextColor(Color.BLACK);
}
return mView;
}
};
adapterMonth
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn2.setAdapter(adapterMonth);

Android how to set color of specific item in GridView

I have a GridView with an Adapter that creates a Calendar to show some events from my web server.
The data loads succesfully and when an item is clicked it shows the event. The thing is that i need to know which days have an event by setting up the background color of the item whithout the need of clicking them.
How can i achieve that?
This is the Adapter code:
public class CalendarAdapter extends BaseAdapter {
private Context mContext;
private java.util.Calendar month;
public GregorianCalendar pmonth; // calendar instance for previous month
/**
* calendar instance for previous month for getting complete view
*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int lastWeekDay;
int leftDays;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
private ArrayList<String> items;
public static List<String> dayString;
private View previousView;
public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {
CalendarAdapter.dayString = new ArrayList<String>();
Locale.setDefault(Locale.US);
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
mContext = c;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public void setItems(ArrayList<String> items) {
for (int i = 0; i != items.size(); i++) {
if (items.get(i).length() == 1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return dayString.size();
}
public Object getItem(int position) {
return dayString.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView) v.findViewById(R.id.date);
// separates daystring into parts.
String[] separatedTime = dayString.get(position).split("-");
// taking last part of date. ie; 2 from 2012-12-02
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
// checking whether the day is in current month or not.
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
// setting offdays to white color.
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.BLUE);
}
if (dayString.get(position).equals(curentDateString)) {
setSelected(v);
previousView = v;
} else {
v.setBackgroundResource(R.drawable.list_item_background);
}
dayView.setText(gridvalue);
// create date string for comparison
String date = dayString.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
// show icon if date is not empty and it exists in the items array
ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
if (date.length() > 0 && items != null && items.contains(date)) {
iw.setVisibility(View.VISIBLE);
} else {
iw.setVisibility(View.INVISIBLE);
}
Log.e("color",""+CalendarView.color);
if (CalendarView.color.equals("YELLOW")){
v.setBackgroundColor(Color.YELLOW);
}else if (CalendarView.color.equals("RED")){
v.setBackgroundColor(Color.RED);
}
return v;
}
public View setSelected(View view) {
if (previousView != null) {
previousView.setBackgroundResource(R.drawable.list_item_background);
}
previousView = view;
view.setBackgroundResource(R.drawable.calendar_cel_selectl);
return view;
}
public void refreshDays() {
// clear items
items.clear();
dayString.clear();
Locale.setDefault(Locale.US);
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
/**
* Calendar instance for getting a complete gridview including the three
* month's (previous,current,next) dates.
*/
pmonthmaxset = (GregorianCalendar) pmonth.clone();
/**
* setting the start date as previous month's required date.
*/
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
/**
* filling calendar gridview.
*/
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
dayString.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
}
Thanks in advance
Calendar_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:background="#drawable/calendar_cell"
android:gravity="center"
android:orientation="vertical"
android:padding="2dip" >
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000D7"
android:textSize="14dip"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/date_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/date"
android:src="#drawable/dot"
android:visibility="gone" />
</LinearLayout>
You can set the background color for the required Grid Item depending on the item position
row.setBackgroundColor(Color.GREEN);
But due to the GridView item view recycle , you will see the same background color set to some other items too.(since the view instance is same , only the data is different)
To fix this, you need to let GridView know that you have two different type of views ( one with color and other without color)
First Override getViewTypeCount and inform GridView that you have two view types
#Override
public int getViewTypeCount() {
return 2;
}
Next, Override getItemViewType and let GridView know that which items position type is different. e.g., if the items 1 and 3 has events then return 1(some unique number) for those position and return 0 for other positions
#Override
public int getItemViewType(int position){
// return a unique number
if(position == 1 || position == 3){
return 1;
}
else {
return 0;
}
}
Finally, when getView is called. check for the position and set background color only for those specific items
if(position == 1 || position == 3){
row.setBackgroundColor(Color.GREEN);
}
This should work. perfectly. Let me know.
get the id of your inflator parent layout and set like below:
LinearLayout f_l = (LinearLayout) itemView.findViewById(R.id.pr_bg);
if (position % 3 == 0) {
f_l.setBackgroundResource(R.drawable.campaignbase_black);
} else if (position % 3 == 1) {
f_l.setBackgroundResource(R.drawable.campaignbase_pink);
} else if (position % 3 == 2) {
f_l.setBackgroundResource(R.drawable.campaignbase_brown);
}

Gingerbread listview item animation issue

I've looked for this issue but nothing I've found matches my situation.
My problem occurs onAndroid 2.3.x (on 4.x it works perfectly)
I have an application with a custom list view. I initialize my listview as follows
ListAdapter mListAdapter = new ListAdapter(getApplicationContext(), this, ...);
lvSelector = (ListView) findViewById(R.id.listView1);
lvSelector.setAdapter(mListAdapter);
My ListAdapter is as follows:
public class ListAdapter extends BaseAdapter {
static class Holder {
LinearLayout layoutRoot, layoutColor;
TextView hText;
Animation anim = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
public Holder() {
layoutRoot = new LinearLayout(mContext);
layoutColor = new LinearLayout(mContext);
hText = new TextView(mContext);
}
public Holder(Holder holder) {
this.layoutRoot = holder.layoutRoot;
this.layoutColor = holder.layoutColor;
this.hText = holder.hText;
}
}
int mSwap1, mSwap2;
Animation mAnimation;
public ListAdapter(Context _context, Activity _activity, FileHandler _fileHandler, String _strSchemaName, List<String> _list, List<String> _solution) {
mContext = _context;
mActivity = _activity;
mAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
mAnimation.reset();
mSwap1 = mSwap2 = -1;
/* ... */
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int fPosition = position;
View row = convertView;
Holder lHolder = null;
if (row==null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
row = inflater.inflate(R.layout.layout_schema_element, parent, false);
lHolder = new Holder();
lHolder.layoutRoot = (LinearLayout) row.findViewById(R.id.elementLayoutRoot);
lHolder.layoutColor = (LinearLayout) row.findViewById(R.id.elementLayoutColor);
lHolder.hText = (TextView) row.findViewById(R.id.textViewWord);
row.setTag(lHolder);
}
else {
lHolder = (Holder)row.getTag();
}
row.setOnClickListener(null);
if (position==0 || position==mDataList.size()-1) {
lHolder.layoutColor.setBackgroundResource(R.drawable.bg_elem_fixed);
lHolder.layoutColor.setOnClickListener(null);
}
else {
lHolder.layoutColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveElement(fPosition);
}
});
}
lHolder.hText.setText(mDataList.get(position));
lHolder.layoutRoot.setBackgroundResource(0);
mHolder.set(position, lHolder);
return row;
}
}
protected void moveElement(int _element) {
if (mDataList.get(_element).equals(mSolution.get(_element)))
return;
if (mSwap1==-1)
{
System.out.println("setting swap1=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap1 = _element;
}
else
{
if (mSwap2==-1)
{
System.out.println("setting swap2=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap2 = _element;
}
}
if (mSwap1!=-1)
{
System.out.println("running animation on mSwap1=" + mSwap1);
mHolder.get(mSwap1).layoutRoot.clearAnimation();
mHolder.get(mSwap1).layoutRoot.startAnimation(mAnimation);
}
/***** THIS IS WHAT DOES NOT WORK *****/
if (mSwap2!=-1)
{
System.out.println("running animation on mSwap2=" + mSwap2);
mHolder.get(mSwap2).layoutRoot.clearAnimation();
mHolder.get(mSwap2).layoutRoot.startAnimation(mAnimation);
}
if (mSwap1!=-1 && mSwap2!=-1)
{
mHolder.get(mSwap1).layoutRoot.setBackgroundColor(0);
mHolder.get(mSwap2).layoutRoot.setBackgroundColor(0);
if (mSwap1==mSwap2)
{
mSwap1 = mSwap2 = -1;
return;
}
Collections.swap(mDataList, mSwap1, mSwap2);
Collections.swap(mHolder, mSwap1, mSwap2);
Collections.swap(dataObjs, mSwap1, mSwap2);
mSwap1 = mSwap2 = -1;
notifyDataSetChanged();
}
}
}
Everything works fine when I perform Collections.swap(list, mSwap1, mSwap2), elements are correctly swapped.
First animation (mSwap1) is run fine; my problem is that when second animation is run (mSwap2), it is executed on another element in screen even if mSwap2 is right (e.g.: mSwap1=1 -> second element in list is animated, mSwap2=2 -> n-1 element and n-2 element in list are animated where n is the number of visible elemnts).
I've solved my problem replacing animation calls
mHolder.get(idx).layoutRoot.clearAnimation();
mHolder.get(idx).layoutRoot.startAnimation(mAnimation);
with the following method
private void animateItem(int _index, Animation _animation) {
if (
_index<mLvSelector.getFirstVisiblePosition() // selected item is above first visible element
|| _index>mLvSelector.getLastVisiblePosition() // selected item is below last visible element
)
// element is invisible -> no animation
return;
int newIndex = _index;
if (
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH // before android 4.0
&& mSwap2>=0 // second selection
&& mSwap1!=mSwap2 // second selection differs from first selection
)
newIndex = mLvSelector.getFirstVisiblePosition() + (mLvSelector.getLastVisiblePosition() - _index);
mHolder.get(newIndex).layoutRoot.clearAnimation();
mHolder.get(newIndex).layoutRoot.startAnimation(_animation);
}
Adding Animation argument to the method allows to differentiate animation between elements (mSwap1 and mSwap2).

Categories

Resources