Animating (Translation) Horizontal listview items

643 views Asked by At

I've an horizontal listview with several textviews. I want to translate (animate) these textviews (items) only from right to left as a circular way, while click a button. Not translate the horizontal listview layout. Is this possible?

Edit:

For this horizontal listview, i used an adapter to load the values into that list.

Like this:

private static String[] dataObjects = new String[]{ "Text #1",
    "Text #2",
    "Text #3","Text #4","Text #5","Text #6","Text #7","Text #8","Text #9","Text #10" };

private BaseAdapter mAdapter = new BaseAdapter() {

    @Override
    public int getCount() {
        return dataObjects.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint({ "ViewHolder", "InflateParams" })
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        //Button button = (Button) retval.findViewById(R.id.clickbutton);
        //button.setOnClickListener(mOnButtonClicked);
        title.setText(dataObjects[position]);

        return retval;
    }       

};

And set this adapter to the horizontal listview as:

listView=(HorizontalListView)findViewById(R.id.simple_list);
    listView.setAdapter(mAdapter);

Thanks in advance...

1

There are 1 answers

6
Mohammed Ali On

Make translate.xml in res -> anim directory. Code:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_decelerate_interpolator"
 android:duration="500"
 android:fillAfter="true"
 android:fromXDelta="100%"
 android:toXDelta="0%" >
  />

Change percentages in android:fromXDelta="100%" android:toXDelta="0%" to fit your needs. Now use :

animationT = AnimationUtils.loadAnimation(myContext,
        R.anim.translate);

//put inside your clickListener
button.setOnClickListener (new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    textView1.startAnimation(animationT);
    textView2.startAnimation(animationT);
    textView3.startAnimation(animationT);
});