Friday, July 15, 2011

Ticker

Ticker Method in MAinClass:

Gallery tickerGallery;
TimerThread timer;
Timer delay;
int sizeint=0,TickerLength=0;
int j=0;
boolean tickerLengthCalculated=false;
boolean pauseFlag=false,firstLoadFlag=false;

public void updateUIwithDataTicker() {

try{

tickerGallery.setAdapter(new custom_tickermessage(Aajtak.this,ticker_title,ticker_image));
tickerLengthCalculated=true;

tickerGallery.setFocusable(true);
tickerGallery.setFocusableInTouchMode(true);

timer = new TimerThread();
delay = new Timer();
delay.schedule(timer,30000, 30000);

}catch(Exception e){

Log.e(tag,"Error in updateUIwithDataTicker"+e);

}
}
};


class TickerLengthThread extends TimerTask{
@Override
public void run() {


this.cancel();
TickerLength=0;

tickerLengthCalculated=true;

}
}


/*
* Activity Pause and Resume
* =========================================================================================
*/
@Override
public void onPause(){
if(tickerLengthCalculated){
try {
pauseFlag=true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onPause();

}
@Override
public void onResume(){

try {
if(pauseFlag)
if(tickerLengthCalculated){
pauseFlag=false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onResume();

}

@Override
public void onDestroy(){
if(tickerLengthCalculated){
delay.cancel();
timer.cancel();
}

super.onDestroy();
}
class TimerThread extends TimerTask{
@Override
public void run() {
mHandler.post(mUpdateTicker1);

}
}

final Runnable mUpdateTicker1 = new Runnable() {
public void run() {
RefreshTicket();
if(TickerLength {

++TickerLength;
tickerGallery.setSelection(TickerLength);
}


}
};

/**
* Method to call Parser and cancel working timer Thread.
*/
private void RefreshTicket()
{
if(tickerLengthCalculated){
if(TickerLength>=ticker_size-1)
{
timer.cancel();
delay.cancel();

mHandler.post(mUpdateResultsTicker);
}

}
}

Custom Adaspter:


public class custom_tickermessage extends BaseAdapter {

ImageLoader imageLoader;
LayoutInflater myInflater;
private Activity activity;
String[] txt_title,image;
public custom_tickermessage(Activity a, String[] txt_title,String[] image) {
// TODO Auto-generated constructor stub
activity = a;
// TODO Auto-generated constructor stub
myInflater=LayoutInflater.from(a);

this.txt_title=txt_title;
this.image=image;

imageLoader=new ImageLoader(activity.getApplicationContext());
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return this.txt_title.length;
}




@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View vi, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(vi==null)
{
vi = myInflater.inflate(R.layout.hometickermessage, null);
holder=new ViewHolder();

holder.image=(ImageView)vi.findViewById(R.id.tic_image);
holder.head=(TextView)vi.findViewById(R.id.tic_message);

vi.setTag(holder);
}
else
{
holder=(ViewHolder)vi.getTag();
}
holder.head.setText(txt_title[position]);
holder.image.setTag(image[position]);

imageLoader.DisplayImage(image[position], activity, holder.image);

return vi;
}

class ViewHolder{

ImageView image;
TextView head;
}


}


Layout:



android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6E6464"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
android:id="@+id/tic_image"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="14dip"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/tic_image"
android:id="@+id/tic_message"
/>





Main Class Layout:

android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#ABEEF5"
android:id="@+id/top_rel2"
android:layout_below="@id/top_rel"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:id="@+id/gallery_ticker"
android:spacing="2dip"
android:gravity="center_vertical"
android:fadingEdge="none"
/>

No comments:

Post a Comment