我有一个带有自定义适配器的列表视图。该列表视图的每一行都包含两个文本视图和一个viewflipper。
当我单击一个按钮时,我想用随机文本为每行设置动画/翻转。 对于动画,我使用了View Flipper,但是动画不能一个接一个地工作,而是对所有行同时工作。
我的按钮点击代码如下。
public void onButtonClick(View view) {
customAdapter.setFlipRow(!(customAdapter.isFlipRow()));
listView.invalidateViews();
}
自定义适配器:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
boolean flag[]= null;
int flags[];
LayoutInflater inflter;
Random r = null;
private static String TAG = "CustomAdapter";
public ViewHolder getViewHolder() {
return viewHolder;
}
ViewHolder viewHolder;
public boolean isFlipRow() {
return flipRow;
}
public void setFlipRow(boolean flipRow) {
this.flipRow = flipRow;
}
private boolean flipRow = false;
public CustomAdapter(Context applicationContext, String[] countryList) {
this.context = context;
this.countryList = countryList;
flag = new boolean[countryList.length];
for(int f = 0;f<flag.length;f++)
{
flag[f] = false;
}
inflter = (LayoutInflater.from(applicationContext));
r = new Random(5);
}
private static class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
View v = view;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflter.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
int rInt = r.nextInt(5);
if(isFlipRow()){
viewHolder.country1.setText("Random No: "+rInt);
viewHolder.viewFlipper.setFlipInterval(1000);
if(pos <= countryList.length-1)
{
flag[pos] = true;
if(pos == 0 && flag[pos]){
viewHolder.viewFlipper.startFlipping();
}
if(pos != 0 && flag[pos-1] == true){
viewHolder.viewFlipper.startFlipping();
flag[pos-1] = false;
/*viewHolder.viewFlipper.stopFlipping();
I tried to have a boolean flag for each row,
setting it before startFlipping() and resetting it after
stopFlipping().But adding this stopFlipping() here
stops all rows except first row and all rows flip at the
same time.Moreover getView is called only during setAdapter()
and invalidateviews().*/
}
if(pos == countryList.length-1){
Arrays.fill(flag,false);
}
}
}
else{
viewHolder.viewFlipper.stopFlipping();
Arrays.fill(flag,false);
}
return view;
}
}
我需要的是一次对每一行进行动画处理,但是现在所有行都在同时进行动画处理。任何见解或输入都将非常有帮助。
答案 0 :(得分:1)
尝试一下:
public void onButtonClick(View view) {
customAdapter.flipRow(view);
}
和适配器:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
int rnds[];
LayoutInflater inflater;
Random r;
private boolean flipRow = false;
private int flipRowNum = 0;
Button btn;
final private static String TAG = "CustomAdapter";
final private static int FLIP_DELAY = 1000;
final private static int FLIP_COUNT = 2;
public boolean isFlipRow() {
return flipRow;
}
public void flipRow(View view) {
btn = (Button)view;
btn.setEnabled(false);
flipRow = true;
notifyDataSetChanged();
}
public CustomAdapter(Context context, String[] countryList) {
this.context = context;
this.countryList = countryList;
inflater = (LayoutInflater.from(context));
r = new Random(5);
rnds = new int[countryList.length];
for(int f = 0;f<rnds.length; f++)
{
rnds[f] = r.nextInt(5);
}
}
private class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
@Override
public int getCount() {
return countryList.length;
}
@Override
public String getItem(int i) {
return countryList[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
//viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
viewHolder.country1.setText("Random No: " + rnds[pos]);
viewHolder.viewFlipper.setFlipInterval(0);
if(isFlipRow() && (pos == flipRowNum)){
viewHolder.viewFlipper.startFlipping();
viewHolder.viewFlipper.setFlipInterval(FLIP_DELAY);
viewHolder.viewFlipper.postDelayed(new Runnable() { //Stop flipping
@Override
public void run() {
viewHolder.viewFlipper.stopFlipping();
flipRow = !flipRow;
flipRowNum++;
if(flipRowNum == countryList.length) flipRowNum = 0;
btn.setEnabled(true);
}
}, (2*FLIP_COUNT - 1)*FLIP_DELAY + 50);
}
return view;
}
}
希望有帮助!