在ListView的Andr​​oid活性转换 [英] Android Activity Transition in a ListView

查看:160
本文介绍了在ListView的Andr​​oid活性转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在Android的下列活动转变:

1 我们看到一个平常的ListView。现在( 2 ),当我把我的手指一个ListView元素和幻灯片我的手指向左,所有其他的ListView元素离开,但一有我的手指上。如果要是完成了转型,在选择的ListView元件膨胀在屏幕上,并显示一些内容。

问题:这是可能的转型与股票API函数

解决方案

虽然@hasanghaforian是techincally正确的,继承适配器视图是非常困难的。我建议你​​去使用这种方法。我已经包括一个完整的工作的例子。

  1. 把你的ListView视图在一个RelativeLayout的
  2. 的OnClick,抬高你的渲染器的副本
  3. 找到了在那里的关系渲染坐到ListView
  4. 的父全局坐标
  5. 添加复制渲染到RelativeLayout的(ListView控件的母公司)
  6. 动画列表视图客场
  7. 在该动画的末尾,动画的新的渲染
  8. 利润!

 公共类MainActivity延伸活动{

    私人RelativeLayout的布局;
    私人的ListView ListView的;
    私人MyRenderer selectedRenderer;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        布局=新RelativeLayout的(这一点);
        的setContentView(布局);
        ListView控件=新的ListView(本);
        RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.addView(ListView中,RLP);

        listView.setAdapter(新MyAdapter());
        listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){

                //找出被点击的视图坐在关系到
                //父容器
                INT T = view.getTop()+ listView.getTop();
                int类型l = view.getLeft()+ listView.getLeft();

                //创建列表视图的副本,并将其添加到父
                // 容器
                //在同一地点是在列表视图
                selectedRenderer =新MyRenderer(view.getContext());
                RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(view.getWidth(),查看
                        .getHeight());
                rlp.topMargin = T;
                rlp.leftMargin =升;
                selectedRenderer.textView.setText(((MyRenderer)视图).textView.getText());
                layout.addView(selectedRenderer,RLP);
                view.setVisibility(View.INVISIBLE);

                //动画出的ListView
                动画outAni =新TranslateAnimation(Animation.RELATIVE_TO_SELF,0F,
                        Animation.RELATIVE_TO_SELF,-1f,Animation.RELATIVE_TO_SELF,0F,
                        Animation.RELATIVE_TO_SELF,0F);
                outAni.setDuration(1000);
                outAni.setFillAfter(真正的);
                outAni.setAnimationListener(新Animation.AnimationListener(){
                    @覆盖
                    公共无效onAnimationStart(动画动画){
                    }

                    @覆盖
                    公共无效onAnimationRepeat(动画动画){
                    }

                    @覆盖
                    公共无效onAnimationEnd(动画动画){
                        ScaleAnimation scaleAni =新ScaleAnimation(1F,
                                1F,1F,2F,
                                Animation.RELATIVE_TO_SELF,0.5F,
                                Animation.RELATIVE_TO_SELF,0.5F);
                        scaleAni.setDuration(400);
                        scaleAni.setFillAfter(真正的);
                        selectedRenderer.startAnimation(scaleAni);
                    }
                });

                listView.startAnimation(outAni);
            }
        });
    }

    公共类MyAdapter扩展了BaseAdapter {
        @覆盖
        公众诠释getCount将(){
            返回10;
        }

        @覆盖
        公共字符串的getItem(INT位置){
            返回的Hello World+位置;
        }

        @覆盖
        众长getItemId(INT位置){
            返回的位置;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            MyRenderer渲染器;
            如果(convertView!= NULL)
                渲染=(MyRenderer)convertView;
            其他
                渲染器=新MyRenderer(MainActivity.this);
            renderer.textView.setText(的getItem(位置));
            返回渲染器;
        }
    }

    公共类MyRenderer扩展RelativeLayout的{

        公众的TextView TextView的;

        公共MyRenderer(上下文的背景下){
            超(上下文);
            setPadding(20,20,20,20);
            setBackgroundColor(0xFFFF0000地址);

            RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.addRule(CENTER_IN_PARENT);
            TextView的=新的TextView(上下文);
            addView(TextView的,RLP);
        }

    }
}
 

I'm trying to accomplish the following Activity transition in Android:

In 1 we see an usual ListView. Now (2), when I place my finger on a ListView element and slide my finger to the left, all the other ListView elements move away, except the one which has my finger on it. If the transition if completed, the "selected" ListView element expands over the screen and shows some content.

Question: Is this transition possible with stock API functions?

解决方案

While @hasanghaforian is techincally correct, subclassing AdapterView is extremely difficult. I suggest you go with this approach. I've included a full working example.

  1. Put your ListView view in a relativeLayout
  2. OnClick, inflate a copy of your renderer
  3. Find the global coordinates for where the renderer sits in relationship to the parent of the ListView
  4. Add the copied renderer to the RelativeLayout (parent of ListView)
  5. Animate the listView away
  6. On the end of that animate, animate your new renderer
  7. Profit!

public class MainActivity extends Activity {

    private RelativeLayout layout;
    private ListView listView;
    private MyRenderer selectedRenderer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new RelativeLayout(this);
        setContentView(layout);
        listView = new ListView(this);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.addView(listView, rlp);

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

                // find out where the clicked view sits in relationship to the
                // parent container
                int t = view.getTop() + listView.getTop();
                int l = view.getLeft() + listView.getLeft();

                // create a copy of the listview and add it to the parent
                // container
                // at the same location it was in the listview
                selectedRenderer = new MyRenderer(view.getContext());
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(view.getWidth(), view
                        .getHeight());
                rlp.topMargin = t;
                rlp.leftMargin = l;
                selectedRenderer.textView.setText(((MyRenderer) view).textView.getText());
                layout.addView(selectedRenderer, rlp);
                view.setVisibility(View.INVISIBLE);

                // animate out the listView
                Animation outAni = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f,
                        Animation.RELATIVE_TO_SELF, 0f);
                outAni.setDuration(1000);
                outAni.setFillAfter(true);
                outAni.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ScaleAnimation scaleAni = new ScaleAnimation(1f, 
                                1f, 1f, 2f, 
                                Animation.RELATIVE_TO_SELF, 0.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAni.setDuration(400);
                        scaleAni.setFillAfter(true);
                        selectedRenderer.startAnimation(scaleAni);
                    }
                });

                listView.startAnimation(outAni);
            }
        });
    }

    public class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public String getItem(int position) {
            return "Hello World " + position;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyRenderer renderer;
            if (convertView != null)
                renderer = (MyRenderer) convertView;
            else
                renderer = new MyRenderer(MainActivity.this);
            renderer.textView.setText(getItem(position));
            return renderer;
        }
    }

    public class MyRenderer extends RelativeLayout {

        public TextView textView;

        public MyRenderer(Context context) {
            super(context);
            setPadding(20, 20, 20, 20);
            setBackgroundColor(0xFFFF0000);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.addRule(CENTER_IN_PARENT);
            textView = new TextView(context);
            addView(textView, rlp);
        }

    }
}

这篇关于在ListView的Andr​​oid活性转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆