0

无法通过 onItemClickListener ListView 的方法为 ImageView 设置动画。ListView 位于片段内。有人可以建议我该怎么做吗?

public class MainFragment extends Fragment {

View view;



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    ListView lv = (ListView)view.findViewById(R.id.listf);
    this.view= view;


lv.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){


          ImageView i = (ImageView)view.findViewById(R.id.imgv);
          AnimationSet as = new AnimationSet(true);
          Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.anim1);              
          Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.anim2);
          as.addAnimation(animation1);
          as.addAnimation(animation2);
          i.setImageResource(R.drawable.logocpic1);
          i.startAnimation(as);
    }
}
4

3 回答 3

2
I think you have to try this following code it works for me....

动画 animation1 = AnimationUtils.loadAnimation(getapplicationContext(), R.anim.anim1);

代替

动画 animation1 = AnimationUtils.loadAnimation(this, R.anim.anim1);

可能对你有用。

于 2013-04-18T10:52:11.053 回答
1

好的,您遇到错误是因为您在非 UI 线程中执行 UI 操作。你可以使用下面的代码,

public class MainFragment extends Fragment {
View view;
ImageView i;
.
.
.
.

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
.
.
.   


   getActivity().runOnUiThread(new Runnable() {
      public void run() {
         i = (ImageView)getActivity().findViewById(R.id.imgv);
      }
   });
于 2012-12-06T14:20:20.110 回答
0

请改成喜欢

Animation animation1 = AnimationUtils.loadAnimation(MainFragment.this, R.anim.anim1);
于 2015-03-18T09:37:00.550 回答